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])
| 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: |