2016-04-25 90 views
1

所以我有一个系列透明PNG图像,并将它们添加到一个新的图像()魔杖:如何安装透明的gif /清晰的背景每一帧

with Image() as new_gif: 
    for img_path in input_images: 
     with Image(filename=img_path) as inimg: 
      # create temp image with transparent background to composite 
      with Image(width=inimg.width, height=inimg.height, background=None) as new_img: 
       new_img.composite(inimg, 0, 0) 
       new_gif.sequence.append(new_img) 
    new_gif.save(filename=output_path) 

遗憾的背景是不是“清除”当新图像被追加。他们将有最后的图像有作为:

enter image description here

但是我怎么清除背景?我虽然我通过合成一个新的图像前期完成。`:| HALP!

我看到有一个similar东西与命令行ImageMagick但魔杖没有这样的东西。到目前为止,我必须用适合的背景色来解决问题。

回答

2

没有看到源图像,我可以假设-set dispose background是需要的。对于,您需要拨打wand.api.library.MagickSetOption方法。

from wand.image import Image 
from wand.api import library 

with Image() as new_gif: 
    # Tell new gif how to manage background 
    library.MagickSetOption(new_gif.wand, 'dispose', 'background') 
    for img_path in input_images: 
     library.MagickReadImage(new_gif.wand, img_path) 
    new_gif.save(filename=output_path) 

Assembled transparent GIF

或可替代...

您可以程度魔杖管理背景处置行为。这种方法将以编程方式为您提供alter/generate每帧的好处。但缺点是会包含更多的工作。例如。

import ctypes 
from wand.image import Image 
from wand.api import library 

# Tell python about library method 
library.MagickSetImageDispose.argtypes = [ctypes.c_void_p, # Wand 
              ctypes.c_int] # DisposeType 
# Define enum DisposeType 
BackgroundDispose = ctypes.c_int(2) 
with Image() as new_gif: 
    for img_path in input_images: 
     with Image(filename=img_path) as inimg: 
      # create temp image with transparent background to composite 
      with Image(width=inimg.width, height=inimg.height, background=None) as new_img: 
       new_img.composite(inimg, 0, 0) 
       library.MagickSetImageDispose(new_img.wand, BackgroundDispose) 
       new_gif.sequence.append(new_img) 
    # Also rebuild loop and delay as ``new_gif`` never had this defined. 
    new_gif.save(filename=output_path) 

With MagickSetImageDispose < - 仍然需要延迟补偿

+0

嗯不起作用。我想我删除了原始图像:/但是,如果您查看我发布^的图像,例如通过'xnview',您可以逐步浏览单个图像** Shift + PgDown/PgUp **,并且看到它们实际上没有轨道存储!我首先想到的是它们是如何放在一起,而不是它们如何渲染...... – ewerybody

+0

当然,它全部与命令行imageMagick和'-dispose background'一起使用。哦,该死。我希望它变得很好,pythonic ......:| – ewerybody

+0

啊!我知道了。我们需要直接通过C-API构建动画 – emcconville