python修改动图gif的尺寸

 时间:2026-02-17 15:16:29

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的尺寸

  • jquery如何获取页面所有a标签的链接名称
  • Visual Studio Code怎么关闭显示选项卡
  • ASP.NET 创建Web服务并发布使用
  • 在SQL中尽量使用可变长度数据类型
  • win10安装docker教程
  • 热门搜索
    低保申请怎么写 论文题目怎么定 证明书怎么写 aaron怎么读 脸红发热是怎么回事 喝水喝多了会怎么样 普通话怎么报名 腰间盘突出怎么治疗效果好 现代瑞纳怎么样 电脑怎么连接路由器