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

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


None yet

3 participants



ValueError: too many values to unpack

valueerror tRNS Transparency (PNG Specification)


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 ='e2a6f2ea-88f3-11e3-9d40-b86f41857491.png')

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

Sample code


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['transparency'] to none after the conversion.

In general though, this is a bug in 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

Thanks, helped.


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

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

Can we get a fix for this in 2.4.0?


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