From fc0799efa690ce99a013dcd84406e90c0da7b944 Mon Sep 17 00:00:00 2001 From: Yassine Date: Sun, 7 Jan 2024 20:09:52 +0100 Subject: [PATCH 1/8] Remove conversion to RGB --- src/diffusers/utils/loading_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 279aa6fe737b..72336838789e 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -33,5 +33,5 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: "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 From 4db7f1c7cd5c684917a9bca3f439ec1b7b1cccf1 Mon Sep 17 00:00:00 2001 From: Yassine Date: Tue, 9 Jan 2024 16:09:17 +0100 Subject: [PATCH 2/8] Add a Conversion Function --- src/diffusers/utils/loading_utils.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 72336838789e..64994caeaea2 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -6,13 +6,16 @@ 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=None) -> 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. + convert_method (`function`, optional): + A conversion method to apply to the image after loading it. The default is None. + Returns: `PIL.Image.Image`: A PIL Image. @@ -24,14 +27,18 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.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" + 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." + "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) + if convert_method is not None: + image = convert_method(image) + else: + image = image.convert('RGB') + return image From f42e61dfc5f2edd10a3c08ccd6dd592b5ee2360c Mon Sep 17 00:00:00 2001 From: Yassine Date: Thu, 11 Jan 2024 13:21:59 +0100 Subject: [PATCH 3/8] Add type hint for convert_method --- src/diffusers/utils/loading_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 64994caeaea2..a75390577da8 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -1,12 +1,12 @@ import os -from typing import Union +from typing import Union, Callable import PIL.Image import PIL.ImageOps import requests -def load_image(image: Union[str, PIL.Image.Image], convert_method=None) -> 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. From 4e9d815cfa72e3980338bb6ba96797bfab69067a Mon Sep 17 00:00:00 2001 From: Yassine El Boudouri Date: Thu, 11 Jan 2024 15:08:16 +0100 Subject: [PATCH 4/8] Update src/diffusers/utils/loading_utils.py Update docstring Co-authored-by: Patrick von Platen --- src/diffusers/utils/loading_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index a75390577da8..2b92abc902e1 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -14,7 +14,7 @@ def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL image (`str` or `PIL.Image.Image`): The image to convert to the PIL Image format. convert_method (`function`, optional): - A conversion method to apply to the image after loading it. The default is None. + A conversion method to apply to the image after loading it. When set to `None` the image will be converted "RGB". Returns: `PIL.Image.Image`: From d809652e0c1a818c50a6f7f72675126cce2b4bc9 Mon Sep 17 00:00:00 2001 From: Yassine Date: Thu, 11 Jan 2024 15:10:19 +0100 Subject: [PATCH 5/8] Update docstring --- src/diffusers/utils/loading_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 2b92abc902e1..8436e8c82873 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -13,8 +13,9 @@ def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL Args: image (`str` or `PIL.Image.Image`): The image to convert to the PIL Image format. - convert_method (`function`, optional): - A conversion method to apply to the image after loading it. When set to `None` the image will be converted "RGB". + 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: `PIL.Image.Image`: From a528d55a67eed78f47c92aca6f8e3f57b1244e0f Mon Sep 17 00:00:00 2001 From: Yassine Date: Thu, 11 Jan 2024 18:21:46 +0100 Subject: [PATCH 6/8] Optimize imports --- src/diffusers/utils/loading_utils.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 8436e8c82873..c298e7a56b34 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -1,12 +1,11 @@ import os from typing import Union, Callable -import PIL.Image -import PIL.ImageOps import requests +from PIL import Image, ImageOps -def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL.Image.Image], PIL.Image.Image] = None) -> PIL.Image.Image: +def load_image(image: Union[str, Image], convert_method: Callable[[Image], Image] = None) -> Image: """ Loads `image` to a PIL Image. @@ -23,9 +22,9 @@ def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL """ if isinstance(image, str): if image.startswith("http://") or image.startswith("https://"): - image = PIL.Image.open(requests.get(image, stream=True).raw) + image = Image.open(requests.get(image, stream=True).raw) elif os.path.isfile(image): - image = PIL.Image.open(image) + image = 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." @@ -35,7 +34,7 @@ def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL "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 = ImageOps.exif_transpose(image) if convert_method is not None: image = convert_method(image) From a3c6a08cd0eeced0aab7008191ba460e93e423f6 Mon Sep 17 00:00:00 2001 From: Yassine Date: Thu, 11 Jan 2024 18:25:23 +0100 Subject: [PATCH 7/8] Optimize imports (2) --- src/diffusers/utils/loading_utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index c298e7a56b34..65de748178e1 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -2,10 +2,11 @@ from typing import Union, Callable import requests -from PIL import Image, ImageOps +import PIL.Image +import PIL.ImageOps -def load_image(image: Union[str, Image], convert_method: Callable[[Image], Image] = None) -> 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. @@ -22,9 +23,9 @@ def load_image(image: Union[str, Image], convert_method: Callable[[Image], Image """ if isinstance(image, str): if image.startswith("http://") or image.startswith("https://"): - image = Image.open(requests.get(image, stream=True).raw) + image = PIL.Image.open(requests.get(image, stream=True).raw) elif os.path.isfile(image): - image = Image.open(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." @@ -34,7 +35,7 @@ def load_image(image: Union[str, Image], convert_method: Callable[[Image], Image "Incorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image." ) - image = ImageOps.exif_transpose(image) + image = PIL.ImageOps.exif_transpose(image) if convert_method is not None: image = convert_method(image) From d835adea61148d921b9954610617ebb1c00398ee Mon Sep 17 00:00:00 2001 From: Yassine Date: Thu, 11 Jan 2024 22:00:24 +0100 Subject: [PATCH 8/8] Reformat code --- src/diffusers/utils/loading_utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index 65de748178e1..e129d5f3e366 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -1,12 +1,14 @@ import os -from typing import Union, Callable +from typing import Callable, Union -import requests import PIL.Image import PIL.ImageOps +import requests -def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL.Image.Image], PIL.Image.Image] = None) -> 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. @@ -40,6 +42,6 @@ def load_image(image: Union[str, PIL.Image.Image], convert_method: Callable[[PIL if convert_method is not None: image = convert_method(image) else: - image = image.convert('RGB') + image = image.convert("RGB") return image