Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9eb9391

Browse files
authored
Merge pull request #7521 from radarhere/gif_save_all
2 parents ccfe33f + dc0379f commit 9eb9391

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

Tests/test_file_gif.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,14 @@ def test_identical_frames(tmp_path):
856856

857857

858858
@pytest.mark.parametrize(
859-
"duration", ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500)
859+
"duration",
860+
(
861+
[1000, 1500, 2000],
862+
(1000, 1500, 2000),
863+
# One more duration than the number of frames
864+
[1000, 1500, 2000, 4000],
865+
1500,
866+
),
860867
)
861868
def test_identical_frames_to_single_frame(duration, tmp_path):
862869
out = str(tmp_path / "temp.gif")
@@ -872,7 +879,7 @@ def test_identical_frames_to_single_frame(duration, tmp_path):
872879
assert reread.n_frames == 1
873880

874881
# Assert that the new duration is the total of the identical frames
875-
assert reread.info["duration"] == 8500
882+
assert reread.info["duration"] == 4500
876883

877884

878885
def test_loop_none(tmp_path):

src/PIL/GifImagePlugin.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -624,28 +624,28 @@ def _write_multiple_frames(im, fp, palette):
624624
bbox = None
625625
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
626626

627-
if len(im_frames) > 1:
628-
for frame_data in im_frames:
629-
im_frame = frame_data["im"]
630-
if not frame_data["bbox"]:
631-
# global header
632-
for s in _get_global_header(im_frame, frame_data["encoderinfo"]):
633-
fp.write(s)
634-
offset = (0, 0)
635-
else:
636-
# compress difference
637-
if not palette:
638-
frame_data["encoderinfo"]["include_color_table"] = True
639-
640-
im_frame = im_frame.crop(frame_data["bbox"])
641-
offset = frame_data["bbox"][:2]
642-
_write_frame_data(fp, im_frame, offset, frame_data["encoderinfo"])
643-
return True
644-
elif "duration" in im.encoderinfo and isinstance(
645-
im.encoderinfo["duration"], (list, tuple)
646-
):
647-
# Since multiple frames will not be written, add together the frame durations
648-
im.encoderinfo["duration"] = sum(im.encoderinfo["duration"])
627+
if len(im_frames) == 1:
628+
if "duration" in im.encoderinfo:
629+
# Since multiple frames will not be written, use the combined duration
630+
im.encoderinfo["duration"] = im_frames[0]["encoderinfo"]["duration"]
631+
return
632+
633+
for frame_data in im_frames:
634+
im_frame = frame_data["im"]
635+
if not frame_data["bbox"]:
636+
# global header
637+
for s in _get_global_header(im_frame, frame_data["encoderinfo"]):
638+
fp.write(s)
639+
offset = (0, 0)
640+
else:
641+
# compress difference
642+
if not palette:
643+
frame_data["encoderinfo"]["include_color_table"] = True
644+
645+
im_frame = im_frame.crop(frame_data["bbox"])
646+
offset = frame_data["bbox"][:2]
647+
_write_frame_data(fp, im_frame, offset, frame_data["encoderinfo"])
648+
return True
649649

650650

651651
def _save_all(im, fp, filename):

0 commit comments

Comments
 (0)