Skip to content

Restore WebP duration and timestamp when loading an already decoded frame - #10677

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
Yasser-Ameur:fix/webp-duration-after-seeking-back
Dec 1, 2026
Merged

radarhere merged 3 commits into
python-pillow:mainfrom
Yasser-Ameur:fix/webp-duration-after-seeking-back

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • After im.load(), im.seek(1), im.seek(0), im.load() on an animated WebP, im.info has no duration or timestamp, although frame 0 is loaded. GIF, APNG and AVIF images have their duration there after the same calls.
  • Seeking removes the two keys (Remove duration and timestamp from WebP info after seek #9791), and load() only set them again when it decoded a frame. Seeking away and back to the frame that is already decoded skips the decode, so they stayed missing. load() now puts back the values of the decoded frame.
  • The keys are still absent between a seek and the next load, as documented.
  • A test in Tests/test_file_webp_animated.py covers the sequence above.
import io

from PIL import Image

frames = [Image.new("RGB", (16, 16), (i * 60, 0, 0)) for i in range(3)]
webp = io.BytesIO()
frames[0].save(webp, "WEBP", save_all=True, append_images=frames[1:], duration=[100, 200, 300], lossless=True)
gif = io.BytesIO()
frames[0].save(gif, "GIF", save_all=True, append_images=frames[1:], duration=[100, 200, 300])

for fmt, fp in (("WEBP", webp), ("GIF", gif)):
    fp.seek(0)
    with Image.open(fp) as im:
        im.load()
        im.seek(1)
        im.seek(0)
        im.load()
        print(fmt, im.info.get("duration"))

On 13.0.0 this prints WEBP None and GIF 100. On that release it was also visible when saving an animated WebP as APNG, where the first frame came out with a duration of 0.

…rame

Seeking away from a loaded frame and back removed its duration and
timestamp from info, and load() did not decode the frame again, so they
stayed missing.
@radarhere radarhere added the WebP label Nov 30, 2026
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review November 30, 2026 21:58
@radarhere
radarhere force-pushed the fix/webp-duration-after-seeking-back branch 3 times, most recently from 251e11f to 530a7fa Compare December 1, 2026 01:28
@radarhere

Copy link
Copy Markdown
Member

Using a class variable for __loaded_info meant that it was shared between all instances of WebPImageFile, so I've switched to an instance variable instead.

@radarhere
radarhere force-pushed the fix/webp-duration-after-seeking-back branch from 530a7fa to 94548c3 Compare December 1, 2026 01:33
@radarhere
radarhere enabled auto-merge (squash) December 1, 2026 01:33
@radarhere
radarhere merged commit 55efb77 into python-pillow:main Dec 1, 2026
55 of 56 checks passed
@radarhere radarhere added the 🤖-assisted AI-assisted label Dec 1, 2026
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.

2 participants