wand: 如何 assemble 透明 gif / 清除每一帧的背景
wand: How to assemble transparent gif / clear background each frame
所以我有一系列透明的 png 并将它们附加到新的 Image()
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)
很遗憾,添加新图像时背景不是 "cleared"。他们也会有最后一张图片:
但是如何清除背景呢?我虽然通过预先合成一个新图像来做到这一点..`:|哈普!!
我看到命令行 ImageMagick 有一个 similar 东西,但 wand 没有类似的东西。到目前为止,我必须使用合适的背景颜色来解决问题。
没有看到源图像,我可以假设 -set dispose background
是所需要的。对于 wand,您需要调用 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)
或者...
您可以扩展魔杖来管理背景处理行为。这种方法将以编程方式为您提供 alter/generate 每个帧的好处。但不利的一面是 ctypes 需要做更多的工作。例如。
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)
<- 仍然需要延迟修正
所以我有一系列透明的 png 并将它们附加到新的 Image()
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)
很遗憾,添加新图像时背景不是 "cleared"。他们也会有最后一张图片:
但是如何清除背景呢?我虽然通过预先合成一个新图像来做到这一点..`:|哈普!!
我看到命令行 ImageMagick 有一个 similar 东西,但 wand 没有类似的东西。到目前为止,我必须使用合适的背景颜色来解决问题。
没有看到源图像,我可以假设 -set dispose background
是所需要的。对于 wand,您需要调用 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)
或者...
您可以扩展魔杖来管理背景处理行为。这种方法将以编程方式为您提供 alter/generate 每个帧的好处。但不利的一面是 ctypes 需要做更多的工作。例如。
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)