Skip to content

cannot write mode RGBA as JPEG (4.2.0) #2609

Description

@karolpawlowski

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)

Activity

  1. wiredfool commented on Jul 4, 2017

    @wiredfool
    Member

    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.

  2. karolpawlowski commented on Jul 4, 2017

    @karolpawlowski
    Author

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

  3. wiredfool commented on Jul 4, 2017

    @wiredfool
    Member

    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.

  4. radarhere commented on Jul 8, 2017

    @radarhere
    Member

    @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")
    >>> 
  5. homm commented on Jul 9, 2017

    @homm
    Member

    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)
  6. added a commit that references this issue on Jul 11, 2017
  7. added a commit that references this issue on Oct 2, 2017
  8. added a commit that references this issue on Aug 9, 2018
  9. added a commit that references this issue on Feb 20, 2022
  10. added a commit that references this issue on Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions