【发布时间】:2021-06-01 19:08:44
【问题描述】:
我正在尝试将具有透明背景的 PNG 图像列表转换为 GIF,同时保持背景透明度。我拿了我找到的这段代码,并对其进行了修改:
import os
from PIL import Image
# Create the frames
frames = []
path = "directory/to/my/png/images"
for frame in os.listdir(path):
new_frame = Image.open(path + "/" + frame)
frames.append(new_frame)
# Save into a GIF file
frames[0].save(path + "/../output/animation.gif", format='GIF',
append_images=frames[1:],
save_all=True,
duration=41, loop=1, transparency=0)
它正在打开我在一个文件夹中的所有 PNG 图像,并将它们导出为 GIF,但背景是黑色的。我看过PIL documentation,但我似乎不明白transparency参数是如何工作的,或者我认为我用错了。
【问题讨论】:
-
试试透明度=100 ?
-
我尝试了 0、100 和 255,但它们似乎都没有改变
-
或尝试 new_frame = Image.open(path + "/" + frame, mode='RGBA') 或 frames[0].save(path + "/../output/animation.gif ", format='GIF', append_images=frames[1:], save_all=True, duration=41, loop=1,透明度=0, mode='RGBA')
-
我不确定 PIL 默认是如何读取图像的,尝试找到它并确保默认情况下它们使用 alpha 通道(提供透明通道的通道)保存
-
open() 中的模式仅对应于读/写而不是 RGBA。我在保存中尝试了 mode='RGBA' 但也不起作用
标签: python python-imaging-library png transparency gif