gpt4 book ai didi

python - 如何从圆形蒙版图像制作 gif?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:29 27 4
gpt4 key购买 nike

Download Zip with Images

我有一个包含圆形蒙版 png 图像的文件夹:

enter image description here enter image description here enter image description here

通常我用这段代码来制作gif:

import imageio
import os

imglist = []
images = []
path = ('\\').join(__file__.split('\\')[:-1]) + '\\' + 'images\\'
for r, _, f in os.walk(path):
for i in f:
imglist.append(r + i)

for filename in imglist:
if filename.endswith('.png'):
images.append(imageio.imread(filename))

frames_per_second = 24
gifpath = ('\\').join(__file__.split('\\')[:-1]) + '\\' + 'GIF.gif'
imageio.mimsave(gifpath, images, duration = 1/frames_per_second)

这适用于普通图像,但它似乎忽略了蒙版图像。 gif 看起来像这样:

enter image description here

知道如何制作圆形 gif 吗?

最佳答案

你的 png 文件使用了透明度,因为 GIF 确实支持透明度,imageio 库不是这种情况。

如果 mask 区域为白色没问题,您可以在循环内执行此操作。只需替换:

for filename in imglist:
if filename.endswith('.png'):
images.append(imageio.imread(filename))

与:

for filename in imglist:
if filename.endswith('.png'):
tmp_image = imageio.imread(filename)
mask = (tmp_image[:,:,3] == 0) #where transparency channel is 0
tmp_image[mask] = [255,255,255,255] #Set those pixels to white (other color if you prefer)
images.append(tmp_image)

GIF透明度有点特殊,你设置其中一种颜色渲染为透明。如果你想用 Python 来做 this thread应该有帮助。

我还建议学习一些如何直接使用 ffmpeg 制作 gif。一开始学习起来有点困难,但它是所有视频/gif 库在幕后使用的。

最好的,

关于python - 如何从圆形蒙版图像制作 gif?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547730/

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