Yingqiang Gao
Yingqiang Gao

Reputation: 999

python opencv: cannot save the images to the video

I have a series of images and I like to create a video based on them, and I wrote the following codes:

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()

and the error was:

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

where was wrong?

Upvotes: 2

Views: 5135

Answers (1)

Jurjen
Jurjen

Reputation: 1396

You make a new Videowriter for every frame you want to write, so you are not actually adding frames to the video, but you keep making new videowriters where you add one frame. This probably results in a save error. Try this:

# 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()

See this OpenCV C++ tutorial for more info.

Upvotes: 1

Related Questions