在python中使用imageio从图像生成gif
我试着在网上阅读了很多例子,发现在python中使用imageio从图像生成gif,python,animated-gif,python-imageio,Python,Animated Gif,Python Imageio,我试着在网上阅读了很多例子,发现imageio是一个完美的软件包 我刚刚按照所示的示例进行了如下尝试 import imageio as io import os file_names = sorted((fn for fn in os.listdir('.') if fn.startswith('surface'))) #making animation with io.get_writer('surface.gif', mode='I', duration=0.5) as writer:
imageio
是一个完美的软件包
我刚刚按照所示的示例进行了如下尝试
import imageio as io
import os
file_names = sorted((fn for fn in os.listdir('.') if fn.startswith('surface')))
#making animation
with io.get_writer('surface.gif', mode='I', duration=0.5) as writer:
for filename in file_names:
image = io.imread(filename)
writer.append_data(image)
writer.close()
还有一个例子
images = []
for filename in file_names:
images.append(io.imread(filename))
io.mimsave('surface1.gif', images, duration = 0.5)
这两种方法都不起作用。基本上我只看到gif中的第一帧,然后眨眼就结束了。持续时间设置为0.5秒,因此应该可以正常工作。我可能错过了一些东西 这对我很有用:
import os
import imageio
png_dir = '../animation/png'
images = []
for file_name in sorted(os.listdir(png_dir)):
if file_name.endswith('.png'):
file_path = os.path.join(png_dir, file_name)
images.append(imageio.imread(file_path))
imageio.mimsave('../animation/gif/movie.gif', images)
万一有人需要它,
对于python 3.6.8,它需要fps
imageio.mimsave('/path/file.gif',images,fps=55)
这两种方法都适用于Python2.7.1和(),使用几个.png图像。是否确实选择了所有要使用的图像?检查文件名列表,看看它们是否都在那里。我尝试了第一种方法。它适用于大约758.png文件。但是我有大约3000个.png文件,758之后的文件没有附加到.gif文件中。有什么原因吗?我知道文件名数组中有这些文件。有关我的问题,请参阅。谢谢这是迄今为止我发现的最简单、最优雅的解决方案。一个建议是,在list_dir函数之前添加sorted函数:sorted(os.listdir(png_dir))