Python PIL - All areas of PNG with opacity > 0 hav

2020-07-17 05:24发布

问题:

Imagine a red circle with a black dropshadow that fades away on top of a fully transparent background. When I open and resave the image with PIL the background remains fully transparent but the dropshadow becomes full black.

The problem appears without even altering the image:

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

I want to keep the image looking exactly as the original so that I can crop or resize it.

EDIT: Here's a PNG that demonstrates the effect. It was converted to 8bit by using PNGNQ.

When using the above Python code it comes out as the following:

回答1:

It looks like PIL currently doesn't support full alpha for PNG8.

There is a patch here for read-only support: http://mail.python.org/pipermail/image-sig/2010-October/006533.html

If you're feeling naughty, you could monkeypatch 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')


回答2:

I think that the problem has been somewhat resolved, but is it possible that you need to set the depth of the alpha channel?