Python PIL - all PNG areas with transparency> 0 have their opacity equal to 1

Imagine a red circle with a black shadow that fades over a fully transparent background. When I open and save the image using PIL, the background remains completely transparent, but the dropshadow turns black.

The problem appears even without changing the image:

image = Image.open('input.png')
image = image.convert('RGBA')
image.save('output.png')

I want the image to look exactly like the original so that I can crop or resize it.

EDIT: Here is a PNG demonstrating the effect. It was converted to 8 bits using PNGNQ.

alt text

Using the above Python code, it is output as follows:

alt text

+4
source share
2 answers

, PIL PNG8.

: http://mail.python.org/pipermail/image-sig/2010-October/006533.html

, PIL:

from PIL import Image, ImageFile, PngImagePlugin

def patched_chunk_tRNS(self, pos, len):
    i16 = PngImagePlugin.i16
    s = ImageFile._safe_read(self.fp, len)
    if self.im_mode == "P":
        self.im_info["transparency"] = map(ord, s)
    elif self.im_mode == "L":
        self.im_info["transparency"] = i16(s)
    elif self.im_mode == "RGB":
        self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:])
    return s
PngImagePlugin.PngStream.chunk_tRNS = patched_chunk_tRNS

def patched_load(self):
    if self.im and self.palette and self.palette.dirty:
        apply(self.im.putpalette, self.palette.getdata())
        self.palette.dirty = 0
        self.palette.rawmode = None
        try:
            trans = self.info["transparency"]
        except KeyError:
            self.palette.mode = "RGB"
        else:
            try:
                for i, a in enumerate(trans):
                    self.im.putpalettealpha(i, a)
            except TypeError:
                self.im.putpalettealpha(trans, 0)
            self.palette.mode = "RGBA"
    if self.im:
        return self.im.pixel_access(self.readonly)
Image.Image.load = patched_load

Image.open('kHrY6.png').convert('RGBA').save('kHrY6-out.png')
+6

, , -?

0

All Articles