Forums

create animated GIF

Hi,

I'd like to convert several static GIFs into one animated GIF. Could anyone recommend how to achieve on PA?

Which battery-included library could do it? Are there non-Python program available on PA to do it (Imagemagick, etc)? Or should I ask PA support to install Imagemagick?

Searching around it looks Pillow has gifmaker. https://github.com/python-pillow/Pillow/blob/master/Scripts/gifmaker.py But it seems not included in the version on PA. I copied it to my folder but didn't work with following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aerialist/.virtualenvs/django152/local/lib/python2.7/site-packages/gifmaker.py", line 81, in makedelta
    for s in getheader(im) + getdata(im):
  File "/usr/local/lib/python2.7/dist-packages/PIL/PIL/GifImagePlugin.py", line 351, in getheader
    s.append(im.im.getpalette("RGB")[:maxcolor*3])
AttributeError: 'NoneType' object has no attribute 'getpalette'

Another one recommended on web is images2gif from visvis. http://visvis.googlecode.com/hg/vvmovie/images2gif.py

Copied it to my folder but no luck...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aerialist/.virtualenvs/django152/local/lib/python2.7/site-packages/images2gif.py", line 578, in writeGif
    gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose)
  File "/home/aerialist/.virtualenvs/django152/local/lib/python2.7/site-packages/images2gif.py", line 438, in writeGifToFile
    fp.write(header.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xda in position 6: ordinal not in range(128)

Both seems to be close but not quite...

[edit by admin: formatting]

Hmm, that's hard to debug without more information. Is there any chance you could give us the GIFs you're trying to convert, and the exact commands you're using to convert them that are generating those tracebacks?

Wow, for some reason, gifmaker started to work now ;-) Here's my failed attempt of reproducing the errors...


Thanks for the comment, giles! (and thanks for formatting!) Well, I was just expecting to get a tip of other library rather than someone kindly help me debug.

Anyway, here are what I did:

  1. Extract gif images onto a folder on PA https://dl.dropboxusercontent.com/u/393348/gif.zip
  2. Copy gifmaker.py and images2gif.py into local python path on PA. https://github.com/python-pillow/Pillow/blob/master/Scripts/gifmaker.py http://visvis.googlecode.com/hg/vvmovie/images2gif.py
  3. run code below

<b></b>

from PIL import Image
from images2gif import writeGif
import gifmaker
import glob
fnames = glob.glob("sc*.gif")
ims = [Image.open(fname) for fname in fnames]
writeGif("out.gif", ims)


Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/home/aerialist/.virtualenvs/django152/local/lib/python2.7/site-packages/images2gif.py", line 578, in writeGif
        gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose)   File "/home/aerialist/.virtualenvs/django152/local/lib/python2.7/site-packages/images2gif.py", line 438, in writeGifToFile
        fp.write(header.encode('utf-8')) UnicodeDecodeError: 'ascii' codec can't decode byte 0xda in position 6: ordinal not in range(128)

fp = open("out2.gif", "wb")
gifmaker.makedelta(fp, ims) 
30
fp.close()

[edit by admin: formatting]

Mmm... it looks the functionality of gifmaker is quite limited. I cannot control the speed of animation.

I'd still very much appreciate help on getting images2gif.py work or tips about other tools.

Hi aerialist -- no problem, we're here to help :-)

I think this Stack Overflow post explains the images2gif problem.

Thank you very much for your help, giles! This version of images2gif worked fine on PA. Excellent. https://raw.githubusercontent.com/luopio/bag-of-tricks/master/python/images2gif.py

Great! Glad you got it working :-)