1

I am writing a function to create a video from images in a folder. I am able to do it separately for grayscale or color but unable to generalize. Could you please help? Here is what I have written till now for grayscale images:

import cv2
import os

def images_to_video(images_folder, output_video_path, fps):
    images = [img for img in os.listdir(images_folder)]
    images.sort()
    frame = cv2.imread(os.path.join(images_folder, images[0]))
    height, width = frame.shape
    video = cv2.VideoWriter(output_video_path, 0, fps, (width,height))
    for image in images:
        video.write(cv2.imread(os.path.join(images_folder, image)))
    cv2.destroyAllWindows()
    video.release()

This question helped a bit but I need to generalize to grayscale/color and my frame rate is static (not variable).

1 Answers1

0

You just need to add a variable for channel. It will not be used actually. It will just take value of 1 for grayscale and 3 for color image.

import cv2
import os

def images_to_video(images_folder, output_video_path, fps):
    images = [img for img in os.listdir(images_folder)]
    images.sort()
    frame = cv2.imread(os.path.join(images_folder, images[0]))
    height, width, channels = frame.shape
    video = cv2.VideoWriter(output_video_path, 0, fps, (width,height))
    for image in images:
        video.write(cv2.imread(os.path.join(images_folder, image)))
    cv2.destroyAllWindows()
    video.release()
Abhi25t
  • 3,703
  • 3
  • 19
  • 32