更新答案
...我使用那个时出错了...
看起来硬编码的验证值不允许利用这种技术。这是一个错误,我将在上游提交一个补丁。
@ -2548,7 +2548,7 @@ class BaseImage(Resource):
.. versionadded:: 0.4.3
"""
- if method not in ['merge', 'flatten', 'mosaic']:
+ if method not in IMAGE_LAYER_METHOD:
raise TypeError('method must be one of: merge, flatten, mosaic')
目前,wand 没有实现 C-API 方法 MagickSetImageDispose 或 MagickExtentImage,我相信这是您需要的。虽然实现这些方法相当容易,但您可能会被困在逐帧重建每个图像。
from wand.image import Image as Image2
from wand.color import Color
from wand.compat import nested
with nested(Image2(),
Image2(filename='d2.gif'),
Image2(filename='d3.gif')) as (wand, one, two):
width = max(one.width, two.width)
height = max(one.height, two.height)
# Rebuild images with full extent of frame
with Image2(width=width, height=height, background=Color('WHITE')) as f1:
f1.composite(one, 0, 0)
wand.sequence.append(f1)
with Image2(width=width, height=height, background=Color('WHITE')) as f2:
f2.composite(two, 0, 0)
wand.sequence.append(f2)
# Create progressive delay for each frame
for cursor in range(2):
with wand.sequence[cursor] as frame:
frame.delay = 100
wand.type = 'optimize'
wand.save(filename='animated.gif')
原始答案请勿使用!
你想调用wand.image.Image.merge_layers方法,而不是wand.image.Image.type属性。
试试下面...
with Image2() as wand:
# Add new frames into sequance
with Image2(blob=d2) as one:
wand.sequence.append(one)
with Image2(blob=d3) as two:
wand.sequence.append(two)
# Create progressive delay for each frame
for cursor in range(2):
with wand.sequence[cursor] as frame:
frame.delay = 100
# Set layer type
wand.merge_layers('optimize') # or 'optimizeimage', or 'composite'
wand.save(filename='animated.gif')