Animated WebP resize doesn't keep loop and delay
Hi,
When resizing an animation (webp or gif), metadata is lost and the resulting image is not animated. Converting animation without resizing works fine. Tested against debian libvips42 and latest master.
-
vipsthumbnail -s 200x200 -o /tmp/gif.webp samples/animated.gif
-
vipsheader -a /tmp/gif.webp /tmp/gif.webp: 200x116 uchar, 3 bands, srgb, webpload width: 200 height: 116 bands: 3 format: uchar coding: none interpretation: srgb xoffset: 0 yoffset: 0 xres: 1 yres: 1 filename: /tmp/gif.webp vips-loader: webpload
Hello @georgiopandarez,
By default, it'll just process the first frame -- you need to ask for all frames. Try:
vipsthumbnail something.gif[n=-1] -s 200x200 -o x.gif
You can use page= and n= to select a range of pages in the image to thumbnail. n=-1 means all pages.
Hi!
Thanks for the prompt reply. The issue seems to be when resizing and converting. ie
vipsthumbnail something.gif[n=-1] -s 200x200 -o x.webp
This results in an image with no animation.
The below python also shows issues:
image = pyvips.Image.gifload(sys.argv[1], access="sequential", n=-1)
dim = int(sys.argv[2])
resized_image = image.resize(dim / image.width, vscale=dim / image.height)
resized_image.webpsave(sys.argv[1] + ".webp")
image.webpsave(sys.argv[1] + ".noresize.webp")
The resized webp version shows a single image with the frames on top of each other. The resave shows an image without animation. Does one need to manually calculate page_height?
It should work, I think. With this GIF:

If I run:
vipsthumbnail dancing-banana.gif[n=-1] -s 200 -o x.webp
I get an animation that plays correctly in chrome (I'd attach it, but github does not allow webp). Do you have an example that fails?
Yes, you have to adjust page-height yourself. thumbnail has extra logic to do this for you.
It's best to use thumbnail if you can -- it also knows about webp shrink-on-load, so it should be much faster than resize.
Sorry to waste your time.
My code issue was that image.height returns the height of the image * the number of pages. I needed to take that into account when resizing.
I tried to verify using vipsthumbnail but missed that -o placed the image in the same directory as the input, not relative to the program.
Yes, it's relative to the input, so you can do eg.:
vipsthumbnail */*.jpg -o %s-thumbnail.jpg
It's more like a pattern for the output path than a standard -o switch. Anyway, glad it's working.