python修改动图gif的尺寸

2025-12-22 15:48:49

1、打开命令行窗口,使用pip安装pillow包,命令如下:

2、找一个gif图放在c盘根目录

python修改动图gif的尺寸

3、打开Python开发工具IDLE,新建‘changeGif.py’文件,将gif的每一帧取出,保存成一张张图片,这里用png格式,也可以用jpg但是

jpg需要转换一次,代码如下:

from PIL import Image,ImageSequence

import os

gifPath = 'c:\\t.gif'

oriGif = Image.open(gifPath)

lifeTime = oriGif.info['duration']

imgList = []

for i in ImageSequence.Iterator(oriGif):

    print (i.copy())

    imgList.append(i.copy())

for index,f in enumerate(imgList):

     f.save("c:\\gif\\%d.png" % index)

python修改动图gif的尺寸

4、运行代码后,在上面保存的文件夹,c盘根目录的gif文件夹内生成了很多png图片,就是gif图的每帧图片。

python修改动图gif的尺寸

5、将每帧图片缩小尺寸后,再次合成gif,代码如下:

from PIL import Image,ImageSequence

import os

gifPath = 'c:\\t.gif'

oriGif = Image.open(gifPath)

lifeTime = oriGif.info['duration']

imgList = []

imgNew = []

for i in ImageSequence.Iterator(oriGif):

    print (i.copy())

    imgList.append(i.copy())

for index,f in enumerate(imgList):

     f.save("c:\\gif\\%d.png" % index)

     img = Image.open("c:\\gif\\%d.png" % index)

     

     img.thumbnail((200,100),Image.ANTIALIAS)

     imgNew.append(img)

imgNew[0].save("c:\\new.gif",'gif',save_all=True,append_images=imgNew[1:],loop=0,

               duration=lifeTime)

python修改动图gif的尺寸

6、F5运行代码,打印出每帧图片信息

python修改动图gif的尺寸

7、在c盘根目录生成了‘new.gif’文件,缩小版的源文件

python修改动图gif的尺寸

python修改动图gif的尺寸

python修改动图gif的尺寸

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢