Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an animated gif in Python using Wand?

The instructions are simple enough in the Wand docs for reading a sequenced image (e.g. animated gif, icon file, etc.):

>>> from wand.image import Image
>>> with Image(filename='sequence-animation.gif') as image:
...     len(image.sequence)

...but I'm not sure how to create one.

In Ruby this is easy using RMagick, since you have ImageLists. (see my gist for an example.)

I tried creating an Image (as the "container") and instantiating each SingleImage with an image path, but I'm pretty sure that's wrong, especially since the constructor documentation for SingleImage doesn't look for use by the end-user.

I also tried creating a wand.sequence.Sequence and going from that angle, but hit a dead-end as well. I feel very lost.

like image 580
Dominick Avatar asked Oct 09 '16 03:10

Dominick


People also ask

How do you make a movable GIF?

If you use Google Photos on Android (or iOS), you can make an animated GIF from a selection of your pictures. Just tap Library, then Utilities and Create New. Choose Animation, select the photos and tap Create.

How do I make my own animated GIFs?

Visit giphy.com/create/gifmaker to get started. Choose Photo or GIF to add by dragging and dropping, clicking the blue search button to browse your files, or entering image URLs to add files to your slideshow. Drag and drop images to change the order of your images to change the way they will appear in your slideshow.

What is wand in Python?

The Wand is an Imagick library for python. It supports the functionalities of Imagick API in Python 2.6, 2.7, 3.3+, and PyPy. This library not only helps in processing the images but also provides valuable functionalities for Machine Learning codes using NumPy.


1 Answers

The best examples are located in the unit-tests shipped with the code. wand/tests/sequence_test.py for example.

For creating an animated gif with wand, remember to load the image into the sequence, and then set the additional delay/optimize handling after all frames are loaded.

from wand.image import Image

with Image() as wand:
    # Add new frames into sequance
    with Image(filename='1.png') as one:
        wand.sequence.append(one)
    with Image(filename='2.png') as two:
        wand.sequence.append(two)
    with Image(filename='3.png') as three:
        wand.sequence.append(three)
    # Create progressive delay for each frame
    for cursor in range(3):
        with wand.sequence[cursor] as frame:
            frame.delay = 10 * (cursor + 1)
    # Set layer type
    wand.type = 'optimize'
    wand.save(filename='animated.gif')

output animated.gif

like image 115
emcconville Avatar answered Nov 01 '22 04:11

emcconville