The PNG loader crashes trying to save palette images with tRNS color type 3 (indexed color) value after convert to RGB #510

Closed
kwist-sgr opened this Issue Jan 29, 2014 · 7 comments

Projects

None yet

3 participants

@kwist-sgr

defaktologo_bad

ValueError: too many values to unpack

valueerror

4.2.1.1. tRNS Transparency (PNG Specification)
http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.tRNS

@wiredfool
Member

Can you post the code that you're using to drive this? It looks like your exception is in save, not open. Either way, it opens and saves ok for me.

>>> from PIL import Image
>>> im = Image.open('e2a6f2ea-88f3-11e3-9d40-b86f41857491.png')
>>> im.save('temp.png')
>>> 
@kwist-sgr

Yes, I'm sorry, I made a mistake in the description.

Sample code
https://gist.github.com/kwist-sgr/403bdf6ad1f9cdb1961d

@wiredfool
Member

Ah, so you're converting to an RGB: image = image.convert('RGB') which makes the tRNS chunk incorrect.

Unfortunately, there's less information in the tRNS chunk in rgb mode -- in P mode it's a vector of alpha values, in RGB it's a single color, so that conversion (if done) would be a lossy conversion.

For this image, since there's no transparent pixels, you can get it to save by setting im.info['transparency'] to none after the conversion.

In general though, this is a bug in Image.py:convert() where mode P with transparency images are promoted to RGB. There's also going to be a similar issue with mode L images, and a different, but related issue promoting P to RGBA.

  • There's no conversion of info['transparency'] when converting between L, RGB, and P
  • Transparency is handled for the promotion of L and RGB to RGBA, but not for P
@kwist-sgr

Thanks, helped.

@wiredfool
Member

P-> RGBA is handled correctly in the guts of the ordinary p conversions, as (somewhere) the palette that's in ImagePalette is converted into an RGBA palette and from there, is all well and good.

So, there's a need for conversion of the transparency between P/RGB/L, but not RGBA. I'm not sure if there's currently an easy way to get the color for a palette entry. There probably should be. Additionally, I've seen at least a couple of different organizations of the palette data in ImagePalette.py.

@aclark4life aclark4life added this to the 2.4.0 milestone Mar 17, 2014
@aclark4life
Member

Can we get a fix for this in 2.4.0?

@wiredfool
Member

Yeah, I think it can happen.

@wiredfool wiredfool self-assigned this Mar 20, 2014
@wiredfool wiredfool added a commit to wiredfool/Pillow that referenced this issue Mar 26, 2014
@wiredfool wiredfool Tests for #510 89c24e9
@wiredfool wiredfool added a commit to wiredfool/Pillow that referenced this issue Mar 26, 2014
@wiredfool wiredfool Transparency conversion for L, RGP and P images, fixes #510 8ea903e
@wiredfool wiredfool closed this in #574 Apr 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment