Skip to content

Corrected duration when combining multiple GIF frames into single frame#7521

Merged
hugovk merged 1 commit intopython-pillow:mainfrom
radarhere:gif_save_all
Dec 21, 2023
Merged

Corrected duration when combining multiple GIF frames into single frame#7521
hugovk merged 1 commit intopython-pillow:mainfrom
radarhere:gif_save_all

Conversation

@radarhere
Copy link
Member

Here is one of our tests, originally added in #4003

@pytest.mark.parametrize(
"duration", ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500)
)
def test_identical_frames_to_single_frame(duration, tmp_path):
out = str(tmp_path / "temp.gif")
im_list = [
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
]
im_list[0].save(out, save_all=True, append_images=im_list[1:], duration=duration)
with Image.open(out) as reread:
# Assert that all frames were combined
assert reread.n_frames == 1
# Assert that the new duration is the total of the identical frames
assert reread.info["duration"] == 8500

Saving multiple identical GIF frames results in a single frame. That PR changed GifImagePlugin to also give that single frame the combined duration. However, if you look at the test, it is saving three frames, but adding together a sequence of four durations. This PR ignores the fourth duration, so that a merged GIF is not longer than it would have been if the frames were not identical.

A second effect of this change is that when saving multiple identical GIF frames with an integer duration (see the last parameter in the test), rather than a sequence, the duration is multiplied by the number of frames, rather than just being the original integer. This again means that a merged GIF has the same total duration that it would have had if the frames were not identical.

@radarhere radarhere added the GIF label Nov 3, 2023
@radarhere radarhere changed the title Corrected combining durations from GIF multiple frames into single frame Corrected duration when combining multiple GIF frames into single frame Nov 3, 2023
@hugovk hugovk merged commit 9eb9391 into python-pillow:main Dec 21, 2023
@radarhere radarhere deleted the gif_save_all branch December 21, 2023 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants