python opencv:无法将图像保存到视频中

时间:2017-04-11 09:07:43

标签: python opencv

我有一系列图片,我喜欢根据它们制作视频,我写了以下代码:

import cv2
img=[]
for i in range(1,285):
    img.append(cv2.imread(str(i)+'.png'))

for j in range(1,285):
    height,width,layers=img[j].shape
    video=cv2.VideoWriter('video.avi',-1,1,(width,height))
    video.write(img[j])

cv2.destroyAllWindows()
video.release()

,错误是:

AVF: AVAssetWriter status: Cannot Save
mMovieWriter.status: 3. Error: Cannot Save

哪里错了?

1 个答案:

答案 0 :(得分:1)

您为要编写的每个帧创建一个新的Videowriter,因此您实际上并没有在视频中添加帧,但是您继续制作新的视频写入器,您可以在其中添加一个帧。这可能会导致保存错误。试试这个:

# Create the videowriter with the right parameters
height , width , layers =  img[0].shape
video=cv2.VideoWriter('video.avi',-1,1,(width,height))

# Loop through all you frames and add each frame to the video
for j in range(1,285):    
    video.write(img[j])

# Cleanup and save video
cv2.destroyAllWindows()
video.release()

有关详细信息,请参阅this OpenCV C++ tutorial