使用python把gif转为图片

使用python把gif转为图片

  • 程序思路
  • 效果
  • 代码

程序思路

  1. 打开 GIF 文件。
  2. 确保输出文件夹存在,如果不存在则创建。
  3. 获取 GIF 的帧数。
  4. 遍历每一帧,将其保存为单独的 PNG 图像,并打印保存路径。

效果

把这张派大星gif转为一张张图片:

代码

python 复制代码
from PIL import Image
import os


def gif_to_images(gif_path, output_folder):
    # 打开 GIF 文件
    gif = Image.open(gif_path)

    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取 GIF 的帧数
    frame_count = gif.n_frames

    for frame in range(frame_count):
        # 设置当前帧
        gif.seek(frame)

        # 将当前帧保存为图像
        frame_image_path = os.path.join(output_folder, f"frame_{frame}.png")
        gif.save(frame_image_path, "PNG")
        print(f"Saved {frame_image_path}")


if __name__ == "__main__":
    gif_path = "QTQBAP2Q.gif"  # 替换为你的 GIF 文件路径
    output_folder = "gif_png"  # 替换为你想保存帧图片的文件夹路径

    gif_to_images(gif_path, output_folder)
相关推荐
酷在前行3 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8283 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
MC皮蛋侠客3 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
Zane19944 小时前
@property 到底是怎么把方法伪装成属性的?一文吃透 property、staticmethod、classmethod
后端·python
qq_316411034 小时前
AI 情感陪伴智能潮玩软硬件一体化开发案例
人工智能·python
废弃的小码农4 小时前
功能测试--Day07--Python编程基础
开发语言·python
zx1154504 小时前
大模型工具调用次数限制
人工智能·python
fengci.5 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
MC皮蛋侠客5 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
程序员zgh5 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++