Merge pull request #7437 from radarhere/apng

This commit is contained in:
Hugo van Kemenade 2023-10-13 23:48:22 +02:00 committed by GitHub
commit 101154e0d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -673,10 +673,16 @@ def test_seek_after_close():
@pytest.mark.parametrize("mode", ("RGBA", "RGB", "P")) @pytest.mark.parametrize("mode", ("RGBA", "RGB", "P"))
def test_different_modes_in_later_frames(mode, tmp_path): @pytest.mark.parametrize("default_image", (True, False))
def test_different_modes_in_later_frames(mode, default_image, tmp_path):
test_file = str(tmp_path / "temp.png") test_file = str(tmp_path / "temp.png")
im = Image.new("L", (1, 1)) im = Image.new("L", (1, 1))
im.save(test_file, save_all=True, append_images=[Image.new(mode, (1, 1))]) im.save(
test_file,
save_all=True,
default_image=default_image,
append_images=[Image.new(mode, (1, 1))],
)
with Image.open(test_file) as reloaded: with Image.open(test_file) as reloaded:
assert reloaded.mode == mode assert reloaded.mode == mode

View File

@ -1104,9 +1104,6 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
for im_frame in ImageSequence.Iterator(im_seq): for im_frame in ImageSequence.Iterator(im_seq):
if im_frame.mode == rawmode: if im_frame.mode == rawmode:
im_frame = im_frame.copy() im_frame = im_frame.copy()
else:
if rawmode == "P":
im_frame = im_frame.convert(rawmode, palette=im.palette)
else: else:
im_frame = im_frame.convert(rawmode) im_frame = im_frame.convert(rawmode)
encoderinfo = im.encoderinfo.copy() encoderinfo = im.encoderinfo.copy()
@ -1167,6 +1164,8 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
# default image IDAT (if it exists) # default image IDAT (if it exists)
if default_image: if default_image:
if im.mode != rawmode:
im = im.convert(rawmode)
ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)]) ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
seq_num = 0 seq_num = 0
@ -1228,11 +1227,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
) )
modes = set() modes = set()
append_images = im.encoderinfo.get("append_images", []) append_images = im.encoderinfo.get("append_images", [])
if default_image: for im_seq in itertools.chain([im], append_images):
chain = itertools.chain(append_images)
else:
chain = itertools.chain([im], append_images)
for im_seq in chain:
for im_frame in ImageSequence.Iterator(im_seq): for im_frame in ImageSequence.Iterator(im_seq):
modes.add(im_frame.mode) modes.add(im_frame.mode)
for mode in ("RGBA", "RGB", "P"): for mode in ("RGBA", "RGB", "P"):