Fredrik Lundh is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

effbot / pil-2009-raclette http://effbot.org/zone/pil-index.htm

Work repository for PIL 1.1.7 and beyond. The "default" branch should be fairly solid, the other branches less so. For production use, see the pil-117 repository.

Clone this repository (size: 1.6 MB): HTTPS / SSH
hg clone https://bitbucket.org/effbot/pil-2009-raclette
hg clone ssh://hg@bitbucket.org/effbot/pil-2009-raclette

Issues

#8 corrupting images in palette mode

Reported anonymously

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:

 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.

#!/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.

 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

Status: new Responsible: nobody Type: bug Priority: major
Milestone: none Component: none Version: none

Attachments

No attachments added for this issue yet.

Comments and changes

  1. #1 Anonymous

    written

    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. #2 Anonymous

    written

    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. #3 Anonymous

    written

    Hi, this problem is solved in current development version.

Add comment / attachment

Verification: Please write the text from the image in the box (letters only)

captcha

Is that you, Humanoid? Is this me?