打开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)
将每帧图片缩小尺寸后,再次合成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)