Python简单制作GIF

2023-01-31 02:01:41 python 简单 制作

# _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’ )

相关文章