Skip to content

Use info loop when saving animated WebP images - #10709

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
guru-bharadwaj20:webp-keep-loop
Dec 3, 2026
Merged

radarhere merged 2 commits into
python-pillow:mainfrom
guru-bharadwaj20:webp-keep-loop

Conversation

@guru-bharadwaj20

Copy link
Copy Markdown
Contributor

When an animated image is saved as WebP without a loop argument, the loop count is always 0, even if the image has a different loop in im.info. GIF and APNG both fall back to im.info["loop"], and WebP already does this for background, so re-saving a WebP (or converting a GIF or APNG) that plays a set number of times turns it into an infinite loop:

from PIL import Image

frames = [Image.new("RGB", (1, 1), c) for c in ("#f00", "#0f0")]
frames[0].save("in.webp", save_all=True, append_images=frames[1:], loop=3)

with Image.open("in.webp") as im:
    print(im.info["loop"])  # 3
    im.save("out.webp", save_all=True)

with Image.open("out.webp") as im:
    print(im.info["loop"])  # main: 0, this branch: 3

Changes proposed in this pull request:

  • In WebPImagePlugin._save_all, use im.encoderinfo.get("loop", im.info.get("loop", 0)), the same line PngImagePlugin uses. An explicit loop argument still wins.
  • Test that the loop count from a WebP, APNG or GIF source is kept when saving as WebP, and that an explicit loop overrides it.
  • Update the WebP loop save option docs and add a release note.

Checks run locally (Linux, Python 3.14):

  • python3 -m pytest Tests/test_file_webp_animated.py Tests/test_file_webp.py Tests/test_file_gif.py Tests/test_file_apng.py: 217 passed.
  • With the plugin change reverted, the 3 new test_loop_from_info cases fail.
  • pre-commit run on the changed files passes.

GIF and APNG fall back to im.info["loop"] when no loop argument is given, but WebP always used 0, so re-saving an animation that plays a set number of times made it loop forever.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 05:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@radarhere
radarhere merged commit ed0ca1c into python-pillow:main Dec 3, 2026
53 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants