diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index c89dc2d1500..ff33119504a 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -729,6 +729,20 @@ def test_apng_save_disposal_previous(tmp_path: Path) -> None: assert im.getpixel((64, 32)) == (0, 255, 0, 255) +def test_apng_save_info_disposal(tmp_path: Path) -> None: + test_file = tmp_path / "temp.png" + red = Image.new("RGBA", (32, 32), (255, 0, 0, 255)) + red_green = red.copy() + red_green.paste((0, 255, 0, 255), (0, 0, 10, 10)) + + red.info["disposal"] = PngImagePlugin.Disposal.OP_PREVIOUS + red.save(test_file, save_all=True, append_images=[red_green]) + with Image.open(test_file) as im: + im.seek(1) + assert im.getpixel((0, 0)) == (0, 255, 0, 255) + assert im.getpixel((20, 20)) == (255, 0, 0, 255) + + def test_apng_save_disposal_background(tmp_path: Path) -> None: test_file = tmp_path / "temp.png" im = Image.new("L", (32, 32), 128) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 832bafe46f4..ab4e8f624d8 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1225,16 +1225,20 @@ def _write_multiple_frames( encoderinfo["duration"] = duration[frame_count] elif duration is None and "duration" in im_frame.info: encoderinfo["duration"] = im_frame.info["duration"] - if isinstance(disposal, (list, tuple)): - encoderinfo["disposal"] = disposal[frame_count] - if isinstance(blend, (list, tuple)): - encoderinfo["blend"] = blend[frame_count] + encoderinfo["disposal"] = ( + disposal[frame_count] + if isinstance(disposal, (list, tuple)) + else disposal + ) + encoderinfo["blend"] = ( + blend[frame_count] if isinstance(blend, (list, tuple)) else blend + ) frame_count += 1 if im_frames: previous = im_frames[-1] - prev_disposal = previous.encoderinfo.get("disposal") - prev_blend = previous.encoderinfo.get("blend") + prev_disposal = previous.encoderinfo["disposal"] + prev_blend = previous.encoderinfo["blend"] if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2: prev_disposal = Disposal.OP_BACKGROUND @@ -1253,8 +1257,8 @@ def _write_multiple_frames( bbox = delta.getbbox(alpha_only=False) if ( not bbox - and prev_disposal == encoderinfo.get("disposal") - and prev_blend == encoderinfo.get("blend") + and prev_disposal == encoderinfo["disposal"] + and prev_blend == encoderinfo["blend"] and "duration" in encoderinfo ): new_duration = ( @@ -1304,8 +1308,8 @@ def _write_multiple_frames( if delay.numerator > 65535: msg = "cannot write duration" raise ValueError(msg) - frame_disposal = encoderinfo.get("disposal", disposal) - frame_blend = encoderinfo.get("blend", blend) + frame_disposal = encoderinfo["disposal"] + frame_blend = encoderinfo["blend"] # frame control chunk( fp,