2016-04-25 25 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