【问题标题】:Frames not disappearing in python wandpython wand中的帧没有消失
【发布时间】:2018-05-17 09:02:38
【问题描述】:

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

当前输出:

【问题讨论】:

    标签: python image imagemagick imagemagick-convert wand


    【解决方案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')
    

    【讨论】:

    • 谢谢,但我在使用时遇到了错误,如here 所示。然后我尝试了所有错误建议的选项,例如合并、展平、马赛克,但它们都只是生成图像,而不是动画 gif。
    • 我想你误解了我的问题。我不想创建合并图像(使用错误描述中建议的方法之一导致合成 2 个图像:here)。我希望每个图像都是一帧动画 gif,并且动画 gif 以最大尺寸的图像作为其大小(因此它可以显示所有图像/帧)。
    • 太棒了。谢谢你。更新后的答案有效(尽管有细微的变化,d2 和 d3 r blob,而不是文件)。我希望文档对这些未实现的方法更加详细。您还可以看看另一个类似的问题here,其中 wand 会创建空帧和不规则的帧延迟?
    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多