diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index e9fb41d03ae..381d799e776 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -252,7 +252,7 @@ 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) @@ -323,8 +323,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 +337,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..81919314f6d 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1113,7 +1113,12 @@ 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: + 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"] ) @@ -1130,7 +1135,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 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: