Creating a Video from Images in a folder using OpenCV in Python
The code reads frames from a folder containing images, resizes each frame to match the specified output video dimensions, and adds them to a new video file. The output video's frame rate and video codec are set, and the output video file is saved to a specified location. The code loops through each image file in the input folder, reads and resizes the image, and writes it to the output video using the cv2.VideoWriter() function. Finally, the `VideoWriter` object is released and all windows are destroyed.
Let us look at an example to read all images from a folder and create a video file:
Code 1-1 Read all images from a folder and then create a video file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import cv2
import os
# Set input folder, output file name, and output folder
input_folder = 'D:\\img\\videos\\frames\\'
output_file = 'output.mp4'
output_folder = 'D:\\img\\videos\\'
# Get the list of image files in the input folder
image_files = os.listdir(input_folder)
# Get the dimensions of the first image
first_image_path = os.path.join(input_folder, image_files[0])
first_image = cv2.imread(first_image_path)
height, width, channels = first_image.shape
# Set the output video dimensions, frame rate, and codec
fps = 30
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# Create a VideoWriter object
output_path = os.path.join(output_folder, output_file)
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# Loop through each image file in the input folder and add it to the output video
for image_file in image_files:
# Read the image
image_path = os.path.join(input_folder, image_file)
image = cv2.imread(image_path)
# Resize the image to match the output video dimensions
resized_image = cv2.resize(image, (width, height))
# Write the image to the output video
out.write(image)
# Display the frame
cv2.imshow("Frame", image)
# Wait for a key event
if cv2.waitKey(1) == ord('q'):
break
print('Processing Completed.')
# Release the VideoWriter object and destroy all windows
out.release()
cv2.destroyAllWindows()
|
Line 5-7: We set the input and output folder paths.
Line 10 :Using the `os.listdir()` function we get a list of image files in the input folder.
Line 13-15 :Reads the first image and then extracts its dimensions using the `.shape` attribute and stores them in the `height`, `width`, and `channels` variables.
Line 18: Set the output video frame rate to 30 frames per second. It is pretty standard to show 30 frames in one second.
Line 19 :Set the output video codec to `'mp4v'`. `VideoWriter_fourcc` is a function in the OpenCV library that is used to set the codec for the video file that is being written. The function takes a four-character code as a parameter, which represents the codec to be used for video compression. The four characters represent the encoding type, and they are usually specific to a particular video codec. For example, `'mp4v'` is the four-character code for the MPEG-4 Video codec.
Line 23 :Create a `VideoWriter` object with the output file path, codec, frame rate, and dimensions of the output video as arguments.
Line 26-42: We loop through each image file in the input folder and perform the following steps:
a. Read the image using the cv2.imread() function and store it in the image variable.
b. Resize the image to match the output video dimensions using the cv2.resize() function and store it in the resized_image variable.
c. Write the resized image to the output video using the out.write() function.
d. If user presses "q" then stop processing
Line 45: After looping through all the image files, we release the VideoWriter object using the out.release() function.
Line 46 : Finally, we destroy all windows using the cv2.destroyAllWindows() function.
The code reads frames from a folder containing images, resizes each frame to match the specified output video dimensions, and adds them to a new video file. The output video's frame rate and video codec are set, and the output video file is saved to a specified location. The code loops through each image file in the input folder, reads and resizes the image, and writes it to the output video using the `cv2.VideoWriter()` function. Finally, the `VideoWriter` object is released and all windows are destroyed.
Figure 1: From images in the input folder create a video and write to output folder.