From b77cd009e210cefae9424206f80ed5dc65b53fdf Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 6 Oct 2024 11:30:27 +1100 Subject: [PATCH] Use transparency when combining P frames --- Tests/test_file_apng.py | 4 ++-- src/PIL/PngImagePlugin.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index e95850212..ee6c867c3 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -258,8 +258,8 @@ def test_apng_mode() -> None: assert im.mode == "P" im.seek(im.n_frames - 1) im = im.convert("RGBA") - assert im.getpixel((0, 0)) == (255, 0, 0, 0) - assert im.getpixel((64, 32)) == (255, 0, 0, 0) + assert im.getpixel((0, 0)) == (0, 255, 0, 255) + assert im.getpixel((64, 32)) == (0, 255, 0, 255) with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im: assert im.mode == "P" diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 28ade293e..4e1227204 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1063,6 +1063,12 @@ class PngImageFile(ImageFile.ImageFile): "RGBA", self.info["transparency"] ) else: + if self.im.mode == "P" and "transparency" in self.info: + t = self.info["transparency"] + if isinstance(t, bytes): + updated.putpalettealphas(t) + elif isinstance(t, int): + updated.putpalettealpha(t) mask = updated.convert("RGBA") self._prev_im.paste(updated, self.dispose_extent, mask) self.im = self._prev_im