Python分离GIF动画成为多帧图像

阅读 44

2023-06-09


首先需要使用命令pip install pillow安装Python扩展库pillow。

from PIL import Image
import os
gifFileName = 'test.gif'
#使用Image模块的open()方法打开gif动态图像时,默认是第一帧
im = Image.open(gifFileName)
pngDir = gifFileName[:-4]
#创建存放每帧图片的文件夹
os.mkdir(pngDir)
try:
    while True:
        #保存当前帧图片
        current = im.tell()
        im.save(pngDir+'\\'+str(current)+'.png')
        #获取下一帧图片
        im.seek(current+1)
except EOFError:
    pass

“Python小屋”进入公众号,关注后可以查看更多内容!

欢迎转发给您的朋友,或许这正是Ta需要的知识!

精彩评论(0)

0 0 举报