框架不会在python魔杖中消失

时间:2018-05-17 09:02:38

标签: python image imagemagick imagemagick-convert wand

根据here的提示,我尝试创建一个带有2个不同图像的gif,如下所示。它可以工作,但是一帧不会消失以显示另一帧。为什么会发生这种情况以及如何纠正?

from wand.image import Image as Image2

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.type = 'optimize'
    wand.save(filename='animated.gif')

display(Image('animated.gif'))

电流输出:
img

1 个答案:

答案 0 :(得分:1)

更新回答

  

......使用它时出错......

看起来硬编码的验证值不允许使用此技术。这是一个错误,我将在上游提交补丁。

@ -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')

目前,未实施我认为您需要的C-API方法MagickSetImageDisposeMagickExtentImage。虽然实现这些方法相当容易,但您可能会陷入重建每个图像的过程中 - 逐帧。

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')