35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
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)
|