Skip to content

Commit 69ed0aa

Browse files
authored
Merge pull request #3728 from radarhere/pa_mode
Improvements to PA and LA conversion and palette operations
2 parents 2fdd3e1 + 54272c9 commit 69ed0aa

5 files changed

Lines changed: 173 additions & 38 deletions

File tree

Tests/test_image_convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def convert(im, mode):
1212
self.assertEqual(out.mode, mode)
1313
self.assertEqual(out.size, im.size)
1414

15-
modes = "1", "L", "I", "F", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"
15+
modes = ("1", "L", "LA", "P", "PA", "I", "F",
16+
"RGB", "RGBA", "RGBX", "CMYK", "YCbCr")
1617

1718
for mode in modes:
1819
im = hopper(mode)

Tests/test_image_putpalette.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ def palette(mode):
1414
return im.mode, p[:10]
1515
return im.mode
1616
self.assertRaises(ValueError, palette, "1")
17-
self.assertEqual(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
18-
self.assertEqual(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
17+
for mode in ["L", "LA", "P", "PA"]:
18+
self.assertEqual(palette(mode),
19+
("PA" if "A" in mode else "P",
20+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
1921
self.assertRaises(ValueError, palette, "I")
2022
self.assertRaises(ValueError, palette, "F")
2123
self.assertRaises(ValueError, palette, "RGB")

src/PIL/Image.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,10 +1619,10 @@ def putdata(self, data, scale=1.0, offset=0.0):
16191619

16201620
def putpalette(self, data, rawmode="RGB"):
16211621
"""
1622-
Attaches a palette to this image. The image must be a "P" or
1623-
"L" image, and the palette sequence must contain 768 integer
1624-
values, where each group of three values represent the red,
1625-
green, and blue values for the corresponding pixel
1622+
Attaches a palette to this image. The image must be a "P",
1623+
"PA", "L" or "LA" image, and the palette sequence must contain
1624+
768 integer values, where each group of three values represent
1625+
the red, green, and blue values for the corresponding pixel
16261626
index. Instead of an integer sequence, you can use an 8-bit
16271627
string.
16281628
@@ -1631,7 +1631,7 @@ def putpalette(self, data, rawmode="RGB"):
16311631
"""
16321632
from . import ImagePalette
16331633

1634-
if self.mode not in ("L", "P"):
1634+
if self.mode not in ("L", "LA", "P", "PA"):
16351635
raise ValueError("illegal image mode")
16361636
self.load()
16371637
if isinstance(data, ImagePalette.ImagePalette):
@@ -1643,7 +1643,7 @@ def putpalette(self, data, rawmode="RGB"):
16431643
else:
16441644
data = "".join(chr(x) for x in data)
16451645
palette = ImagePalette.raw(rawmode, data)
1646-
self.mode = "P"
1646+
self.mode = "PA" if "A" in self.mode else "P"
16471647
self.palette = palette
16481648
self.palette.mode = "RGB"
16491649
self.load() # install new palette

src/_imaging.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,8 @@ _putpalette(ImagingObject* self, PyObject* args)
15691569
if (!PyArg_ParseTuple(args, "s"PY_ARG_BYTES_LENGTH, &rawmode, &palette, &palettesize))
15701570
return NULL;
15711571

1572-
if (strcmp(self->image->mode, "L") != 0 && strcmp(self->image->mode, "P")) {
1572+
if (strcmp(self->image->mode, "L") && strcmp(self->image->mode, "LA") &&
1573+
strcmp(self->image->mode, "P") && strcmp(self->image->mode, "PA")) {
15731574
PyErr_SetString(PyExc_ValueError, wrong_mode);
15741575
return NULL;
15751576
}

0 commit comments

Comments
 (0)