gif图抽帧

from PIL import Image
import os

def save_gif_frames_as_images(gif_path, output_dir):
    with Image.open(gif_path) as im:
        # 获取 GIF 动画总帧数
        n_frames = im.n_frames
    
        # 遍历每一帧
        for i in range(n_frames):
            # 选择当前帧
            im.seek(i)
            
            # 构造输出文件路径
            frame_filename = f"frame_{i}.png"
            output_path = os.path.join(output_dir, frame_filename)
            
            # 保存当前帧为图片
            im.save(output_path, format='PNG')

# 调用函数保存 GIF 动画的每一帧为图片
print(os.getcwd())
save_gif_frames_as_images('1.gif', 'image')

你可能感兴趣的:(python脚本,python)