From dba38912fb1217bad9610f842c9ff5194f64ef53 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 20 Mar 2019 08:13:56 +1100 Subject: [PATCH] Allow putpalette for LA and PA --- Tests/test_image_putpalette.py | 6 ++++-- src/PIL/Image.py | 12 ++++++------ src/_imaging.c | 3 ++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Tests/test_image_putpalette.py b/Tests/test_image_putpalette.py index 34b585f55..4bac448fd 100644 --- a/Tests/test_image_putpalette.py +++ b/Tests/test_image_putpalette.py @@ -14,8 +14,10 @@ class TestImagePutPalette(PillowTestCase): return im.mode, p[:10] return im.mode self.assertRaises(ValueError, palette, "1") - self.assertEqual(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) - self.assertEqual(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + for mode in ["L", "LA", "P", "PA"]: + self.assertEqual(palette(mode), + ("PA" if "A" in mode else "P", + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) self.assertRaises(ValueError, palette, "I") self.assertRaises(ValueError, palette, "F") self.assertRaises(ValueError, palette, "RGB") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index ed3719100..dfa526e7a 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1619,10 +1619,10 @@ class Image(object): def putpalette(self, data, rawmode="RGB"): """ - Attaches a palette to this image. The image must be a "P" or - "L" image, and the palette sequence must contain 768 integer - values, where each group of three values represent the red, - green, and blue values for the corresponding pixel + Attaches a palette to this image. The image must be a "P", + "PA", "L" or "LA" image, and the palette sequence must contain + 768 integer values, where each group of three values represent + the red, green, and blue values for the corresponding pixel index. Instead of an integer sequence, you can use an 8-bit string. @@ -1631,7 +1631,7 @@ class Image(object): """ from . import ImagePalette - if self.mode not in ("L", "P"): + if self.mode not in ("L", "LA", "P", "PA"): raise ValueError("illegal image mode") self.load() if isinstance(data, ImagePalette.ImagePalette): @@ -1643,7 +1643,7 @@ class Image(object): else: data = "".join(chr(x) for x in data) palette = ImagePalette.raw(rawmode, data) - self.mode = "P" + self.mode = "PA" if "A" in self.mode else "P" self.palette = palette self.palette.mode = "RGB" self.load() # install new palette diff --git a/src/_imaging.c b/src/_imaging.c index 614f809bc..037348baa 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1569,7 +1569,8 @@ _putpalette(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "s"PY_ARG_BYTES_LENGTH, &rawmode, &palette, &palettesize)) return NULL; - if (strcmp(self->image->mode, "L") != 0 && strcmp(self->image->mode, "P")) { + if (strcmp(self->image->mode, "L") && strcmp(self->image->mode, "LA") && + strcmp(self->image->mode, "P") && strcmp(self->image->mode, "PA")) { PyErr_SetString(PyExc_ValueError, wrong_mode); return NULL; }