如何从圆形遮罩图像制作gif?

2024-09-29 01:31:57 发布

您现在位置:Python中文网/ 问答频道 /正文

Download Zip with Images

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

enter image description hereenter image description hereenter image description here

通常我用这个代码来制作礼品:

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

有什么主意吗?在


Tags: pathin图像importforpngosfilename
1条回答
网友
1楼 · 发布于 2024-09-29 01:31:57

您的png文件使用透明性,因为GIF确实支持透明性,而imageio库则不是这样。在

如果蒙版区域是白色是可以的,你可以在你的循环内这样做。只需更换:

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

有:

^{pr2}$

GIF透明度有点特殊,你可以将其中一种颜色设置为透明。如果你想用Python做这件事,this thread应该会有帮助。在

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

最好的, R

相关问题 更多 >