1.使用mimsave
"""使用imageio重新生成一个GIF"""
import imageio
from PIL import Image
def preProcess(image_list):
"""处理一下图片大小"""
for image_name in image_list:
im = Image.open(image_name)
im = im.resize((100, 100)) # 都搞成(100,100)尺寸的
im.save(image_name, False) # False指的是覆盖掉之前尺寸不规范的图片
def create_gif(image_list, gif_name, duration=1):
"""制作gif图"""
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=duration) # 选择'GIF'类型
if __name__ == '__main__':
# image_list放上自己所需要合成的图片名
image_list = ['1.jpeg', '2.jpeg', '3.jpeg', '4.jpeg', '5.jpeg', '6.jpeg']
# gif_name:导出的gif图名
gif_name = 'new.gif'
duration = 1 # 这里的duration单位是1秒,与PIL.Image不一样(1ms)
preProcess(image_list) # 处理一下图片尺寸
create_gif(image_list, gif_name, duration) # 开始制作
版权声明:本文为CSDN博主「小白tree」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43469047/article/details/103484755
2.使用get_writer()
import os,imageio
import numpy as np
import matplotlib.pyplot as plt
def frames2video(path):
"""
Merges images in path into a video
:param path: path with prediction images
:return: nothing
"""
fnames = os.listdir(path)
fnames.sort()
images = np.array([plt.imread(os.path.join(path, fname)) for fname in fnames])
# h, w, c = images[0].shape
videowriter = imageio.get_writer('prediction_video.gif', fps=1)
for im in images:
videowriter.append_data(im)
videowriter.close()
if __name__="__main__":
path='./face' #face文件中放着7个图片
frames2video(path)