Python简单制作GIF

发布时间:2019-08-23 07:54:44编辑:auto阅读(1287)

    # _author_ == ‘ljh’
    import imageio
    import glob
    import re
    from PIL import Image, ImageDraw, ImageFont, ImageColor

    # modify the size of the images
    def change():
    old_img_filenames = glob.glob(r’C:\Users\Jack\Desktop\old*.jpg’)
    widthlist = []
    heightlist = []
    for img_names in old_img_filenames:
    img = Image.open(img_names)
    width, height = img.size
    widthlist.append(width)
    heightlist.append(height)
    widthlist.sort()
    heightlist.sort()
    width_min = widthlist[0]
    height_min = heightlist[0]
    for i,j in enumerate(old_img_filenames):
    img = Image.open(j)
    out = img.resize((width_min,height_min),Image.ANTIALIAS)
    out.save(r’C:\Users\Jack\Desktop\new\%s.jpg’%str(i),’jpeg’)

    # look for all images needed
    def find_all_png():
    png_filenames = glob.glob(r’C:\Users\Jack\Desktop\old*.jpg’)
    buf=[]
    for png_file in png_filenames:
    buf.append(png_file)
    return buf

    #make images into a gif
    def create_gif(image_list, gif_name):
    frames = []
    for image_name in image_list:
    frames.append(imageio.imread(image_name))
    # Save them as frames into a gif
    imageio.mimsave(gif_name, frames, ‘GIF’, duration = 0.8)

    if name == ‘main‘:
    # change()
    # buff = find_all_png()
    # create_gif(buff,r’C:\Users\Jack\Desktop\xinxin.gif’ )

关键字