From 804183c2489242251fdd9858b6e781ceefb08c8b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 27 Jun 2021 18:29:02 +1000 Subject: [PATCH] Only use an RGBA palette for images with an alpha channel --- Tests/test_file_apng.py | 4 ++-- Tests/test_image_convert.py | 2 +- src/PIL/Image.py | 2 +- src/PIL/WebPImagePlugin.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index 8348da4eb..7fb6f59d4 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -249,8 +249,8 @@ def test_apng_mode(): assert im.mode == "P" im.seek(im.n_frames - 1) im = im.convert("RGBA") - assert im.getpixel((0, 0)) == (0, 255, 0, 255) - assert im.getpixel((64, 32)) == (0, 255, 0, 255) + assert im.getpixel((0, 0)) == (255, 0, 0, 0) + assert im.getpixel((64, 32)) == (255, 0, 0, 0) with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im: assert im.mode == "P" diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 01bcbb95f..20ef3a57f 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -167,7 +167,7 @@ def test_gif_with_rgba_palette_to_p(): with Image.open("Tests/images/hopper.gif") as im: im.info["transparency"] = 255 im.load() - assert im.palette.mode == "RGBA" + assert im.palette.mode == "RGB" im_p = im.convert("P") # Should not raise ValueError: unrecognized raw mode diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 5c63c91b5..76814a8cc 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -833,7 +833,7 @@ class Image: palette_length = self.im.putpalette(mode, arr) self.palette.dirty = 0 self.palette.rawmode = None - if "transparency" in self.info: + if "transparency" in self.info and mode in ("RGBA", "LA", "PA"): if isinstance(self.info["transparency"], int): self.im.putpalettealpha(self.info["transparency"], 0) else: diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 50b82feb0..b63a07ca8 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -320,7 +320,7 @@ def _save(im, fp, filename): alpha = ( "A" in im.mode or "a" in im.mode - or (im.mode == "P" and "A" in im.im.getpalettemode()) + or (im.mode == "P" and "transparency" in im.info) ) im = im.convert("RGBA" if alpha else "RGB")