From 0966293a0df0e45a2d4924542e180e917098c2fc Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 16 Mar 2023 20:05:56 +1100 Subject: [PATCH] Consider transparency when applying blend mask --- Tests/images/blend_transparency.png | Bin 0 -> 211 bytes Tests/test_file_apng.py | 6 ++++++ src/PIL/PngImagePlugin.py | 10 +++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 Tests/images/blend_transparency.png diff --git a/Tests/images/blend_transparency.png b/Tests/images/blend_transparency.png new file mode 100644 index 0000000000000000000000000000000000000000..cef0a16de1f2db2db74e87d8bed5c8ba32038f28 GIT binary patch literal 211 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1SBVv2j2ryY$ZW{!9e;y1H%P7;~F4~BQZI| z2S|eyF?{ZwTmYn`(ja0WA+Ql31_Us_U|?Kvy6GB_$K&bZ7*a7O`N#PK3=GVSj0`9L zO?nR$hv|mt1kq3c(rvsW17w##T8d)`kY)m!knrO?kio>rAZ@=c0i@K^)z4*}Q$iB} Do3|(? literal 0 HcmV?d00001 diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index b2bec5984..feca72aa6 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -163,6 +163,12 @@ def test_apng_blend(): assert im.getpixel((64, 32)) == (0, 255, 0, 255) +def test_apng_blend_transparency(): + with Image.open("Tests/images/blend_transparency.png") as im: + im.seek(1) + assert im.getpixel((0, 0)) == (255, 0, 0) + + def test_apng_chunk_order(): with Image.open("Tests/images/apng/fctl_actl.png") as im: im.seek(im.n_frames - 1) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 9078957dc..15a3c8291 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1003,9 +1003,13 @@ class PngImageFile(ImageFile.ImageFile): else: if self._prev_im and self.blend_op == Blend.OP_OVER: updated = self._crop(self.im, self.dispose_extent) - self._prev_im.paste( - updated, self.dispose_extent, updated.convert("RGBA") - ) + if self.im.mode == "RGB" and "transparency" in self.info: + mask = updated.convert_transparent( + "RGBA", self.info["transparency"] + ) + else: + mask = updated.convert("RGBA") + self._prev_im.paste(updated, self.dispose_extent, mask) self.im = self._prev_im if self.pyaccess: self.pyaccess = None