@@ -166,6 +166,54 @@ def _generator(body: dict) -> Generator[Image, None, None]:
166166 # Pass the response body to the generator
167167 return _generator (response .json ())
168168
169+ def import_image (
170+ self ,
171+ source : Union [str , bytes ],
172+ reference : Optional [str ] = None ,
173+ message : Optional [str ] = None ,
174+ changes : Optional [List [str ]] = None ,
175+ ) -> "Image" :
176+ """Import a tarball as an image (equivalent of 'podman import').
177+
178+ Args:
179+ source: Path to a tarball (str) or a file-like object opened in binary mode.
180+ reference: Optional reference for the new image (e.g. 'myimage:latest').
181+ message: Optional commit message.
182+ changes: Optional list of Dockerfile-style instructions
183+ (e.g. ['CMD /bin/bash', 'ENV FOO=bar']).
184+
185+ Returns:
186+ An Image object for the newly imported image.
187+
188+ Raises:
189+ APIError: when service returns an error.
190+ """
191+ params = {}
192+ if reference :
193+ params ["reference" ] = reference
194+ if message :
195+ params ["message" ] = message
196+ if changes :
197+ params ["changes" ] = changes # requests sends repeated keys as a list
198+
199+ if isinstance (source , str ):
200+ with open (source , "rb" ) as f :
201+ data = f .read ()
202+ else :
203+ data = source .read ()
204+
205+ response = self .client .post (
206+ "/images/import" ,
207+ params = params ,
208+ data = data ,
209+ headers = {"Content-Type" : "application/x-tar" },
210+ )
211+ response .raise_for_status ()
212+
213+ body = response .json ()
214+ image_id = body .get ("Id" ) or body .get ("id" )
215+ return self .get (image_id )
216+
169217 def prune (
170218 self ,
171219 all : Optional [bool ] = False , # pylint: disable=redefined-builtin
0 commit comments