Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when merging identical images to GIF with a list of durations #4003

Merged
merged 8 commits into from Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -495,6 +495,26 @@ def test_identical_frames(self):
# Assert that the new duration is the total of the identical frames
self.assertEqual(reread.info["duration"], 4500)

def test_identical_frames_to_single_frame(self):
for duration in ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500):
out = self.tempfile("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
)
reread = Image.open(out)

# Assert that all frames were combined
self.assertEqual(reread.n_frames, 1)

# Assert that the new duration is the total of the identical frames
self.assertEqual(reread.info["duration"], 8500)

def test_number_of_loops(self):
number_of_loops = 2

Expand Down
5 changes: 5 additions & 0 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -489,6 +489,11 @@ def _write_multiple_frames(im, fp, palette):
offset = frame_data["bbox"][:2]
_write_frame_data(fp, im_frame, offset, frame_data["encoderinfo"])
return True
elif "duration" in im.encoderinfo and isinstance(
im.encoderinfo["duration"], (list, tuple)
):
# Since multiple frames will not be written, add together the frame durations
im.encoderinfo["duration"] = sum(im.encoderinfo["duration"])


def _save_all(im, fp, filename):
Expand Down