python 魔杖中的帧不会消失

Frames not disappearing in python wand

根据 的提示,我尝试创建一个包含 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'))

当前输出:

更新答案

... I got error using that ...

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

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