MCPcopy Create free account
hub / github.com/scanny/python-pptx / from_file

Method from_file

src/pptx/parts/image.py:156–174  ·  view source on GitHub ↗

Return a new |Image| object loaded from `image_file`. `image_file` can be either a path (str) or a file-like object.

(cls, image_file: str | IO[bytes])

Source from the content-addressed store, hash-verified

154
155 @classmethod
156 def from_file(cls, image_file: str | IO[bytes]) -> Image:
157 """Return a new |Image| object loaded from `image_file`.
158
159 `image_file` can be either a path (str) or a file-like object.
160 """
161 if isinstance(image_file, str):
162 # treat image_file as a path
163 with open(image_file, "rb") as f:
164 blob = f.read()
165 filename = os.path.basename(image_file)
166 else:
167 # assume image_file is a file-like object
168 # ---reposition file cursor if it has one---
169 if callable(getattr(image_file, "seek")):
170 image_file.seek(0)
171 blob = image_file.read()
172 filename = None
173
174 return cls.from_blob(blob, filename)
175
176 @property
177 def blob(self) -> bytes:

Calls 3

readMethod · 0.80
basenameMethod · 0.80
from_blobMethod · 0.45

Tested by 2