Skip to content
Open
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
16 changes: 16 additions & 0 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,22 @@ def test_apng_save_large_duration(tmp_path: Path) -> None:
with pytest.raises(ValueError, match="cannot write duration"):
im.save(test_file, save_all=True, append_images=[im2], duration=65536000)

# Do not merge identical frames if the combined duration cannot be written
im.save(
test_file,
save_all=True,
append_images=[im.copy()],
duration=[32768000, 32768000],
)

with Image.open(test_file) as reloaded:
assert isinstance(reloaded, PngImagePlugin.PngImageFile)
assert reloaded.n_frames == 2
assert reloaded.info["duration"] == 32768000

reloaded.seek(1)
assert reloaded.info["duration"] == 32768000


def test_apng_save_disposal(tmp_path: Path) -> None:
test_file = tmp_path / "temp.png"
Expand Down
9 changes: 7 additions & 2 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,13 @@ def _write_multiple_frames(
and prev_blend == encoderinfo.get("blend")
and "duration" in encoderinfo
):
previous.encoderinfo["duration"] += encoderinfo["duration"]
continue
new_duration = (
previous.encoderinfo["duration"] + encoderinfo["duration"]
)
delay = Fraction(new_duration / 1000).limit_denominator(65535)
if delay.numerator <= 65535:
previous.encoderinfo["duration"] = new_duration
continue
else:
bbox = None
im_frames.append(_Frame(im_frame, bbox, encoderinfo))
Expand Down
Loading