如何从Python中的图像目录创建视频文件

时间:2017-02-20 10:04:43

标签: python opencv

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 #

当我运行此代码时,发生以下错误

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

这是为什么?请帮帮我。

1 个答案:

答案 0 :(得分:1)

也许不能直接回答这个问题,但你确定你正在使用合适的工具吗?

我找到了this answer,它解释了如何使用ffmpeg将图片转换为视频。根据您的使用情况,您可能会考虑使用Python来组织图像并调用ffmpeg。