Q:

OpenCV - Making a video from existing images

I have an array of numpy array images named images. Let's say for the discussion's sake that - 
images = [cv2.imread("data/frame" + str(i) + ".jpg") for i in range(15)]

Where data is a directory with frames from a video. 
I then try to save them as a video, using the following code:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
shape = images[0].shape[:2]
vid = cv2.VideoWriter("my_vid.avi", fourcc, 1, shape)
for fg_frame in images:
    vid.write(np.uint8(fg_frame))
vid.release()

But the video that is saved is only 5-6 KB in size and plays nothing. What am I doing wrong?

A:

The reason is that the constructor of cv2.VideoWriter takes video size in the 4th argument which is expected to be a tuple of the form (width, height).
The shape member of numpy array stores dimensions as (height, width).
Due to dimension mismatch between VideoWriter and the actual image, no frames are written to the disk.
You have to swap the elements of shape when passing as an argument to VideoWriter. The correct code may look like this:
shape = images[0].shape[:2]
video_size = (shape[1], shape[0])
vid = cv2.VideoWriter("my_vid.avi", fourcc, 1, video_size)

Verified and tested on Ubuntu 14.04 with Python 3 and OpenCV 3.4.

