gpt4 book ai didi

python - 魔杖:如何在每帧中组装透明 gif/清晰背景

转载 作者:行者123 更新时间:2023-12-01 03:59:34 24 4
gpt4 key购买 nike

所以我有一系列透明的 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)

不幸的是,当附加新图像时,背景并未“清除”。他们也会在那里看到最后一张图片:

enter image description here

但是如何清除背景呢?我想我是通过预先合成一个新图像来做到这一点的。`:|哈普!!

我看到有一个 similar命令行 ImageMagick 的东西,但 wand 没有类似的东西。到目前为止,我必须使用合适的背景颜色来解决问题。

最佳答案

在没有看到源图像的情况下,我可以假设 -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

或者...

您可以扩展魔杖来管理后台处理行为。这种方法将为您带来以编程方式更改/生成每个帧的好处。但缺点是需要更多的工作 。例如。

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 <- 仍然需要延迟修正

关于python - 魔杖:如何在每帧中组装透明 gif/清晰背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36839428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com