简体   繁体   中英

Python's imageio module doesn't return more than one frame

I've the following code about the imageio Python library, which loads 2 images I have from the current folder, replaces all the colors > 200 with 0 (making it darker), and then printing the result to a new .gif image:

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

The problem is that the generated image contains only 1 frame, only 1 image, not both of the images which I already "merged" in a surface1.gif. Why is that?

Using the get_reader and get_writer objects you can do it like this:

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

I tested it and works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM