Skip to content
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

cannot write mode RGBA as JPEG (4.2.0) #2609

Closed
karolpawlowski opened this issue Jul 4, 2017 · 6 comments
Closed

cannot write mode RGBA as JPEG (4.2.0) #2609

karolpawlowski opened this issue Jul 4, 2017 · 6 comments

Comments

@karolpawlowski
Copy link

@karolpawlowski karolpawlowski commented Jul 4, 2017

What did you do?

Tried to save JPG file (any JPG file).

What did you expect to happen?

Saving without any exceptions (was working in 4.1.1 version)

What actually happened?

File /usr/lib/python3.6/site-packages/PIL/Image.py, line 1893, in save
save_handler(self, fp, filename)
File /usr/lib/python3.6/site-packages/PIL/JpegImagePlugin.py, line 604, in _save
raise IOError(cannot write mode %s as JPEG % im.mode)
OSError: cannot write mode RGBA as JPEG

What versions of Pillow and Python are you using?

4.2.0 (with 4.1.1 works properly) / Python 3.6

Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, Django, or buildout, try to replicate the issue just using Pillow.

im = Image.open(file_path)
im = im.convert("RGBA")
im.save(hidpi_path, file_type, quality=95)
@wiredfool
Copy link
Member

@wiredfool wiredfool commented Jul 4, 2017

Yep: https://github.com/python-pillow/Pillow/blob/master/docs/releasenotes/4.2.0.rst#removed-deprecated-items

JPEGs can't represent an alpha channel. It's been issuing a warning for a while, removed in this release.

@karolpawlowski
Copy link
Author

@karolpawlowski karolpawlowski commented Jul 4, 2017

Thanks @wiredfool ! What is preferred way to make it works with both PNG and JPEG files?

@wiredfool
Copy link
Member

@wiredfool wiredfool commented Jul 4, 2017

If it's got an alpha channel that you want to preserve, PNG is really the only reasonable choice. Alternately you can flatten it to a RGB image and save as a JPEG.

@ghost
Copy link

@ghost ghost commented Jul 7, 2017

Hi, i am interrested did you find a way to make it works with both PNG and JPEG ?

@radarhere
Copy link
Member

@radarhere radarhere commented Jul 8, 2017

@AymericHENRY Hi. While this is just reiterating what has already been said, you should have no problem saving an image in RGBA mode to PNG. To save an image in RGBA mode as JPEG means getting rid of the alpha channel, because JPEG doesn't transparency. So, if you have an image in RGBA mode, then converting it to RGB will let you save it as JPEG.

>>> from PIL import Image
>>> im = Image.new("RGBA",(100,100))
>>> im.save("test.jpg")
>>> Traceback (most recent call last):
  File "PIL/JpegImagePlugin.py", line 602, in _save
KeyError: 'RGBA'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 1893, in save
  File "PIL/JpegImagePlugin.py", line 604, in _save
OSError: cannot write mode RGBA as JPEG

>>> im = im.convert("RGB")
>>> im.save("test.jpg")
>>> 
@homm
Copy link
Member

@homm homm commented Jul 9, 2017

In most cases the discarding the alpha channel will give you undesirable result, because transparent pixels also have some unpredictable colors. It is much better to fill transparent pixels with certain color:

fill_color = ''  # your background
image = Image.open(file_path)
if image.mode in ('RGBA', 'LA'):
    background = Image.new(image.mode[:-1], image.size, fill_color)
    background.paste(image, image.split()[-1])
    image = background
im.save(hidpi_path, file_type, quality=95)
sfermigier added a commit to abilian/abilian-core that referenced this issue Jul 11, 2017
zach-adams added a commit to zach-adams/imgd that referenced this issue Jul 12, 2017
See python-pillow/Pillow#2609
Using solution proposed by @homm to make sure alpha channel is stripped from PNGs before conversion to JPGs.
jarun added a commit to jarun/imgp that referenced this issue Jul 24, 2017
* adding a quality option

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* removing the unnecessary check-ins

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* removing the unnecessary check-ins

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* minor changes: reframing the sentences

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* addressing review comments

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* addressing code review comments 2

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* addressing code review comments 3

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* addressing code review comments 4

Signed-off-by: ananyajana <ananya.jana@gmail.com>

* Update features

* Add Python 3.6 support

* Fix issue "cannot write mode RGBA as JPEG"

See python-pillow/Pillow#2609
Using solution proposed by @homm to make sure alpha channel is stripped from PNGs before conversion to JPGs.
@wiredfool wiredfool closed this Sep 9, 2017
jni added a commit to jni/scikit-image-feedstock that referenced this issue Oct 2, 2017
The novice module, removed in 0.14.x, runs into this error:
python-pillow/Pillow#2609
nfahlgren added a commit to danforthcenter/plantcv that referenced this issue Jul 19, 2018
* In matplotlib saving a histogram to a JPG file used to work
* Pillow deprecated the ability to save RGBA images as JPG since JPG doesn't support an alpha channel
* Now saving the histograms as PNG instead to bypass the issue
* Source python-pillow/Pillow#2609
karlcow added a commit to karlcow/webcompat.com that referenced this issue Aug 9, 2018
zoidbergwill added a commit to zoidbergwill/csgo-overviews that referenced this issue Oct 2, 2018
Partially related: python-pillow/Pillow#2609

I tried to refactor this, but can probably just simplify it a bunch
zoidbergwill added a commit to zoidbergwill/csgo-overviews that referenced this issue Oct 2, 2018
Partially related: python-pillow/Pillow#2609

I tried to refactor this, but can probably just simplify it a bunch
maltelau added a commit to maltelau/wallpaper-reddit that referenced this issue Jan 3, 2019
When png images have an alpha layer, they can retain them in RGBA mode.
This causes img.save(..., "JPEG") to fail with IOError("cannot write mode
RGBA as JPEG").
This commit layers RGBA images on top of a black background before
removing the alpha channel and converting to RGB.

Example image that used to fail:  https://i.redd.it/gnro5oykh1821.png
Issue in Pillow: python-pillow/Pillow#2609
StackOverflow solution: http://stackoverflow.com/a/9459208/284318
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants