import imageio
import os
from PIL import Image
import os.path
import glob
def create_gif(image_list, gif_name, duration=0.35):
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
return
def convertjpg(jpgfile,outdir,width=124,height=124):
img=Image.open(jpgfile)
try:
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
def main(image_list):
gif_name = r'D:\cc\xx.gif'
duration = 1
create_gif(image_list, gif_name, duration)
if __name__ == '__main__':
filelist = []
path = r'D:\制作gif'
new_path = r'D:\cc'
for name in os.listdir(path):
if name.endswith('png') or name.endswith('jpg'):
file = os.path.join(path, name)
convertjpg(file, new_path)
new_file = os.path.join(new_path, name)
filelist.append(new_file)
main(filelist)