"src/git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "071807c853d28f04f8f9b1db54cd124fd6fcda2c"
Unverified Commit c6b04589 authored by Yassine El Boudouri's avatar Yassine El Boudouri Committed by GitHub
Browse files

Remove conversion to RGB (#6479)



* Remove conversion to RGB

* Add a Conversion Function

* Add type hint for convert_method

* Update src/diffusers/utils/loading_utils.py

Update docstring
Co-authored-by: default avatarPatrick von Platen <patrick.v.platen@gmail.com>

* Update docstring

* Optimize imports

* Optimize imports (2)

* Reformat code

---------
Co-authored-by: default avatarPatrick von Platen <patrick.v.platen@gmail.com>
Co-authored-by: default avatarSayak Paul <spsayakpaul@gmail.com>
parent 5dc34713
import os import os
from typing import Union from typing import Callable, Union
import PIL.Image import PIL.Image
import PIL.ImageOps import PIL.ImageOps
import requests import requests
def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: def load_image(
image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL.Image.Image], PIL.Image.Image] = None
) -> PIL.Image.Image:
""" """
Loads `image` to a PIL Image. Loads `image` to a PIL Image.
Args: Args:
image (`str` or `PIL.Image.Image`): image (`str` or `PIL.Image.Image`):
The image to convert to the PIL Image format. 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 it.
When set to `None` the image will be converted "RGB".
Returns: Returns:
`PIL.Image.Image`: `PIL.Image.Image`:
A PIL Image. A PIL Image.
...@@ -24,14 +30,18 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: ...@@ -24,14 +30,18 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
image = PIL.Image.open(image) image = PIL.Image.open(image)
else: else:
raise ValueError( raise ValueError(
f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path" 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: else:
raise ValueError( raise ValueError(
"Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image." "Incorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image."
) )
image = PIL.ImageOps.exif_transpose(image) image = PIL.ImageOps.exif_transpose(image)
image = image.convert("RGB")
if convert_method is not None:
image = convert_method(image)
else:
image = image.convert("RGB")
return image return image
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment