From 7a9147dfacdc066fee4d50e6d00168ab19366033 Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:32:06 +0530 Subject: [PATCH 1/2] Alpha composite APNG frames with OP_OVER blend for RGBA and LA Frames using APNG_BLEND_OP_OVER were applied with Image.paste() and the frame's own alpha as mask, which also interpolates the alpha channel. Drawing a partially transparent pixel over an opaque canvas therefore made the canvas partially transparent (alpha 128 over alpha 255 gave 191). Use alpha compositing for RGBA and LA images instead. --- Tests/test_file_apng.py | 40 ++++++++++++++++++++++++++++++++++----- src/PIL/PngImagePlugin.py | 13 +++++++++++-- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index e9fb41d03ae..f12009e0de5 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -252,10 +252,40 @@ def test_apng_blend() -> None: with Image.open("Tests/images/apng/blend_op_over_near_transparent.png") as im: assert isinstance(im, PngImagePlugin.PngImageFile) im.seek(im.n_frames - 1) - assert im.getpixel((0, 0)) == (0, 255, 0, 97) + assert im.getpixel((0, 0)) == (0, 255, 0, 255) assert im.getpixel((64, 32)) == (0, 255, 0, 255) +@pytest.mark.parametrize("mode", ("LA", "RGBA")) +def test_apng_blend_over_alpha(mode: str, tmp_path: Path) -> None: + test_file = tmp_path / "temp.png" + data: list[tuple[int, ...]] + frame_data: list[tuple[int, ...]] + expected: list[tuple[int, ...]] + if mode == "LA": + data = [(200, 255), (0, 0)] + frame_data = [(100, 128), (100, 128)] + expected = [(150, 255), (100, 128)] + else: + data = [(255, 0, 0, 255), (0, 0, 0, 0)] + frame_data = [(0, 0, 255, 128), (0, 0, 255, 128)] + expected = [(127, 0, 128, 255), (0, 0, 255, 128)] + im = Image.new(mode, (2, 1)) + im.putdata(data) + frame = Image.new(mode, (2, 1)) + frame.putdata(frame_data) + im.save( + test_file, + save_all=True, + append_images=[frame], + blend=[PngImagePlugin.Blend.OP_SOURCE, PngImagePlugin.Blend.OP_OVER], + ) + + with Image.open(test_file) as reloaded: + reloaded.seek(1) + assert [reloaded.getpixel((x, 0)) for x in range(2)] == expected + + def test_apng_blend_transparency() -> None: with Image.open("Tests/images/blend_transparency.png") as im: im.seek(1) @@ -323,8 +353,8 @@ def test_apng_mode() -> None: assert isinstance(im, PngImagePlugin.PngImageFile) assert im.mode == "RGBA" im.seek(im.n_frames - 1) - assert im.getpixel((0, 0)) == (0, 0, 128, 191) - assert im.getpixel((64, 32)) == (0, 0, 128, 191) + assert im.getpixel((0, 0)) == (0, 0, 128, 255) + assert im.getpixel((64, 32)) == (0, 0, 128, 255) with Image.open("Tests/images/apng/mode_grayscale.png") as im: assert isinstance(im, PngImagePlugin.PngImageFile) @@ -337,8 +367,8 @@ def test_apng_mode() -> None: assert isinstance(im, PngImagePlugin.PngImageFile) assert im.mode == "LA" im.seek(im.n_frames - 1) - assert im.getpixel((0, 0)) == (128, 191) - assert im.getpixel((64, 32)) == (128, 191) + assert im.getpixel((0, 0)) == (128, 255) + assert im.getpixel((64, 32)) == (128, 255) with Image.open("Tests/images/apng/mode_palette.png") as im: assert isinstance(im, PngImagePlugin.PngImageFile) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index cdb21a34563..93e221fe527 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1113,7 +1113,13 @@ def load_end(self) -> None: else: if self._prev_im and self.blend_op == Blend.OP_OVER: updated = self._crop(self.im, self.dispose_extent) - if self.im.mode == "RGB" and "transparency" in self.info: + mask = None + if self.im.mode in ("LA", "RGBA"): + # Alpha composite, so that the alpha channel is blended too + updated = Image.core.alpha_composite( + self._crop(self._prev_im, self.dispose_extent), updated + ) + elif self.im.mode == "RGB" and "transparency" in self.info: mask = updated.convert_transparent( "RGBA", self.info["transparency"] ) @@ -1130,7 +1136,10 @@ def load_end(self) -> None: self.im = self._prev_im.copy() else: self.im = self._prev_im - self.im.paste(updated, self.dispose_extent, mask) + if mask is None: + self.im.paste(updated, self.dispose_extent) + else: + self.im.paste(updated, self.dispose_extent, mask) def _getexif(self) -> dict[int, Any] | None: if "exif" not in self.info: From 77875f29e2ba8e3d269011c2801387ab54aabd85 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 22 Nov 2026 23:09:47 +1100 Subject: [PATCH 2/2] Simplify code --- Tests/test_file_apng.py | 30 ------------------------------ src/PIL/PngImagePlugin.py | 11 +++++------ 2 files changed, 5 insertions(+), 36 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index f12009e0de5..381d799e776 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -256,36 +256,6 @@ def test_apng_blend() -> None: assert im.getpixel((64, 32)) == (0, 255, 0, 255) -@pytest.mark.parametrize("mode", ("LA", "RGBA")) -def test_apng_blend_over_alpha(mode: str, tmp_path: Path) -> None: - test_file = tmp_path / "temp.png" - data: list[tuple[int, ...]] - frame_data: list[tuple[int, ...]] - expected: list[tuple[int, ...]] - if mode == "LA": - data = [(200, 255), (0, 0)] - frame_data = [(100, 128), (100, 128)] - expected = [(150, 255), (100, 128)] - else: - data = [(255, 0, 0, 255), (0, 0, 0, 0)] - frame_data = [(0, 0, 255, 128), (0, 0, 255, 128)] - expected = [(127, 0, 128, 255), (0, 0, 255, 128)] - im = Image.new(mode, (2, 1)) - im.putdata(data) - frame = Image.new(mode, (2, 1)) - frame.putdata(frame_data) - im.save( - test_file, - save_all=True, - append_images=[frame], - blend=[PngImagePlugin.Blend.OP_SOURCE, PngImagePlugin.Blend.OP_OVER], - ) - - with Image.open(test_file) as reloaded: - reloaded.seek(1) - assert [reloaded.getpixel((x, 0)) for x in range(2)] == expected - - def test_apng_blend_transparency() -> None: with Image.open("Tests/images/blend_transparency.png") as im: im.seek(1) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 93e221fe527..81919314f6d 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1113,12 +1113,11 @@ def load_end(self) -> None: else: if self._prev_im and self.blend_op == Blend.OP_OVER: updated = self._crop(self.im, self.dispose_extent) - mask = None - if self.im.mode in ("LA", "RGBA"): - # Alpha composite, so that the alpha channel is blended too + if self.im.mode in {"LA", "RGBA"}: updated = Image.core.alpha_composite( self._crop(self._prev_im, self.dispose_extent), updated ) + mask = None elif self.im.mode == "RGB" and "transparency" in self.info: mask = updated.convert_transparent( "RGBA", self.info["transparency"] @@ -1136,10 +1135,10 @@ def load_end(self) -> None: self.im = self._prev_im.copy() else: self.im = self._prev_im - if mask is None: - self.im.paste(updated, self.dispose_extent) - else: + if mask is not None: self.im.paste(updated, self.dispose_extent, mask) + else: + self.im.paste(updated, self.dispose_extent) def _getexif(self) -> dict[int, Any] | None: if "exif" not in self.info: