How to create an animated gif in Python using Wand?

The instructions are simple enough in Wand docs to read a sequential 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 it.

In Ruby, it is easy to use RMagick since you have ImageList s. (see my point for an example.)

I tried to create an Image (as a β€œcontainer”) and create an instance of each SingleImage using the image path, but I'm sure this is wrong, especially since the constructor documentation for SingleImage does not look for end-user use.

I also tried to create wand.sequence.Sequence and go from this angle, but also to a dead end. I feel very lost.

+5
source share
1 answer

The best examples are in unit tests that come with code. wand/tests/sequence_test.py e.g.

To create an animated gif using a stick, be sure to load the image into a sequence, and then set up additional delay / optimization processing after loading all frames.

 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

+4
source

Source: https://habr.com/ru/post/1257915/


All Articles