Skip to content

Commit 5baa1ac

Browse files
committed
Added support for PNG images with transparency palette
1 parent f3d601b commit 5baa1ac

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

PIL/Image.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,11 @@ def load(self):
614614
self.palette.mode = "RGB"
615615
self.palette.rawmode = None
616616
if "transparency" in self.info:
617-
self.im.putpalettealpha(self.info["transparency"], 0)
617+
if self.info["transparency_palette"]:
618+
self.im.putpalettealpha(0, 0, self.info["transparency_palette"])
619+
else:
620+
self.im.putpalettealpha(self.info["transparency"], 0)
621+
618622
self.palette.mode = "RGBA"
619623
if self.im:
620624
return self.im.pixel_access(self.readonly)

PIL/PngImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def chunk_tRNS(self, pos, len):
254254
i = s.find(b"\0")
255255
if i >= 0:
256256
self.im_info["transparency"] = i
257+
self.im_info["transparency_palette"] = s
257258
elif self.im_mode == "L":
258259
self.im_info["transparency"] = i16(s)
259260
elif self.im_mode == "RGB":

_imaging.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,13 @@ _convert(ImagingObject* self, PyObject* args)
733733
return NULL;
734734
if (paletteimage != NULL) {
735735
if (!PyImaging_Check(paletteimage)) {
736-
PyObject_Print((PyObject *)paletteimage, stderr, 0);
737-
PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'");
738-
return NULL;
736+
PyObject_Print((PyObject *)paletteimage, stderr, 0);
737+
PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'");
738+
return NULL;
739739
}
740740
if (paletteimage->image->palette == NULL) {
741-
PyErr_SetString(PyExc_ValueError, "null palette");
742-
return NULL;
741+
PyErr_SetString(PyExc_ValueError, "null palette");
742+
return NULL;
743743
}
744744
}
745745

@@ -1407,7 +1407,9 @@ _putpalettealpha(ImagingObject* self, PyObject* args)
14071407
{
14081408
int index;
14091409
int alpha = 0;
1410-
if (!PyArg_ParseTuple(args, "i|i", &index, &alpha))
1410+
char* tpalette = NULL;
1411+
int tpaletteSize = 0;
1412+
if (!PyArg_ParseTuple(args, "i|i"PY_ARG_BYTES_LENGTH, &index, &alpha, &tpalette, &tpaletteSize))
14111413
return NULL;
14121414

14131415
if (!self->image->palette) {
@@ -1419,9 +1421,17 @@ _putpalettealpha(ImagingObject* self, PyObject* args)
14191421
PyErr_SetString(PyExc_ValueError, outside_palette);
14201422
return NULL;
14211423
}
1422-
1424+
14231425
strcpy(self->image->palette->mode, "RGBA");
1424-
self->image->palette->palette[index*4+3] = (UINT8) alpha;
1426+
1427+
if (tpaletteSize > 0) {
1428+
for (index = 0; index < tpaletteSize; index++) {
1429+
self->image->palette->palette[index*4+3] = (UINT8) tpalette[index];
1430+
}
1431+
}
1432+
else {
1433+
self->image->palette->palette[index*4+3] = (UINT8) alpha;
1434+
}
14251435

14261436
Py_INCREF(Py_None);
14271437
return Py_None;
@@ -3391,3 +3401,4 @@ init_imaging(void)
33913401
}
33923402
#endif
33933403

3404+

selftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def testimage():
157157

158158
def check_module(feature, module):
159159
try:
160-
__import__("PIL." + module)
160+
__import__(module)
161161
except ImportError:
162162
print("***", feature, "support not installed")
163163
else:

0 commit comments

Comments
 (0)