调整gif文件尺寸到宽480或高360的程序

调整gif文件尺寸到宽480或高360的程序

在设计幼儿编程课程中编写的一个小程序。 程序比较简单,迭代每一帧,调整后再合帧,输出为新的gif图形。

"""
调整gif文件大小
"""
from PIL import Image,ImageSequence

def resize_gif(infile,outfile):
    im = Image.open(infile)
    k = im.width/im.height
    if k > (4/3):        #  如果宽高比大于4比3则宽度设为480
        width = 480
        height = int(width / k)
    else:
        height = 360
        width = int(height * k)
    
    frames = []
    for frame in ImageSequence.Iterator(im):
        frame = frame.resize((width,height))
        frames.append(frame)
    pic = frames[0]
    pic.save(outfile, save_all=True,append_images=frames[1:], optimize=True,quality=100,duration=200)

path = 'F:\\风火轮编程\\课程体系\\L1_幼儿编程启蒙\\05_星空\\'
file = '银河系.gif'
outfile = path + '银河系x.gif'
resize_gif(path+file,outfile)

李兴球

李兴球的博客是Python创意编程原创博客