-
-
Notifications
You must be signed in to change notification settings - Fork 498
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
«OSError: cannot write mode RGBA as JPEG» when get_thumbnail for PNG in mode=P #564
Comments
Is there going to be a PR for this? |
I have the same issue when saving an image through django rest framework. Where should I put this piece of code @stopdesign ? |
This comment is not related to the issue but might help people like me looking for a solution. If you don't mind having PNG format thumbnails you can use the following setting
This saves PNG images as PNG thumbnails and thus bypasses the RGBA problem completely. |
Still relevant :) |
I'm curious as to why sorl worked fine before and now it doesn't in this case. Maybe it was better to just throw a warning? |
So part of the issue for me is in the fact that sorl uses the file extension to determine the output format instead of actually reading the headers of the file like Pillow does to determine format. That means that the workaround Should this be an independent issue? |
Same problem here. Will be there a fix for this? |
Any update on this? :) |
My current work-around: # ImageField from model
image_field = image_model.image
img = Image.open(image_field.file)
fill_color = "#ffffff"
if img.mode in ("RGBA", "LA"):
# Necessary because of "Bad Transparency Mask" issues with LA mode, this makes errors disappear, untested if LA conversion to RGBA results in a desirable transparency mask
if img.mode == "LA":
img = img.convert("RGBA")
background = Image.new(img.mode[:-1], img.size, fill_color)
background.paste(img, img.split()[-1])
img = background
thumbnail = get_thumbnail(img, "800x800", quality=90, format="JPEG")
else:
thumbnail = get_thumbnail(img, "800x800", quality=90) As you can see, it's possible to put this behavior directly in By suppling a kwarg This way - even if for whatever reason the developer wants to do it on PNG, it will happen. If the kwarg is not supplied, then we can still fail if asked to convert a PNG with an alpha channel to JPEG (which IMO is correct behavior). |
Bump |
What did I do?
Tried to get thumbnail (JPEG without transparency) for PNG image in mode=P (indexed colours?).
(It works good for PNG with mode=RGBA but not mode=P)
What did I expect to happen?
Saving thumbnail without any exceptions, flattened alpha-channel into solid background of some colour (eg THUMBNAIL_PADDING_COLOR, some specific colour from settings or 'white').
Test image
https://user-images.githubusercontent.com/244666/45594613-a5aba780-b9a6-11e8-9321-da6710f9fb4f.png
What actually happened?
Versions
Python == 3.6.5
Pillow == 5.2.0 (or any versions >= 4.2.0)
sorl-thumbnail == 12.4.1
Related issue
python-pillow/Pillow#2609
How to fix it?
You need to make a background image with certain colour and merge source image into background:
Correct result
The text was updated successfully, but these errors were encountered: