有效的解决方案
迄今为止其他解决办法的问题:
(一)
2个)
没有解决无序目录迭代的方法,这对于GIFs是必不可少的
三)
没有解释如何为python 3安装imageio
按如下方式安装imageio:
python3-m pip安装imageio
注:
您需要确保您的帧在文件名中有某种索引,以便对它们进行排序,否则您将无法知道GIF的开始或结束位置
import imageio
import os
path = '/Users/myusername/Desktop/Pics/' # on Mac: right click on a folder, hold down option, and click "copy as pathname"
image_folder = os.fsencode(path)
filenames = []
for file in os.listdir(image_folder):
filename = os.fsdecode(file)
if filename.endswith( ('.jpeg', '.png', '.gif') ):
filenames.append(filename)
filenames.sort() # this iteration technique has no built in order, so sort the frames
images = list(map(lambda filename: imageio.imread(filename), filenames))
imageio.mimsave(os.path.join('movie.gif'), images, duration = 0.04) # modify duration as needed