2016-10-09 52 views
5

的说明在Wand docs非常简单,阅读排序的图像(如gif动画,图标文件等):如何使用魔杖在Python中创建动画GIF?

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

...但我不知道如何创建一。

在Ruby中,这很容易使用RMagick,因为你有ImageList s。 (见my gist的一个例子。)

我试图创建一个Image(作为“容器”),并与成像路径实例每个SingleImage,但我敢肯定,这是错误的,特别是因为SingleImage没有按构造文档不寻找最终用户的使用。

我也试过创建一个wand.sequence.Sequence,并从那个角度出发,但也碰到了一个死胡同。我感到非常失落。

+0

我的问题看起来是http://stackoverflow.com/questions/17394869/writing-animated-gif-using-wand-and-imagemagick?rq=1 – Dominick

+0

的一个欺骗那些好奇的人,这就是我最终的结果(它的工作原理是我想要的),感谢@ emcconville接受的答案如下:https://gist.github.com/dguzzo/cecc2ef8b8b520af3dc40e209eadc183 – Dominick

回答

4

最佳示例位于代码附带的单元测试中。例如wand/tests/sequence_test.py

要使用魔杖创建动画gif,请记住将图像加载到序列中,然后在加载所有帧后设置附加延迟/优化处理。

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

+0

很酷,谢谢@emcconville;我应该看看那些单元测试! – Dominick