diff --git a/PIL/Image.py b/PIL/Image.py index b2b5d2493..8ca8fa6cf 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -614,7 +614,11 @@ class Image: self.palette.mode = "RGB" self.palette.rawmode = None if "transparency" in self.info: - self.im.putpalettealpha(self.info["transparency"], 0) + if self.info["transparency_palette"]: + self.im.putpalettealpha(0, 0, self.info["transparency_palette"]) + else: + self.im.putpalettealpha(self.info["transparency"], 0) + self.palette.mode = "RGBA" if self.im: return self.im.pixel_access(self.readonly) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 12d9fe018..887ca02d1 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -254,6 +254,7 @@ class PngStream(ChunkStream): i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i + self.im_info["transparency_palette"] = s elif self.im_mode == "L": self.im_info["transparency"] = i16(s) elif self.im_mode == "RGB": diff --git a/_imaging.c b/_imaging.c index 986f221e4..8adbe3b16 100644 --- a/_imaging.c +++ b/_imaging.c @@ -733,13 +733,13 @@ _convert(ImagingObject* self, PyObject* args) return NULL; if (paletteimage != NULL) { if (!PyImaging_Check(paletteimage)) { - PyObject_Print((PyObject *)paletteimage, stderr, 0); - PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'"); - return NULL; + PyObject_Print((PyObject *)paletteimage, stderr, 0); + PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'"); + return NULL; } if (paletteimage->image->palette == NULL) { - PyErr_SetString(PyExc_ValueError, "null palette"); - return NULL; + PyErr_SetString(PyExc_ValueError, "null palette"); + return NULL; } } @@ -1407,7 +1407,9 @@ _putpalettealpha(ImagingObject* self, PyObject* args) { int index; int alpha = 0; - if (!PyArg_ParseTuple(args, "i|i", &index, &alpha)) + char* tpalette = NULL; + int tpaletteSize = 0; + if (!PyArg_ParseTuple(args, "i|i"PY_ARG_BYTES_LENGTH, &index, &alpha, &tpalette, &tpaletteSize)) return NULL; if (!self->image->palette) { @@ -1419,9 +1421,17 @@ _putpalettealpha(ImagingObject* self, PyObject* args) PyErr_SetString(PyExc_ValueError, outside_palette); return NULL; } - + strcpy(self->image->palette->mode, "RGBA"); - self->image->palette->palette[index*4+3] = (UINT8) alpha; + + if (tpaletteSize > 0) { + for (index = 0; index < tpaletteSize; index++) { + self->image->palette->palette[index*4+3] = (UINT8) tpalette[index]; + } + } + else { + self->image->palette->palette[index*4+3] = (UINT8) alpha; + } Py_INCREF(Py_None); return Py_None; @@ -3391,3 +3401,4 @@ init_imaging(void) } #endif + diff --git a/selftest.py b/selftest.py index b6d7a300c..1d490f197 100644 --- a/selftest.py +++ b/selftest.py @@ -157,7 +157,7 @@ def testimage(): def check_module(feature, module): try: - __import__("PIL." + module) + __import__(module) except ImportError: print("***", feature, "support not installed") else: