Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用imageio从图像生成gif_Python_Animated Gif_Python Imageio - Fatal编程技术网

在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))