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

8.3.0: convert on a P image with transparency now returns a RGB image (used to be RGBA) #5593

Closed
benjaminrigaud opened this issue Jul 8, 2021 · 3 comments · Fixed by #5606

Comments

@benjaminrigaud
Copy link

What did you do?

I converted an image in P mode with transparency on the new 8.3.0 release.

What did you expect to happen?

I expected the output to be RGBA.

What actually happened?

it is now in RGB.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 18.04
  • Python: 3.7
  • Pillow: 8.3.0 or 8.3.1

Please find the code and image to reproduce the issue on https://gist.github.com/benjaminrigaud/d98a4b54fb409bc8f745d169df0196fc

def test_convert_p():
    with open("indexed_with_transparency.png", "rb") as f:
        data = BytesIO(f.read())

    img = Image.open(data)

    assert img.mode == "P"
    assert img.palette.mode == "RGB"
    assert img.info.get("transparency") == 0

    img2 = img.convert()

    assert img2.mode == "RGBA"
@radarhere
Copy link
Member

This would be due to 804183c. It changed palettes for P images with info["transparency"] to RGB instead of RGBA. Separate to that, converting from a P image with no mode argument uses the palette mode if there is one.

I've created PR #5594 to fix this.

@radarhere radarhere changed the title 8.3.0: convert on a P image with transparency now returns a RGB image (use to be RGBA) 8.3.0: convert on a P image with transparency now returns a RGB image (used to be RGBA) Jul 8, 2021
@NicolasHug
Copy link

I am facing a similar issue when converting a P image into LA: values of the transparency channel are all 255 with pillow>=8.3:

from PIL import Image
import numpy as np

# same image that OP mentioned https://gist.github.com/benjaminrigaud/d98a4b54fb409bc8f745d169df0196fc
path = the_path_here

with Image.open(path) as img:
    img = img.convert("LA")
np.unique(np.asarray(img)[:, :, -1])

This will print array([255], dtype=uint8) on 8.3.1 and array([ 0, 255], dtype=uint8) on 8.2

Using another P image like https://github.com/pytorch/vision/blob/master/test/assets/fakedata/logos/palette_pytorch.png, the results are even more drastic:

array([255], dtype=uint8) vs array([ 0, 16, 64, 80, 96, 128, 160, 208, 224, 240, 255], dtype=uint8)

@radarhere
Copy link
Member

I've created #5606 to fix this second problem. I validated the result just by visually inspecting the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants