The Python Oracle

How do you scale an animated GIF image in PIL and preserve the animation

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 How Do You Scale An Animated Gif Image In Pil And Preserve The Animation
01:10 Accepted Answer Score 5
02:19 Answer 2 Score 8
02:42 Answer 3 Score 0
02:52 Thank you

--

Full question
https://stackoverflow.com/questions/9128...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #image #imageprocessing #plone #pythonimaginglibrary

#avk47



ANSWER 1

Score 8


You can use images2gif.py to read gifs and than scale each frame independently. images2gif will allow you to write an animated gif with a sequence of Images.

The images2gif.py I found on the internet didn't handle transparency, so i fixed that. You can find that here: https://bitbucket.org/bench/images2gif.py




ACCEPTED ANSWER

Score 5


PIL has got some limited support for animated GIF's but it is, as said, limited, and you have to work in very low level to deal with it.

While it is possible to manipulate and scale GIFs in PIL (Pillow) at the current version (9.2), its rather obscure, and only offer the "rendered" version of each frame: that is, if you have a GIF with custom palettes or shapes per frame, you can only get access to the flattened frames as they are presented in an image viewing program.

I'd advise trying some other method for scaling images than PIL if you want to deal with animated gif's. Possibly, the most straightforward way is to use an off-process ImageMagick, with subprocess.Popen - (and even then, at this time, I am only guessing ImageMagick "does the right thing" with animated GIF's) -

An option is to have an "image processing server", with another Python script, apart from your zope install that will receive requests to scale the images - possibly with a xmlrpc call - you could build this as a GIMP plug-in and use GIMP for scaling the GIF's.

Another option is to leave things as they are, and use "stills" for animated GIF's where they need to appear in another dimension than the original, and display the original image where the animation is appropriate. (Or maybe simply require that animated gif's be submitted already on the proper size)




ANSWER 3

Score 0


Pillow (PIL's fork) supports animated GIF since version 3.0.0 when PR #1384 was merged.