The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001

--

Chapters
00:00 Question
01:34 Accepted answer (Score 4)
02:45 Answer 2 (Score 7)
03:16 Answer 3 (Score 0)
03:34 Thank you

--

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

Answer 1 links:
https://bitbucket.org/bench/images2gif.p...

Answer 2 links:
[Pillow]: https://pypi.python.org/pypi/Pillow
[3.0.0]: https://github.com/python-pillow/Pillow/...
[PR #1384]: https://github.com/python-pillow/Pillow/...

--

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.