python制作动图
程序员文章站
2022-03-09 20:17:50
...
import imageio
import os
def create_gif(image_list, gif_name, d=1.0):
'''
:param image_list: list of pic name
:param gif_name: gif name
:param d: time space between two pic
:return:
'''
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=d)
return
def main():
# image list
image_path = './pic/'
files = os.listdir(image_path)
image_list = []
for i in files:
image_list.append(image_path+i)
# sort number
image_list.sort(key=lambda x: int(x[9:-5]))
gif_name = 'new.gif'
duration = 0.05
create_gif(image_list, gif_name, duration)
if __name__ == '__main__':
main()
记得修改22行跟28行