Q:

How can I create a video file from a directory of images in Python

import cv2
import cv2.cv as cv
import glob
import numpy as np
from PIL import Image
filelist = glob.glob('./*.jpg')

isColor = 1
fps     = 25  # or 30, frames per second
frameW  = 256 # images width
frameH  = 256 # images height
writer = cv2.VideoWriter('test1.avi',-1, 25.0, (640,480))

#-----------------------------
#Writing the video file:
#-----------------------------

nFrames = 10; #number of frames
for i in range(nFrames):
    img = np.array([np.array(Image.open(i)) for i in filelist])
#img = cv.LoadImage("image%d.jpg"%i) #specify filename and the extension
#add the frame to the video
#cv2.cv.WriteFrame(writer,img)
    writer.write(img)

VideoWriter = None #

when I run this code the following error occurs
Traceback (most recent call last):
  File "C:/Users/Buddhi/Desktop/New folder/Create Mp4.py", line 24, in <module>
    writer.write(img)
error: ..\..\..\..\opencv\modules\core\src\matrix.cpp:524: error: (-215) dims <= 2 in function cv::Mat::operator struct _IplImage

Why is this? Please help me.

A:

Maybe not a direct answer to the question, but are you sure you're using the right tool for the job?
I've found this answer, which explains how you can use ffmpeg to convert images into a video. Depending on your use case, you might consider using Python to organize the images and call ffmpeg, of course.

