Skip to content

P->RGBA->resize(LANCZOS)->P messes up transparency #2702

Closed

Description

@kkopachev

What did you do?

Given original image test.png
and following steps:

from PIL import Image
im = Image.open('test.png')
im.mode # 'P'
im = im.convert('RGBA')
im = im.resize((320,240), Image.LANCZOS)
im = im.convert('P')
im.save('broken.png')

What did you expect to happen?

good

What actually happened?

bad

What versions of Pillow and Python are you using?

Python 2.7.13 / Pillow 4.2.1

However, if I slightly change the script and save RGBA image after convertion and resize to a file and then reopen that file, quantize it to palette, then it's all good:

from PIL import Image
im = Image.open('test.png')
im = im.convert('RGBA')
im = im.resize((320,240), Image.LANCZOS)
im.save('intermidiary.png')
intermidiary = Image.open('intermidiary.png')
intermidiary.mode # 'RGBA'
intermidiary = intermidiary.convert('P')
intermidiary.save('good.png')

Activity

  1. wiredfool commented on Aug 31, 2017

    @wiredfool
    Member

    Reproduced here. Bug is that the palette is still hanging around in the RGBA image:

    (vpy27) erics@builder-16:~/test$ python -m pdb issue_2702.py
    > /home/erics/test/issue_2702.py(1)<module>()
    -> from PIL import Image
    (Pdb) n
    n
    > /home/erics/test/issue_2702.py(2)<module>()
    -> im = Image.open('issue_2702.png')
    (Pdb) n
    > /home/erics/test/issue_2702.py(3)<module>()
    -> print im.mode # 'P'
    (Pdb) n
    P
    > /home/erics/test/issue_2702.py(4)<module>()
    -> im = im.convert('RGBA')
    (Pdb) n
    > /home/erics/test/issue_2702.py(5)<module>()
    -> im = im.resize((320,240), Image.LANCZOS)
    (Pdb) n
    > /home/erics/test/issue_2702.py(6)<module>()
    -> im.show()
    [good image]
    (Pdb) im
    <PIL.Image.Image image mode=RGBA size=320x240 at 0x7F16BC7EAED0>
    (Pdb) im.palette
    <PIL.ImagePalette.ImagePalette object at 0x7f16bc7f7450>
    (Pdb) im.palette = None
    (Pdb) im.show()
    [good image]
    

    Workaround Code:

    from PIL import Image
    im = Image.open('issue_2702.png')
    print im.mode # 'P'
    im = im.convert('RGBA')
    im.palette = None
    im = im.resize((320,240), Image.LANCZOS)
    im = im.convert('P')
    im.save('issue_2702.out.png')
  2. added
    BugAny unexpected behavior, until confirmed feature.
    on Aug 31, 2017
  3. added this to the 4.3.0 milestone on Aug 31, 2017
  4. added a commit that references this issue on Aug 31, 2017
  5. onassar commented on Sep 23, 2017

    @onassar

    This fixes the issue for this image:

    But the issue remains for this PNG:

    Resizing results in something like this:

  6. kkopachev commented on Sep 25, 2017

    @kkopachev
    ContributorAuthor

    That's a different issue with another library.

  7. onassar commented on Sep 25, 2017

    @onassar

    How do you mean @kkopachev?
    I'm using Thumbor and this is happening. Does it not happen in PIL natively?

  8. kkopachev commented on Sep 25, 2017

    @kkopachev
    ContributorAuthor

    PIL is fine, it's issue in thumbor. I'll create PR there shortly.

  9. onassar commented on Sep 25, 2017

    @onassar

    Okay thanks. I'll work up a series of examples that are causing issues and post it there to ensure everything is captured.

  10. rogerchom commented on Jan 22, 2018

    @rogerchom

    Hey @kkopachev, have you created a PR to fix this yet? I saw thumbor/thumbor#989, but this addresses something different.

  11. kkopachev commented on Jan 22, 2018

    @kkopachev
    ContributorAuthor
  12. rogerchom commented on Jan 23, 2018

    @rogerchom

    @kkopachev Thanks!

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

    BugAny unexpected behavior, until confirmed feature.PNGPalette

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions