Turning a series of images into a video is a common requirement in animation, data visualization, and media production. Python offers powerful libraries like OpenCV and Moviepy, making it easy to automate combining multiple images into high-quality video files.
In this article, we’ll guide you through using these libraries to create videos efficiently. Additionally, we’ll see how Cloudinary can store your images and seamlessly generate videos through its API. So let’s get started!
In this article:
- How Can You Use Python to Create Video from Images?
- Building Our Own Python Tool to Create Video From Images With OpenCV
- How to Create Video From Images With Cloudinary
- Make an Impact, Not a Slideshow
How Can You Use Python to Create Video from Images?
Creating a video from images in Python involves combining multiple image files into a video format. As mentioned above, Python provides several libraries and tools to simplify this process. Here’s some commonly used options:
- OpenCV: An open-source library offering comprehensive tools for image, video processing and computer vision. It supports various image file types, video formats and allows precise video creation and editing control, including setting the aspect ratio to ensure the video maintains the desired resolution and proportions.
- Moviepy: A user-friendly library for video editing that simplifies tasks like creating videos from images, adding audio tracks, and applying effects. It is ideal for rapid prototyping and straightforward video tasks.
- FFmpeg: A powerful command-line tool with Python bindings available. FFmpeg is renowned for its versatility and extensive format support, making it suitable for complex video processing tasks.
- Pillow: Primarily an image processing library, Pillow can be used with other tools like OpenCV or Moviepy to handle image processing before creating videos.
- Cloudinary: A cloud-based service that provides an API for managing and transforming media. With Cloudinary, you can upload images and generate videos directly from them, leveraging its powerful cloud-based processing and delivery capabilities.
Each tool offers different strengths, so you can choose the one that best suits your project’s requirements and workflows.
Building Our Own Python Tool to Create Video From Images With OpenCV
Let’s look at how to convert a series of images into a video using OpenCV.
To begin, start by importing the necessary libraries. We need OpenCV for video creation and OS for file handling. We will also need to specify the directory where your images are stored and the path where you want to save the output video:
import cv2 import os # Define input and output paths image_folder = './images/' # Replace with the path to your images video_name = 'output_video.mp4' # Name of the output video file
Next, we list all the image files in the specified directory using the OS library. We will also use OpenCV to get the dimensions of the video by reading the dimensions of the first image to obtain its size:
# Get list of images images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")] # Read the first image to get the size first_image = cv2.imread(os.path.join(image_folder, images[0])) height, width, _ = first_image.shape
Finally, create a VideoWriter
object and specify the codec (e.g.,'mp4v'
for mp4 video formats), frame rate, and resolution. Now loop through each image, read it, and write it to the video file. After completion, release the VideoWriter
object to finalize the video file:
# Initialize video writer fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' for MP4 format video = cv2.VideoWriter(video_name, fourcc, 20, (width, height)) # Add images to the video for image in images: img_path = os.path.join(image_folder, image) frame = cv2.imread(img_path) video.write(frame) # Release the video writer video.release() cv2.destroyAllWindows()
How to Create Video From Images With Cloudinary
Cloudinary is a powerful cloud-based media management platform that simplifies creating video content from still images. Its intuitive API and user-friendly interface make it accessible to users of all technical backgrounds, allowing you to generate videos without the complexities of coding or server configurations.
Let’s examine how Cloudinary makes it easy to create videos from images. For this tutorial, we’ll use images extracted from blue-sports-car.mp4
, which is in the Cloudinary demo cloud.
To convert images to a video, we will first initialize our Cloudinary API and define the images that we need to upload to the Cloudinary cloud.
import cloudinary from cloudinary import uploader import os cloudinary.config( cloud_name="your_cloud_name", api_key="your_api_key", api_secret="your_api_secret" ) # Define input and output paths image_folder = './images/' # Replace with the path to your images video_name = 'output_video.mp4' # Name of the output video file # Define image paths and public IDs image_public_ids = [img for img in os.listdir(image_folder) if img.endswith(".jpg")] tag = 'my_gif'
Here, in addition to the Cloudinary credentials, we define a path containing our images and give the output file a name. We also use OS
to iterate over the images in the image folder and add them to a list.
Next, we iterate over the public IDs of our images and upload them to the Cloudinary cloud.
# Upload images with a shared tag for public_id in image_public_ids: uploader.upload(image_folder + f"{public_id}", public_id=public_id, tags=[tag])
Finally, we use the Cloudinary uploader’s multi()
method to convert our images to an mp4 format. We then print the URL of our video on the terminal.
# Create the video result = uploader.multi(tag, format="mp4") video_url = result["url"] print(video_url)
As you can see, Cloudinary makes it very easy to convert images into video. In addition to its simplicity, Cloudinary gives you granular control over other aspects of your assets, allowing you to add transformations or other effects you may want to define.
With Cloudinary’s efficient processing and optimization capabilities, you can easily create high-quality videos from your image assets. Here is what our final video looks like:
Make an Impact, Not a Slideshow
Creating videos from images is a transformative way to present information and tell stories more dynamically than static slideshows ever could. Python offers powerful libraries that enable you to convert images into high-quality videos with precision and ease. These tools provide a range of functionalities, from simple image sequencing to complex video editing, catering to various needs and expertise levels.
For those looking to streamline their video creation process even further, Cloudinary offers an excellent solution. Cloudinary’s cloud-based service not only simplifies the process of creating videos from images through its robust API but also handles video storage, optimization, and delivery with remarkable efficiency.
So are you ready to elevate your video creation workflow? Create an account today and discover how effortless and powerful video production can be.
More from Cloudinary:
Reducing the Size of Animated GIFs and Converting Them to WebM or MP4 Through Automation