Skip to content

Commit c6b0458

Browse files
yelboudouripatrickvonplatensayakpaul
authored
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: Patrick von Platen <patrick.v.platen@gmail.com> * Update docstring * Optimize imports * Optimize imports (2) * Reformat code --------- Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent 5dc3471 commit c6b0458

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/diffusers/utils/loading_utils.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import os
2-
from typing import Union
2+
from typing import Callable, Union
33

44
import PIL.Image
55
import PIL.ImageOps
66
import requests
77

88

9-
def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
9+
def load_image(
10+
image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL.Image.Image], PIL.Image.Image] = None
11+
) -> PIL.Image.Image:
1012
"""
1113
Loads `image` to a PIL Image.
1214
1315
Args:
1416
image (`str` or `PIL.Image.Image`):
1517
The image to convert to the PIL Image format.
18+
convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], optional):
19+
A conversion method to apply to the image after loading it.
20+
When set to `None` the image will be converted "RGB".
21+
1622
Returns:
1723
`PIL.Image.Image`:
1824
A PIL Image.
@@ -24,14 +30,18 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
2430
image = PIL.Image.open(image)
2531
else:
2632
raise ValueError(
27-
f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path"
33+
f"Incorrect path or URL. URLs must start with `http://` or `https://`, and {image} is not a valid path."
2834
)
29-
elif isinstance(image, PIL.Image.Image):
30-
image = image
3135
else:
3236
raise ValueError(
33-
"Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image."
37+
"Incorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image."
3438
)
39+
3540
image = PIL.ImageOps.exif_transpose(image)
36-
image = image.convert("RGB")
41+
42+
if convert_method is not None:
43+
image = convert_method(image)
44+
else:
45+
image = image.convert("RGB")
46+
3747
return image

0 commit comments

Comments
 (0)