wagtail / Willow

A wrapper that combines the functionality of multiple Python image libraries into one API
https://willow.wagtail.org/
BSD 3-Clause "New" or "Revised" License
273 stars 53 forks source link

Pillow animated gifs #76

Open kaedroho opened 5 years ago

kaedroho commented 5 years ago

This PR adds support for animated GIFs in the Pillow backend

danihodovic commented 5 years ago

What's the status of this PR?

tomdyson commented 5 years ago

@danihodovic it's awaiting review

gasman commented 4 years ago

Code looks good to me. One thing I'm not clear on, though: given that PillowAnimatedImage can presumably do everything PillowImage can do and more, is there any reason for the "choose the best backend" logic in registry.py to ever prefer PillowImage - and if not, do we really need both classes to exist?

kaedroho commented 4 years ago

Code looks good to me. One thing I'm not clear on, though: given that PillowAnimatedImage can presumably do everything PillowImage can do and more, is there any reason for the "choose the best backend" logic in registry.py to ever prefer PillowImage - and if not, do we really need both classes to exist?

Good point! I've merged the two classes together now

kaedroho commented 4 years ago

My plan for this is to implement both animated GIF and animated WEBP support in Willow, then add logic into Wagtail so that it always transcodes animated GIFs into animated WEBP files. This fixes issues with having to save a GIF that contains more than 256 colours (which will be inevitable if we've resized the image). See discussion in https://github.com/wagtail/Willow/pull/76#discussion_r331426263

coredumperror commented 4 years ago

I just tried out this PR on my shop's codebase, because the Wand-based solution causes our servers to die when someone uploads a sufficiently large or complex animated GIF.

Using this code, I was able to get one of the offending GIFs to generate renditions without crashing, but they're... broken.

Here's the original (appologies for the gigantic GIF): funnel_pluviation

And here's the Image Chooser rendition: funnel_pluviation max-165x165

Maybe I've done something wrong? Is there anything I can tweak to try to make Pillow generate animated GIFs in a different way, which hopefully won't break them?

kaedroho commented 4 years ago

Hey @coredumperror

I'm getting similar results with a different image here

I think this is just a Pillow limitation. It must be difficult saving GIF images like this which have a limit of 256 colours. I think the best solution would be for us to add support for animated webp/apng so images can be saved in those formats instead.

coredumperror commented 4 years ago

I think the best solution would be for us to add support for animated webp/apng so images can be saved in those formats instead.

Yes, please! Animated GIF is soooo 1990s. Hell, mosts sites that let you share GIFs just convert to mp4 video with no audio track. I wonder if that would be possible?

kaedroho commented 4 years ago

Hell, mosts sites that let you share GIFs just convert to mp4 video with no audio track. I wonder if that would be possible?

Hadn't thought of that before! I think we could view MP4 w/o audio as just another animated image format, so could support that if Pillow/wand does (but can always add an ffmpeg plugin if not!)

gasman commented 4 years ago

Relevant blog post: https://paulbakaus.com/2020/01/17/gifs-must-die/ Historically Wagtail has deliberately shied away from dealing with video because doing it well (handling arbitrary format conversions, extracting metadata, generating thumbnails, dealing with large file uploads...) would be as big a job as the content management side... but given that the demand for animated GIFs isn't going away, and the border between animation and video is getting ever more blurred, I think it might be time to rethink that.

coredumperror commented 4 years ago

I think it might be time to rethink that.

I am exceedingly glad to hear this. :)

jamesray commented 3 years ago

@zerolab possibly in relation to issue #80 , is there any traction on this?

coredumperror commented 3 years ago

We're still very interested in Wagtail properly supporting animated whatevers (GIF, webp, mp4, etc.).

zerolab commented 3 years ago

Incidentally @thenewguy makes the case for animated GIFs to be treated as "media" (i.e. no resizing and other Willlow filters) - https://github.com/torchbox/wagtailmedia/issues/127 Might that be the way to go?

kaedroho commented 3 years ago

I like that idea! In many cases, GIFs would be both smaller and better quality if we don't try to process them. Wagtailmedia may also be able to provide the processing code required to convert them into videos as well.