Spaces:
Running
on
Zero
Running
on
Zero
| import os | |
| from typing import Union | |
| import PIL.Image | |
| import PIL.ImageOps | |
| import requests | |
| def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: | |
| """ | |
| Loads `image` to a PIL Image. | |
| Args: | |
| image (`str` or `PIL.Image.Image`): | |
| The image to convert to the PIL Image format. | |
| Returns: | |
| `PIL.Image.Image`: | |
| A PIL Image. | |
| """ | |
| if isinstance(image, str): | |
| if image.startswith("http://") or image.startswith("https://"): | |
| image = PIL.Image.open(requests.get(image, stream=True).raw) | |
| elif os.path.isfile(image): | |
| image = PIL.Image.open(image) | |
| else: | |
| raise ValueError( | |
| f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path" | |
| ) | |
| elif isinstance(image, PIL.Image.Image): | |
| image = image | |
| else: | |
| raise ValueError( | |
| "Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image." | |
| ) | |
| image = PIL.ImageOps.exif_transpose(image) | |
| image = image.convert("RGB") | |
| return image | |