How to Make a Video Out of Images in Python

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:

  1. 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.
  2. 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

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:

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.

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.

You can also run the command within the Python script using the os.system() function as follows.

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:

Method 4: Using the imageio package

In this case, you need to install imageio-ffmpeg using

pip install imageio-ffmpeg

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

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.