Uh oh!
There was an error while loading. Please reload this page.
Remove conversion to RGB - #6479
Conversation
patrickvonplaten
commented
Jan 9, 2024
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: fromtypingimportUnionimportosimportrequestsimportPIL.ImageimportPIL.ImageOpsdefload_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. """ifisinstance(image, str):
ifimage.startswith("http://") orimage.startswith("https://"):
image=PIL.Image.open(requests.get(image, stream=True).raw)
elifos.path.isfile(image):
image=PIL.Image.open(image)
else:
raiseValueError(
f"Incorrect path or URL. URLs must start with `http://` or `https://`, and {image} is not a valid path."
)
elifisinstance(image, PIL.Image.Image):
passelse:
raiseValueError(
"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 providedifconvert_methodisnotNone:
image=convert_method(image)
else:
image=image.convert('RGB')
returnimageAnd when using it: defcustom_convert_method(img):
returnimg.convert('L', colors=8)
loaded_image=load_image("image.jpg", convert_method=custom_convert_method)What do you think @patrickvonplaten ? |
HuggingFaceDocBuilderDev
commented
Jan 9, 2024
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. |
Uh oh!
There was an error while loading. Please reload this page.
Update docstring Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
sayakpaul
commented
Jan 11, 2024
Let's get the code quality issues fixed. |
DN6
left a comment
There was a problem hiding this comment.
LGTM 👍🏽 Let's just fix code QC issues.
yelboudouri
commented
Jan 11, 2024
Can't get that QC to pass, it says 'Import block is unsorted or unformatted,' but the imports are already sorted. |
yiyixuxu
commented
Jan 11, 2024
@yelboudouri did you run |
yelboudouri
commented
Jan 11, 2024
@yiyixuxu forgot that 🤦♂️ Now it should be all good. |
sayakpaul
commented
Jan 12, 2024
Thank you for your contributions! |
This PR has caused a breaking change. Does this mean that 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.