Skip to content

Commit

Permalink
Merge pull request #7821 from radarhere/apng
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 11, 2024
2 parents 3cdd49f + c322a2f commit 89d8be5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1238,16 +1238,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 @@ -1295,8 +1299,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

0 comments on commit 89d8be5

Please sign in to comment.