Welcome folks today in this blog post we will be creating video from multiple images
with custom fps
and duration
in command line. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries
using the below command
pip install opencv-python
pip install numpy
After installing this libraries you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 |
import cv2 import numpy as np import os # Set the duration of each image in the video (in seconds) image_duration = 5 # Set the frame rate of the video (frames per second) fps = 30 # Set the directory containing the images image_dir = 'input' |
As you can see in the above code we are importing
the opencv and numpy library. And then we are setting the variables
for the duration
of the image in seconds. Here we are given 5
so that each image will stay inside the video for 5 seconds. And then we are setting the fps
of the video to be 30. And then we are setting the image
directory path. Here you need to store all the png
or jpg
images.
Now we will be getting all
the images which are stored inside the directory called input
for this we will be using the for
loop to get all the images paths
and store it inside an array and then we are calling the sort()
method to sort the images.
1 2 3 4 |
image_paths = [os.path.join(image_dir, f) for f in os.listdir(image_dir)] # Sort the images by file name image_paths.sort() |
And now we will be setting the width
and height
of the generated video
file and then we are setting the codec
of the video.
1 2 3 4 5 6 |
# Set the size of the output video width = 1280 height = 720 # Set the fourcc code for the video codec fourcc = cv2.VideoWriter_fourcc(*'MJPG') |
And now we will be generating the video
using the filename
and then we will be calculating the frames
inside the video as shown below
1 2 3 4 5 |
# Create a VideoWriter object for the output video out = cv2.VideoWriter('video.avi', fourcc, fps, (width, height)) # Calculate the number of frames to generate for each image num_frames = fps * image_duration |
Now we will be using the for
loop to iterate over all the images and convert each image to frame
inside the video for 5
seconds as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Iterate through the images, converting each to a series of frames in the video for image_path in image_paths: # Load the image image = cv2.imread(image_path) # Resize the image to the desired size image = cv2.resize(image, (width, height)) # Write the image to the video for the specified number of frames for i in range(num_frames): out.write(image) # Release the VideoWriter object out.release() |
And now wrapping
it up this is the full source code of the app.py
file
app.py
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 numpy as np import os # Set the duration of each image in the video (in seconds) image_duration = 5 # Set the frame rate of the video (frames per second) fps = 30 # Set the directory containing the images image_dir = 'input' # Get a list of the images in the directory image_paths = [os.path.join(image_dir, f) for f in os.listdir(image_dir)] # Sort the images by file name image_paths.sort() # Set the size of the output video width = 1280 height = 720 # Set the fourcc code for the video codec fourcc = cv2.VideoWriter_fourcc(*'MJPG') # Create a VideoWriter object for the output video out = cv2.VideoWriter('video.avi', fourcc, fps, (width, height)) # Calculate the number of frames to generate for each image num_frames = fps * image_duration # Iterate through the images, converting each to a series of frames in the video for image_path in image_paths: # Load the image image = cv2.imread(image_path) # Resize the image to the desired size image = cv2.resize(image, (width, height)) # Write the image to the video for the specified number of frames for i in range(num_frames): out.write(image) # Release the VideoWriter object out.release() |
Now if you execute the above python
script in the terminal as shown below
python app.py