需要将推理出的图片做成GIF,发现好多中文网站都是收费的,只好自己研究一下,实现GIF制作功能,(AI辅助)具体代码如下:
from PIL import Image
import os
def make_gif(folder_path, output_path, duration, width, height):
# 获取文件夹内所有图像文件
images = []
for file_name in os.listdir(folder_path):
if file_name.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'gif','JPG')):
file_path = os.path.join(folder_path, file_name)
img = Image.open(file_path)
# 调整图像大小
img = img.resize((width, height), Image.ANTIALIAS)
images.append(img)
# 检查是否有图像存在
if not images:
raise ValueError("该文件夹内没有图像文件。")
# 将图像保存成一个GIF文件
images[0].save(
output_path,
save_all=True,
append_images=images[1:],
duration=duration,
loop=0
)
# 使用例子
folder_path = "pres" # 替换为包含图像的文件夹路径
output_path = "output1.gif" # 生成的 GIF 文件路径
duration = 300 # 每帧的显示时长(以毫秒计)
width = 800 # 目标宽度
height = 600 # 目标高度
make_gif(folder_path, output_path, duration, width, height)
得到的结果如下图: