Python的imageio模块不会返回多个帧

时间:2017-04-22 12:29:38

标签: python image image-processing python-imageio

我有关于imageio Python库的以下代码,它从当前文件夹中加载了2张图像,替换了所有颜色> 200与0(使其更暗),然后将结果打印到新的.gif图像:

import imageio
import numpy as np

im = 'image1.png'
im2 = 'image2.png'
images = []
images.append(imageio.imread(im))
images.append(imageio.imread(im2))
imageio.mimsave('surface1.gif', images, duration = 0.5)

im4 = imageio.imread('surface1.gif')
im4[im4 > 200] = 0
imageio.imwrite('movie.gif', im4, format='gif')

问题是生成的图像只包含1帧,只包含1张图像,而不是我已经合并过的两张图像"在surface1.gif中。那是为什么?

1 个答案:

答案 0 :(得分:0)

使用get_readerget_writer对象,您可以这样做:

import imageio
import numpy as np

im = 'image1.png'
im2 = 'image2.png'
images = []
images.append(imageio.imread(im))
images.append(imageio.imread(im2))
imageio.mimsave('surface1.gif', images, duration = 0.5)

im4 = imageio.get_reader('surface1.gif')
writer = imageio.get_writer('movie.gif', duration = 0.5)
for im in im4:
    im[im > 200] = 0
    writer.append_data(im[:, :, :])
writer.close()  

我测试了它并按预期工作。