2017-02-20 41 views
0
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 # 

一个图片目录中的视频文件,当我运行这段代码出现下列错误我如何可以创建在Python

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

错误表示图像文件可能已损坏或不存在 – ZdaR

回答

0

也许不是问题的直接答案,但是您确定自己使用的是正确的工具吗?

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

+0

谢谢!我会尝试。 – user7592296

+0

不客气:)作为补充:您可以使用例如'shell'或'subprocess'在Python内调用其他程序和函数,以便将所有内容保存在一个脚本中。 – Nietvoordekat