Image 分解 gif 图片

创建日期: 2022-12-01 09:46 | 作者: 风波 | 浏览次数: 13 | 分类: Python

1. 安装 pillow

pip install pillow -i https://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2. 分解 gif 格式图片

from PIL import Image
import os

def split_image(srcfile, dstdir):
    filename = srcfile.split("/")[-1]
    prefix = filename[:filename.rfind(".")]
    img = Image.open(srcfile)

    files = list()
    for i in range(img.n_frames):
        img.seek(i)
        new = Image.new("RGBA", img.size)
        new.paste(img)
        newfile = os.path.join(dstdir, "{}-{}.png".format(prefix, i)) 
        new.save(newfile)
        files.append(newfile)

    return files

if "__main__" == __name__:
    split_image("./abc.gif", "./bcd")

参考:https://blog.csdn.net/m0_38056893/article/details/103504790

13 浏览
8 爬虫
0 评论