10

I want to change size of GIF animation image using python and PIL or PythonMagick. I can't find solution. PIL and thumbnail method works for jpg and png but not for gif. ImageMagick has command mogrify/convert -resize '1280x1024>' but i can't find documentation and i don't know how to do it with pythonmagick.

Anyone knows solution?

In the worst case i use os/subprocess and convert ;-S

Thanks.

eshlox
  • 766
  • 2
  • 11
  • 22
  • 1
    I'd recomment that you do just that in the meantime - use subprocess and convert. Pythonmagic suffers from this lack of proper maintenance - and PIL just supports reading animated GIFs, nothing else. Leave the question open - it maybe that someone with a psnipped to do the resize with Pythonmagic shows up. – jsbueno Apr 03 '12 at 14:22

2 Answers2

13

You can use PIL and images2gif, a short PIL based module linked to on this blog page, and available here. Code used to process this rose.gif is below. I set the images2gif.readGif 'read as numpy array' property to false in order to get a list of PIL images so as I could use the PIL thumbnail function.

Orignial: rose Processed: small rose

import Image
import images2gif

frames = images2gif.readGif("rose.gif",False)
for frame in frames:
    frame.thumbnail((100,100), Image.ANTIALIAS)

images2gif.writeGif('rose99.gif', frames)

I'm not sure how to preserve transparency, my attempts to do so have failed (so far).

Steve
  • 1,726
  • 10
  • 16
fraxel
  • 34,470
  • 11
  • 98
  • 102
  • 2
    According to http://www.velocityreviews.com/forums/t354000-pil-and-transparent-gifs.html, you have to handle transparency yourself, with transparency = im.info["transparency"]; ...; out.save("out.gif", transparency=transparency) – Talia Sep 22 '12 at 03:19
4

Some amazing person made an updated version of images2gif.py that accounts for transparency:

https://bitbucket.org/bench/images2gif.py/overview

There are still some artifacts, but it's way better than the original!

Coconut
  • 41
  • 2
  • 3
    Failed at first attempt - palettes list looks like [None, None.. None] and tries to write that into the new file. Sadly, that's what you're expected to happen when a piece of code doesn't come with tests. The original one at least gives a meaningful message: `RuntimeError: Cannot get palette. Maybe you should try imageio instead.` – Mihnea Simian Jul 08 '15 at 13:54