A video is a sequence of images (called frames). This article will discuss how to create a video from images using Python. We will discuss three methods of doing this:
- Method 1: Using the moviepy package,
- Method 2: Using the OpenCV library,
- Method 3: Using the FFmpeg command line tool and its corresponding wrapper in Python,
- Method 4: Using the imageio module and,
- Method 5: Using the skvideo package.
Each of these methods has unique features that are interesting to explore. Before we do that, however, let’s briefly define two terms that we will be using mostly:
- Frames per second (FPS) – this is the number of frames/images included in 1 second of video playback. For example, when we say a video has 30 FPS, it means that 30 images are contained in each second of the video. Most movies and video shows play at 24 or 30 FPS.
- Resolution of an image/video – This term describes the number of pixels in an image/video. The higher the resolution, the more detailed an image/video is.
The five methods discussed in this article work best when:
- Input images are of the same resolution (size). You may need to resize the images in some cases (see Method 2 to learn how to resize input images),
- Input images are of the same type, e.g., PNG, JPG, etc.
We are now ready to discuss the methods of converting images into a video using Python.
Note: Some methods use the FFmpeg utility under the hoods. This means we must install the tool before using the methods dependent on it (See Method 1).
Method 1: Using the moviepy module
The following example shows how to use the module – you need to modify the FPS value, the paths to your images, and the output video to fit your use case. You may need to install the package using pip by running:
pip install moviepy
1 2 3 4 5 6 7 8 9 10 |
# ImageSequenceClip requires all images to be the same size. # See Method 2 to learn how to resize images to the same size. from moviepy.editor import ImageSequenceClip # Initialize an ImageSequenceClip object # Define, Frames Per Second (FPS) for the output video, # and path to the images being converted to video. clip = ImageSequenceClip("../test_images", fps=30) # Write the video to a file in the path provided clip.write_videofile("output.mp4") |
If the code snippet above leads to an error like the following, you need to install FFmpeg.
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
You can install it on Linux using the APT package manager by running the following command:
sudo apt install ffmpeg
On Mac, you can use homebrew by running:
brew install ffmpeg
You can follow this link to install FFmpeg on Windows:
https://ffmpeg.org/download.html
After installing it, add the path to the environment variable by adding the following lines to the above code before importing moviepy:
1 2 |
import os os.environ["IMAGEIO_FFMPEG_EXE"] = "/path/to/ffmpeg" |
Method 2: Using the OpenCV library
If OpenCV is not already installed in your system, you can do that using pip by running:
pip install opencv-python
Important note: As stated earlier, ensure that the input images are the same size and match the size specified in the VideoWriter object.
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 |
# use "pip install opencv-python" if cv2 is not found import cv2 import glob # Get a list of PNG images on the "test_images" folder images = glob.glob("../test_images/*.png") # You can also use the following two lines to get PNG files in a folder. # import os # images = [os.path.join("../test_images", file) for file in os.listdir("../test_images") if file.endswith(".png")] # Sort images by name. Optional step. images = sorted(images) # Define codec and create a VideoWriter object # cv2.VideoWriter_fourcc(*"mp4v") or cv2.VideoWriter_fourcc("m", "p", "4", "v") fourcc = cv2.VideoWriter_fourcc(*"mp4v") video = cv2.VideoWriter( filename="output.mp4", fourcc=fourcc, fps=30.0, frameSize=(430, 430) ) # Read each image and write it to the video for image in images: # read the image using OpenCV frame = cv2.imread(image) # Optional step to resize the input image to the dimension stated in the # VideoWriter object above frame = cv2.resize(frame, dsize=(430, 430)) video.write(frame) # Exit the video writer video.release() |
Method 3: Using FFmpeg as a command line tool and as a Python package
Installation of the FFmpeg command line utility has already been discussed in Method 1. Once you install FFmpeg, you can run the following on the command line to convert images to a video.
1 |
ffmpeg -y -framerate 2 -i <path/to/images> -c:v libx264 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4 |
You can also run the command within the Python script using the os.system() function as follows.
1 2 3 4 5 6 |
import os command = ( f"ffmpeg -y -framerate 2 -i ../test_images/image_%d.png -c:v libx264 -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' output.mp4" ) os.system(command) |
If you don’t want to use the command-line tool, you can use the FFmpeg binding in Python, which can be installed using pip with the command
pip install ffmpeg-python
And used with something like this:
1 2 3 4 5 6 7 |
import ffmpeg #install ffmpeg-python ( ffmpeg .input("../test_images/*.png", pattern_type='glob', framerate=25) .output("movie.mp4") .run() ) |
Method 4: Using the imageio package
In this case, you need to install imageio-ffmpeg using
pip install imageio-ffmpeg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import imageio.v3 as imageio ## install imageio-ffmpeg import os # List of image filenames image_filenames = sorted( [file for file in os.listdir("../test_images") if file.endswith(".png")] ) # Create an empty list to hold the images images = [] # Load the images and add them to the list for filename in image_filenames: images.append(imageio.imread(os.path.join("../test_images", filename))) # Create the output video file imageio.imwrite("output1.mp4", images, fps=30) |
Method 5: Using the skvideo package
This method relies on OpenCV for loading images and NumPy to turn the image objects into a Numpy array. You can install all the packages needed with these commands
pip install sk-video
pip install opencv-python
pip install numpy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import os import skvideo.io # install sk-video import cv2 import numpy as np frames = [] for image in os.listdir("../test_images"): image_path = os.path.join("../test_images", image) img = cv2.imread(image_path) frames.append(img) # Writes the output image sequences in a video file skvideo.io.vwrite("video.mp4", np.array(frames)) |
Conclusion
This article discussed five methods of converting images into a video using Python. Each method has unique features; therefore, you may need to explore each to know what fits you.
Also, go through the packages’ documentation to know more about parameters that can be used when making videos from images.