cannot write mode RGBA as JPEG (4.2.0) #2609
Description
Activity
JPEGs can't represent an alpha channel. It's been issuing a warning for a while, removed in this release.
Reacted by Oleg Serbokryl, Wayne Polatkan, Sungwon Jung, jtlz2, FooBar167, deep-programmer and Or ZilcaThanks @wiredfool ! What is preferred way to make it works with both PNG and JPEG files?
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
RGBimage and save as a JPEG.Reacted by Wayne Polatkan, Olaide Ojewale, jtlz2, LexRillo, FooBar167, Ed Summers, Alex Ellis and Francois Vieux@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") >>>
Reacted by Raymond, alcomic, Roman, Konstantin Sokolovskii, Finnian Anderson, Aart Goossens, Ashish Patil, Elodie, Evgeny Fadeev, ave and 88 moreReacted by Miguel Alberto Plazas, Aurélie A., Popa Marius Adrian, axm1 and Adam TealeReacted by Biranchi, TagMeTag, atw, Rémy HUBSCHER, jbustarviejo, DimentR, Pim van Hespen, Jaak Kütt, Michael Kook, Abbas and 9 moreReacted by Miguel Alberto Plazas, Aurélie A. and SwethaPooduruReacted by Miguel Alberto Plazas and axm1In 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)
Reacted by Cal Bryant, Steve Kossouho, tuchang, Raymond, Eduardo Henrique Vieira dos Santos, Sean Bendtsen Powell, bdpothik, Addison Schiller, Mark Feeney, Evgeny Fadeev and 57 moreReacted by nourhan-massoud and norohindReacted by Ignacio Iglesias, AlanJoygit, micsikz, Jean-Sébastien Gosselin, Pavel Martynov and norohind- added a commit that references this issue
on Jul 12, 2017 - added a commit that references this issue
on Jul 24, 2017 - added a commit that references this issue
on Oct 2, 2017 - added a commit that references this issue
on Jul 19, 2018 - added a commit that references this issue
on Aug 9, 2018 - added 2 commits that reference this issue
on Oct 2, 2018 - added a commit that references this issue
on Jan 3, 2019 - added a commit that references this issue
on Aug 11, 2021 - added a commit that references this issue
on Mar 13, 2022
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.