Files
release/logo/yuantu/gif_zip.py

35 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from PIL import Image, ImageSequence
#pip install pillow
def resize_gif(input_path, output_path, width, height):
"""按指定宽高重生成 GIF保持多帧"""
img = Image.open(input_path)
frames = []
for frame in ImageSequence.Iterator(img):
frame = frame.convert("RGBA")
frame = frame.resize((width, height), Image.Resampling.LANCZOS)
frames.append(frame)
# 保存为 GIF
frames[0].save(
output_path,
save_all=True,
append_images=frames[1:],
duration=img.info.get("duration", 100),
loop=img.info.get("loop", 0),
disposal=2,
transparency=img.info.get("transparency")
)
print(f"生成成功: {output_path}")
if __name__ == "__main__":
width = 100 # 你想要的宽
height = 100 # 你想要的高
for file_name in os.listdir("."):
if file_name.lower().endswith(".gif"):
output_name = f"new/{file_name}"
resize_gif(file_name, output_name, width, height)