python imageio mp4 video from a set of png images

tommy.carstensen picture tommy.carstensen · Dec 12, 2017 · Viewed 10.3k times · Source

How do I make an mp4 video from a set of png images with the module imageio? I've tried this:

import imageio
import glob
writer = imageio.get_writer('test.mp4', fps=20)
for png_path in glob.glob('*.png'):
    im = imageio.imread(png_path),
    writer.append_data(im[:, :, 1])
writer.close()

I've also tried replacing im[:, :, 1] with im. What am I doing wrong? I'm happy to use another module.

Answer

Mr K. picture Mr K. · Dec 13, 2017

You don't need to modify the image through im[:, :, 1]. For example, the code below takes all images starting with name in a folder specified by path and creates a videofile called "test.mp4"

fileList = []
for file in os.listdir(path):
    if file.startswith(name):
        complete_path = path + file
        fileList.append(complete_path)

writer = imageio.get_writer('test.mp4', fps=20)

for im in fileList:
    writer.append_data(imageio.imread(im))
writer.close()

All images have to be the same size so you should resize them before appending them to the video file. You can modify the fps through fps, I just set it at 20 because I was following your code.