2017-02-20 49 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