From 459f0d8352f1bf0394e99b19fd09d529aa928d73 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 7 Mar 2023 18:07:46 +1100 Subject: [PATCH] Round duration when saving animated WebP --- Tests/test_file_webp_animated.py | 12 ++++++++++++ src/PIL/WebPImagePlugin.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index c621df0d99c..2fd5e548406 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -134,6 +134,18 @@ def test_timestamp_and_duration(tmp_path): ts += durations[frame] +def test_float_duration(tmp_path): + temp_file = str(tmp_path / "temp.webp") + with Image.open("Tests/images/iss634.apng") as im: + assert im.info["duration"] == 70.0 + + im.save(temp_file, save_all=True) + + with Image.open(temp_file) as reloaded: + reloaded.load() + assert reloaded.info["duration"] == 70 + + def test_seeking(tmp_path): """ Create an animated WebP file, and then try seeking through frames in reverse-order, diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index d060dd4b819..ce8e05fcbb1 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -285,7 +285,7 @@ def _save_all(im, fp, filename): # Append the frame to the animation encoder enc.add( frame.tobytes("raw", rawmode), - timestamp, + round(timestamp), frame.size[0], frame.size[1], rawmode, @@ -305,7 +305,7 @@ def _save_all(im, fp, filename): im.seek(cur_idx) # Force encoder to flush frames - enc.add(None, timestamp, 0, 0, "", lossless, quality, 0) + enc.add(None, round(timestamp), 0, 0, "", lossless, quality, 0) # Get the final output from the encoder data = enc.assemble(icc_profile, exif, xmp)