1. Fredrik Lundh
  2. pil-2009-raclette
  3. Issues
Issue #8 new

corrupting images in palette mode

Anonymous created an issue

Opening image in palette mode is corrupting transparency information.

Example:

I have PNG image in palette mode, with 256 color palette. Every color has different level of transparency. For example:

{{{

!python

Color Table (RGB with 256 entries) 0: 0,0,0,0 1: 238,238,238,154 2: 253,253,109,154 3: 0,0,0,2 4: 0,0,0,4 5: 0,0,0,8 6: 0,0,0,6 7: 237,237,237,154 8: 0,0,0,14 9: 35,35,15,153 10: 1,1,0,138 }}}

I am trying just to open and save this image using PIL. {{{

!python

!/usr/bin/python

import Image

im = Image.open('test.png') im.save('out.png', transparency=0 ) }}}

After running this code, the resulting image is loosing transparency information for each color and it is set to 255. {{{

!python

Color Table (RGB with 256 entries) 0: 0,0,0,0 1: 238,238,238,255 2: 253,253,109,255 3: 0,0,0,255 4: 0,0,0,255 5: 0,0,0,255 6: 0,0,0,255 7: 237,237,237,255 8: 0,0,0,255 9: 35,35,15,255 10: 1,1,0,255 }}}

You can download my test image here: http://gista.sk/dl/test.png

Ivan

Comments (3)

  1. Anonymous

    Here is the patch which allows correct processing of palette image with transparency value on each color. Patch author is Peter Hyben, Gista s.r.o. Would You please look at it ?

    246,248c246,252
    <             i = string.find(s, chr(0))
    <             if i >= 0:
    <                 self.im_info["transparency"] = i
    ---
    >             palette_transparency = map(ord, s)
    >             if self.__is_palette_transparency(palette_transparency):
    >                 self.im_info["palette_transparency"] = palette_transparency
    >             else:
    >                 i = string.find(s, chr(0))
    >                 if i >= 0:
    >                     self.im_info["transparency"] = i
    254a259,264
    >     def __is_palette_transparency(self, pt):
    >         for item in pt:
    >             if item not in [0, 255]:
    >                 return True
    >         return False
    >
    538a549,554
    >     if im.encoderinfo.has_key("palette_transparency"):
    >         if im.mode == "P":
    >             chunk(fp, "tRNS", "".join([chr(max(0, min(255, value))) for value in im.encoderinfo["palette_transparency"][:256]]))
    >         else:
    >             raise IOError("cannot use palette transparency for this mode")
    

    Ivan

  2. Anonymous

    Here is the patch which will allow correct processing palette PNG with transparency value on each color. Would You please look at it ? Patch author is Peter Hyben, Gista s.r.o.

    246,248c246,252
    <             i = string.find(s, chr(0))
    <             if i >= 0:
    <                 self.im_info["transparency"] = i
    ---
    >             palette_transparency = map(ord, s)
    >             if self.__is_palette_transparency(palette_transparency):
    >                 self.im_info["palette_transparency"] = palette_transparency
    >             else:
    >                 i = string.find(s, chr(0))
    >                 if i >= 0:
    >                     self.im_info["transparency"] = i
    254a259,264
    >     def __is_palette_transparency(self, pt):
    >         for item in pt:
    >             if item not in [0, 255]:
    >                 return True
    >         return False
    >
    538a549,554
    >     if im.encoderinfo.has_key("palette_transparency"):
    >         if im.mode == "P":
    >             chunk(fp, "tRNS", "".join([chr(max(0, min(255, value))) for value in im.encoderinfo["palette_transparency"][:256]]))
    >         else:
    >             raise IOError("cannot use palette transparency for this mode")
    

    Ivan

  3. Log in to comment