Python无损合成GIF: 将文件夹中的PNG图像转换为GIF
要将文件夹中的PNG图像合成为无损的GIF图像,可以使用Pillow库。以下是一个示例代码,它遍历文件夹中的PNG图像,并将它们合成为一个无损的GIF图像:
from PIL import Image
import os
# 设置文件夹路径和输出文件名
folder_path = 'path/to/folder'
output_file = 'output.gif'
# 遍历文件夹中的PNG图像
images = []
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
image_path = os.path.join(folder_path, filename)
image = Image.open(image_path)
images.append(image)
# 将图像列表保存为无损的GIF图像
images[0].save(output_file, save_all=True, append_images=images[1:], optimize=False, duration=500, loop=0)
在代码中,你需要将'folder_path'替换为你的文件夹路径,将'output_file'替换为你想要保存的GIF文件名。代码将遍历文件夹中的所有PNG图像,将它们添加到一个图像列表中。最后,它使用'save()'方法将图像列表保存为无损的GIF图像。
原文地址: https://www.cveoy.top/t/topic/pgY2 著作权归作者所有。请勿转载和采集!