1
0
forked from mei/webp2gif

first commit

This commit is contained in:
mei 2025-02-10 12:33:41 +08:00
commit b7821eea69
2 changed files with 36 additions and 0 deletions

2
README.py Normal file
View File

@ -0,0 +1,2 @@
input: 输入WebP文件的目录
output: 输出GIF文件的目录

34
main.py Normal file
View File

@ -0,0 +1,34 @@
import os
from PIL import Image
def convert_webp_to_gif(input_folder, output_folder):
"""
批量将指定文件夹内的webp图片转换为gif并保存至另一文件夹
:param input_folder: 存放webp图片的文件夹路径
:param output_folder: 转换后的gif图片存放的文件夹路径
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中所有文件
for filename in os.listdir(input_folder):
if filename.endswith(".webp"):
webp_path = os.path.join(input_folder, filename)
gif_path = os.path.join(output_folder, filename[:-5] + ".gif") # 替换后缀名.webp为.gif
try:
with Image.open(webp_path) as im:
# 如果是动画WebP则需要特殊处理每一帧并保存为GIF
if hasattr(im, 'is_animated') and im.is_animated:
im.save(gif_path, save_all=True)
else:
im.convert('RGB').save(gif_path, 'GIF')
print(f"{filename} 转换成功")
except Exception as e:
print(f"{filename} 转换失败: {e}")
# 使用函数
input_dir = 'input' # 输入WebP文件目录
output_dir = 'output' # 输出GIF文件目录
convert_webp_to_gif(input_dir, output_dir)