Promote P images to PA in putalpha

This commit is contained in:
Andrew Murray 2019-03-19 11:13:58 +11:00
parent 63141a9c0c
commit f61828acdc
4 changed files with 14 additions and 5 deletions

View File

@ -42,7 +42,7 @@ class TestImageMode(PillowTestCase):
self.assertEqual(signature, result)
check("1", "L", "L", 1, ("1",))
check("L", "L", "L", 1, ("L",))
check("P", "RGB", "L", 1, ("P",))
check("P", "P", "L", 1, ("P",))
check("I", "L", "I", 1, ("I",))
check("F", "L", "F", 1, ("F",))
check("RGB", "RGB", "L", 3, ("R", "G", "B"))

View File

@ -28,6 +28,13 @@ class TestImagePutAlpha(PillowTestCase):
self.assertEqual(im.mode, 'LA')
self.assertEqual(im.getpixel((0, 0)), (1, 2))
im = Image.new("P", (1, 1), 1)
self.assertEqual(im.getpixel((0, 0)), 1)
im.putalpha(2)
self.assertEqual(im.mode, 'PA')
self.assertEqual(im.getpixel((0, 0)), (1, 2))
im = Image.new("RGB", (1, 1), (1, 2, 3))
self.assertEqual(im.getpixel((0, 0)), (1, 2, 3))

View File

@ -247,7 +247,7 @@ _MODEINFO = {
"L": ("L", "L", ("L",)),
"I": ("L", "I", ("I",)),
"F": ("L", "F", ("F",)),
"P": ("RGB", "L", ("P",)),
"P": ("P", "L", ("P",)),
"RGB": ("RGB", "L", ("R", "G", "B")),
"RGBX": ("RGB", "L", ("R", "G", "B", "X")),
"RGBA": ("RGB", "L", ("R", "G", "B", "A")),
@ -1559,7 +1559,7 @@ class Image(object):
self._ensure_mutable()
if self.mode not in ("LA", "RGBA"):
if self.mode not in ("LA", "PA", "RGBA"):
# attempt to promote self to a matching alpha mode
try:
mode = getmodebase(self.mode) + "A"
@ -1568,7 +1568,7 @@ class Image(object):
except (AttributeError, ValueError):
# do things the hard way
im = self.im.convert(mode)
if im.mode not in ("LA", "RGBA"):
if im.mode not in ("LA", "PA", "RGBA"):
raise ValueError # sanity check
self.im = im
self.pyaccess = None
@ -1576,7 +1576,7 @@ class Image(object):
except (KeyError, ValueError):
raise ValueError("illegal image mode")
if self.mode == "LA":
if self.mode in ("LA", "PA"):
band = 1
else:
band = 3

View File

@ -1018,6 +1018,8 @@ frompalette(Imaging imOut, Imaging imIn, const char *mode)
convert = p2l;
else if (strcmp(mode, "LA") == 0)
convert = (alpha) ? pa2la : p2la;
else if (strcmp(mode, "PA") == 0)
convert = l2la;
else if (strcmp(mode, "I") == 0)
convert = p2i;
else if (strcmp(mode, "F") == 0)