Merge pull request #7821 from radarhere/apng

This commit is contained in:
Hugo van Kemenade 2024-03-11 17:38:48 +02:00 committed by GitHub
commit 89d8be5b94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -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)

View File

@ -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":
@ -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