本文主要是介绍python多张PNG生成GIF动图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装:
pip install imageio
具体代码
import os
import imageiodef create_gif(image_list, gif_name):frames = []for image_name in image_list:if image_name.endswith('.png'):print(image_name)frames.append(imageio.imread(image_name))# Save them as frames into a gifimageio.mimsave(gif_name, frames, 'GIF', duration = 0.4) # duration 是帧时长returndef main():path=r'/home/xxx/eval/plots/'#存放PNG图片文件夹位置gif_name = 'curve_gif.gif'#生成gif的名称files = os.listdir(path)#files.sort()files.sort(key = lambda x:int(x[4:-6]), reverse=True)files = [i for i in files if '0.png' in i]print('included files: ', files)image_list=[os.path.join(path,img) for img in files]create_gif(image_list, gif_name)if __name__ == "__main__":main()
这篇关于python多张PNG生成GIF动图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!