Loads `image` to a PIL Image. Args: image (`str` or `PIL.Image.Image`): The image to convert to the PIL Image format. convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], *optional*): A conversion method to apply to the image after loading i
(
image: Union[str, PIL.Image.Image], convert_method: Optional[Callable[[PIL.Image.Image], PIL.Image.Image]] = None
)
| 11 | |
| 12 | |
| 13 | def load_image( |
| 14 | image: Union[str, PIL.Image.Image], convert_method: Optional[Callable[[PIL.Image.Image], PIL.Image.Image]] = None |
| 15 | ) -> PIL.Image.Image: |
| 16 | """ |
| 17 | Loads `image` to a PIL Image. |
| 18 | |
| 19 | Args: |
| 20 | image (`str` or `PIL.Image.Image`): |
| 21 | The image to convert to the PIL Image format. |
| 22 | convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], *optional*): |
| 23 | A conversion method to apply to the image after loading it. When set to `None` the image will be converted |
| 24 | "RGB". |
| 25 | |
| 26 | Returns: |
| 27 | `PIL.Image.Image`: |
| 28 | A PIL Image. |
| 29 | """ |
| 30 | if isinstance(image, str): |
| 31 | if image.startswith("http://") or image.startswith("https://"): |
| 32 | image = PIL.Image.open(requests.get(image, stream=True).raw) |
| 33 | elif os.path.isfile(image): |
| 34 | image = PIL.Image.open(image) |
| 35 | else: |
| 36 | raise ValueError( |
| 37 | f"Incorrect path or URL. URLs must start with `http://` or `https://`, and {image} is not a valid path." |
| 38 | ) |
| 39 | elif isinstance(image, PIL.Image.Image): |
| 40 | image = image |
| 41 | else: |
| 42 | raise ValueError( |
| 43 | "Incorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image." |
| 44 | ) |
| 45 | |
| 46 | image = PIL.ImageOps.exif_transpose(image) |
| 47 | |
| 48 | if convert_method is not None: |
| 49 | image = convert_method(image) |
| 50 | else: |
| 51 | image = image.convert("RGB") |
| 52 | |
| 53 | return image |
| 54 | |
| 55 | |
| 56 | def load_video( |
no outgoing calls