Skip to content

Commit f846849

Browse files
authored
Merge pull request #6265 from radarhere/gif_duration
Use durations from each frame by default when saving GIFs
2 parents 3fa89f0 + 7e084c7 commit f846849

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Tests/test_file_gif.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, features
6+
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, ImageSequence, features
77

88
from .helper import (
99
assert_image_equal,
@@ -691,6 +691,23 @@ def test_multiple_duration(tmp_path):
691691
pass
692692

693693

694+
def test_roundtrip_info_duration(tmp_path):
695+
duration_list = [100, 500, 500]
696+
697+
out = str(tmp_path / "temp.gif")
698+
with Image.open("Tests/images/transparent_dispose.gif") as im:
699+
assert [
700+
frame.info["duration"] for frame in ImageSequence.Iterator(im)
701+
] == duration_list
702+
703+
im.save(out, save_all=True)
704+
705+
with Image.open(out) as reloaded:
706+
assert [
707+
frame.info["duration"] for frame in ImageSequence.Iterator(reloaded)
708+
] == duration_list
709+
710+
694711
def test_identical_frames(tmp_path):
695712
duration_list = [1000, 1500, 2000, 4000]
696713

src/PIL/GifImagePlugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def _write_single_frame(im, fp, palette):
561561

562562
def _write_multiple_frames(im, fp, palette):
563563

564-
duration = im.encoderinfo.get("duration", im.info.get("duration"))
564+
duration = im.encoderinfo.get("duration")
565565
disposal = im.encoderinfo.get("disposal", im.info.get("disposal"))
566566

567567
im_frames = []
@@ -579,6 +579,8 @@ def _write_multiple_frames(im, fp, palette):
579579
encoderinfo = im.encoderinfo.copy()
580580
if isinstance(duration, (list, tuple)):
581581
encoderinfo["duration"] = duration[frame_count]
582+
elif duration is None and "duration" in im_frame.info:
583+
encoderinfo["duration"] = im_frame.info["duration"]
582584
if isinstance(disposal, (list, tuple)):
583585
encoderinfo["disposal"] = disposal[frame_count]
584586
frame_count += 1

0 commit comments

Comments
 (0)