Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = (
Expand Down Expand Up @@ -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,
Expand Down
Loading