Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use maximum frame size in IHDR chunk when saving APNG images #7821

Merged
merged 1 commit into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tests/test_file_apng.py
Expand Up @@ -668,6 +668,16 @@ def test_apng_save_blend(tmp_path: Path) -> None:
assert im.getpixel((0, 0)) == (0, 255, 0, 255)


def test_apng_save_size(tmp_path: Path) -> None:
test_file = str(tmp_path / "temp.png")

im = Image.new("L", (100, 100))
im.save(test_file, save_all=True, append_images=[Image.new("L", (200, 200))])

with Image.open(test_file) as reloaded:
assert reloaded.size == (200, 200)


def test_seek_after_close() -> None:
im = Image.open("Tests/images/apng/delay.png")
im.seek(1)
Expand Down
8 changes: 6 additions & 2 deletions src/PIL/PngImagePlugin.py
Expand Up @@ -1232,16 +1232,20 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
"default_image", im.info.get("default_image")
)
modes = set()
sizes = set()
append_images = im.encoderinfo.get("append_images", [])
for im_seq in itertools.chain([im], append_images):
for im_frame in ImageSequence.Iterator(im_seq):
modes.add(im_frame.mode)
sizes.add(im_frame.size)
for mode in ("RGBA", "RGB", "P"):
if mode in modes:
break
else:
mode = modes.pop()
size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))
else:
size = im.size
mode = im.mode

if mode == "P":
Expand Down Expand Up @@ -1289,8 +1293,8 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
chunk(
fp,
b"IHDR",
o32(im.size[0]), # 0: size
o32(im.size[1]),
o32(size[0]), # 0: size
o32(size[1]),
mode, # 8: depth/type
b"\0", # 10: compression
b"\0", # 11: filter category
Expand Down