在python中使用imageio从图像制作gif [英] making gif from images using imageio in python

查看:792
本文介绍了在python中使用imageio从图像制作gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过在线阅读许多示例,发现imageio是它的理想之选. 也找到了此处编写的示例.

I have tried reading a lot of examples online and found imageio is the perfect package for it. Also found examples written in here.

我刚刚按照显示的示例进行操作,并尝试了以下

I have just followed the example as shown and tried the following

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秒,因此应该可以正常工作.我可能在这里错过了一些东西.

both of these do not work. And basically i only see the first frame from the gif and a blink and finish. The duration is set 0.5secs, so it should work fine. I might have been missing out something here.

推荐答案

这对我有用:

import os
import imageio

png_dir = '../saves/png/'
images = []
for file_name in 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('../saves/gif/movie.gif', images)

这篇关于在python中使用imageio从图像制作gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆