Skip to content

Commit a7fe25d

Browse files
authored
Merge pull request #7522 from radarhere/png_save_all
2 parents c97d2e4 + c29648f commit a7fe25d

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

Tests/test_file_apng.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def test_apng_save(tmp_path):
350350
im.load()
351351
assert not im.is_animated
352352
assert im.n_frames == 1
353-
assert im.get_format_mimetype() == "image/apng"
353+
assert im.get_format_mimetype() == "image/png"
354354
assert im.info.get("default_image") is None
355355
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
356356
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -450,26 +450,29 @@ def test_apng_save_duration_loop(tmp_path):
450450
test_file, save_all=True, append_images=[frame, frame], duration=[500, 100, 150]
451451
)
452452
with Image.open(test_file) as im:
453-
im.load()
454453
assert im.n_frames == 1
455-
assert im.info.get("duration") == 750
454+
assert "duration" not in im.info
456455

457-
# test info duration
458-
frame.info["duration"] = 750
459-
frame.save(test_file, save_all=True)
456+
different_frame = Image.new("RGBA", (128, 64))
457+
frame.save(
458+
test_file,
459+
save_all=True,
460+
append_images=[frame, different_frame],
461+
duration=[500, 100, 150],
462+
)
460463
with Image.open(test_file) as im:
461-
assert im.info.get("duration") == 750
462-
464+
assert im.n_frames == 2
465+
assert im.info["duration"] == 600
463466

464-
def test_apng_save_duplicate_duration(tmp_path):
465-
test_file = str(tmp_path / "temp.png")
466-
frame = Image.new("RGB", (1, 1))
467+
im.seek(1)
468+
assert im.info["duration"] == 150
467469

468-
# Test a single duration is correctly combined across duplicate frames
469-
frame.save(test_file, save_all=True, append_images=[frame, frame], duration=500)
470+
# test info duration
471+
frame.info["duration"] = 300
472+
frame.save(test_file, save_all=True, append_images=[frame, different_frame])
470473
with Image.open(test_file) as im:
471-
assert im.n_frames == 1
472-
assert im.info.get("duration") == 1500
474+
assert im.n_frames == 2
475+
assert im.info["duration"] == 600
473476

474477

475478
def test_apng_save_disposal(tmp_path):
@@ -674,15 +677,16 @@ def test_seek_after_close():
674677

675678
@pytest.mark.parametrize("mode", ("RGBA", "RGB", "P"))
676679
@pytest.mark.parametrize("default_image", (True, False))
677-
def test_different_modes_in_later_frames(mode, default_image, tmp_path):
680+
@pytest.mark.parametrize("duplicate", (True, False))
681+
def test_different_modes_in_later_frames(mode, default_image, duplicate, tmp_path):
678682
test_file = str(tmp_path / "temp.png")
679683

680684
im = Image.new("L", (1, 1))
681685
im.save(
682686
test_file,
683687
save_all=True,
684688
default_image=default_image,
685-
append_images=[Image.new(mode, (1, 1))],
689+
append_images=[im.convert(mode) if duplicate else Image.new(mode, (1, 1), 1)],
686690
)
687691
with Image.open(test_file) as reloaded:
688692
assert reloaded.mode == mode

src/PIL/PngImagePlugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,9 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
11561156
encoderinfo["duration"] = duration
11571157
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
11581158

1159+
if len(im_frames) == 1 and not default_image:
1160+
return im_frames[0]["im"]
1161+
11591162
# animation control
11601163
chunk(
11611164
fp,
@@ -1391,8 +1394,10 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
13911394
chunk(fp, b"eXIf", exif)
13921395

13931396
if save_all:
1394-
_write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
1395-
else:
1397+
im = _write_multiple_frames(
1398+
im, fp, chunk, rawmode, default_image, append_images
1399+
)
1400+
if im:
13961401
ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
13971402

13981403
if info:

0 commit comments

Comments
 (0)