Use durations from each frame by default when saving

This commit is contained in:
Andrew Murray 2022-05-03 20:07:47 +10:00
parent 9d988dab6a
commit 7e084c7ede
2 changed files with 21 additions and 2 deletions

View File

@ -3,7 +3,7 @@ from io import BytesIO
import pytest import pytest
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, features from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, ImageSequence, features
from .helper import ( from .helper import (
assert_image_equal, assert_image_equal,
@ -691,6 +691,23 @@ def test_multiple_duration(tmp_path):
pass pass
def test_roundtrip_info_duration(tmp_path):
duration_list = [100, 500, 500]
out = str(tmp_path / "temp.gif")
with Image.open("Tests/images/transparent_dispose.gif") as im:
assert [
frame.info["duration"] for frame in ImageSequence.Iterator(im)
] == duration_list
im.save(out, save_all=True)
with Image.open(out) as reloaded:
assert [
frame.info["duration"] for frame in ImageSequence.Iterator(reloaded)
] == duration_list
def test_identical_frames(tmp_path): def test_identical_frames(tmp_path):
duration_list = [1000, 1500, 2000, 4000] duration_list = [1000, 1500, 2000, 4000]

View File

@ -561,7 +561,7 @@ def _write_single_frame(im, fp, palette):
def _write_multiple_frames(im, fp, palette): def _write_multiple_frames(im, fp, palette):
duration = im.encoderinfo.get("duration", im.info.get("duration")) duration = im.encoderinfo.get("duration")
disposal = im.encoderinfo.get("disposal", im.info.get("disposal")) disposal = im.encoderinfo.get("disposal", im.info.get("disposal"))
im_frames = [] im_frames = []
@ -579,6 +579,8 @@ def _write_multiple_frames(im, fp, palette):
encoderinfo = im.encoderinfo.copy() encoderinfo = im.encoderinfo.copy()
if isinstance(duration, (list, tuple)): if isinstance(duration, (list, tuple)):
encoderinfo["duration"] = duration[frame_count] encoderinfo["duration"] = duration[frame_count]
elif duration is None and "duration" in im_frame.info:
encoderinfo["duration"] = im_frame.info["duration"]
if isinstance(disposal, (list, tuple)): if isinstance(disposal, (list, tuple)):
encoderinfo["disposal"] = disposal[frame_count] encoderinfo["disposal"] = disposal[frame_count]
frame_count += 1 frame_count += 1