diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index b8e0e623ec8..5b906191b55 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -160,6 +160,19 @@ def test_frame_durations(tmp_path: Path) -> None: assert reloaded.info["duration"] == duration +def test_save_info_loop(tmp_path: Path) -> None: + temp_file = tmp_path / "temp.webp" + frames = [Image.new("RGB", (1, 1), color) for color in ("#f00", "#0f0")] + frames[0].save(temp_file, save_all=True, append_images=frames[1:], loop=2) + + with Image.open(temp_file) as im: + assert im.info["loop"] == 2 + im.save(temp_file, save_all=True) + + with Image.open(temp_file) as im: + assert im.info["loop"] == 2 + + def test_float_duration(tmp_path: Path) -> None: temp_file = tmp_path / "temp.webp" with Image.open("Tests/images/iss634.apng") as im: diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 8ca82474b3e..4c1116e98ef 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -1560,7 +1560,8 @@ will be saved, and the following options will also be available. duration for each frame separately. **loop** - Number of times to repeat the animation. Defaults to [0 = infinite]. + Number of times to repeat the animation. Defaults to the ``loop`` value in + :py:attr:`~PIL.Image.Image.info`, or 0 (infinite) if there is none. **background** Background color of the canvas, as an RGBA tuple with values in diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 9cb99e7d082..3016da79b18 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -219,7 +219,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: background = (background, background, background, 255) duration = im.encoderinfo.get("duration") - loop = im.encoderinfo.get("loop", 0) + loop = im.encoderinfo.get("loop", im.info.get("loop", 0)) minimize_size = im.encoderinfo.get("minimize_size", False) kmin = im.encoderinfo.get("kmin", None) kmax = im.encoderinfo.get("kmax", None)