【问题标题】:How can I create a video file from a directory of images in Python如何从 Python 中的图像目录创建视频文件
【发布时间】:2017-07-09 13:19:39
【问题描述】:
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

这是为什么?请帮帮我。

【问题讨论】:

  • 该错误表明图像文件可能已损坏或不存在

标签: python opencv


【解决方案1】:

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

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

【讨论】:

  • 谢谢!我会试试的。
  • 不客气 :) 另外:您可以使用例如shellsubprocess 从 Python 中调用其他程序和函数,以将所有内容保存在一个脚本中。跨度>
猜你喜欢
  • 2010-09-20
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多