-
Notifications
You must be signed in to change notification settings - Fork 6k
Remove conversion to RGB #6479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove conversion to RGB #6479
Conversation
Hey @yelboudouri, This change would be a breaking change - could you add a parameter: "return_type='....'" to the function that allows to load RGBA images? |
I'm thinking of a way to solve this problem without breaking the code and also solving it once and for all because adding a The updated code will be something like: from typing import Union
import os
import requests
import PIL.Image
import PIL.ImageOps
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):
The conversion method for converting the image to RGB. Default is None.
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):
pass
else:
raise ValueError(
"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)
# Check if a custom convert method is provided
if convert_method is not None:
image = convert_method(image)
else:
image = image.convert('RGB')
return image And when using it: def custom_convert_method(img):
return img.convert('L', colors=8)
loaded_image = load_image("image.jpg", convert_method=custom_convert_method) What do you think @patrickvonplaten ? |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Update docstring Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super! Thanks for this!
Let's get the code quality issues fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏽 Let's just fix code QC issues.
Can't get that QC to pass, it says 'Import block is unsorted or unformatted,' but the imports are already sorted. |
@yelboudouri did you run |
@yiyixuxu forgot that 🤦♂️ Now it should be all good. |
Thank you for your contributions! |
This PR has caused a breaking change. Does this mean that @yelboudouri @patrickvonplaten fix: #6904 |
* 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>
What does this PR do?
I removed the conversion to RGB in src/diffusers/utils/loading_utils.py. I spent hours trying to figure out why my RGBA images were getting a black background upon loading. I then realized that this function internally converts images to RGB, which should not be the case because this function is only responsible for loading the image. It's up to the user to decide which mode they want to convert their images to after loading them.
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.