pythonopencv

Python creating video from images using opencv


I was trying to create a video to show the dynamic variation of the data, like just continuously showing the images one by one quickly, so I used images (the images just called 1,2,3,4,.....) and wrote the following code:

import cv2
import numpy as np

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

height,width,layers=img[1].shape

video=cv2.VideoWriter('video.avi',-1,1,(width,height))

for j in range(0,5):
    video.write(img)

cv2.destroyAllWindows()
video.release()

and a error was raised:

TypeError: image is not a numpy array, neither a scalar

I think I used the list in a wrong way but I'm not sure. So where did I do wrong?


Solution

  • You are writing the whole array of frames. Try to save frame by frame instead:

    ...
    for j in range(0,5):
      video.write(img[j])
    ...
    

    reference