How do I create an animated gif in Python using Wand?
<p>The instructions are simple enough in the <a href="http://docs.wand-py.org/en/0.4.1/guide/sequence.html" rel="nofollow">Wand docs</a> for <em>reading</em> a sequenced image (e.g. animated gif, icon file, etc.):</p>

<pre><code>&gt;&gt;&gt; from wand.image import Image
&gt;&gt;&gt; with Image(filename='sequence-animation.gif') as image:
...     len(image.sequence)
</code></pre>

<p>...but I'm not sure how to <em>create</em> one. </p>

<p>In Ruby this is easy using <strong>RMagick</strong>, since you have <code>ImageList</code>s. (see <a href="https://gist.github.com/dguzzo/99bbca3df827df475f768383a1b04102" rel="nofollow">my gist</a> for an example.)</p>

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

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

<p>The best examples are located in the unit-tests shipped with the code. <a href="https://github.com/dahlia/wand/blob/master/tests/sequence_test.py" rel="nofollow"><code>wand/tests/sequence_test.py</code></a> for example.</p>

<p>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.</p>

<pre class="lang-py prettyprint-override"><code>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')
</code></pre>

<p><a href="https://i.stack.imgur.com/do522.gif" rel="nofollow"><img src="https://i.stack.imgur.com/do522.gif" alt="output animated.gif"></a></p>

python imagemagick animated-gif wand