How I created a Video from Image Sequence for my Presentation using Moviepy in Python
In my last article, I shared how I processed 833 text files to produce 833 figures in a specified folder. That wasn’t the end of that task, I had to present those figures in less than a minute and the best way is to create a video whose frame rate I can control to fit into my presentation time.
In the code described herein, I created a 15 seconds video from my 833 images using moviepy module in python.
- If you dont have moviepy module installed into your python, install it using
pip install moviepy
in your terminal or!pip install moviepy
from jupyter notebook - In the function below, we specified the arguments, obtained each of the 833 images and converted them to a video with a name and format of our choice.
def images_to_video(image_folder_path: str, fps, extension:str, video_name:str, output_format:str):
import os
import moviepy.video.io.ImageSequenceClip
images = [image_folder_path+'/'+img for img in os.listdir(image_folder_path) if img.endswith(extension)]
movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
movie_clip.write_videofile(video_name+output_format)
def images_to_video(image_folder_path:str, fps, extension:str, video_name:str, output_format:str):
defined a function that accepts the following arguments, specifying type where necessary:
- image_folder_path is the directory of folder containing the images
- fps is the number of frames per second
- extension is the format of the images we are interested in. It could be .jpeg, .tiff, .png etc depending on the image format
- video_name is the name of video file created
- output_format is the format of the created video. It could be .mp4, .avi, .mov, etc
import os
helps to creates, handle (and fetch contents from) the image directory
import moviepy.video.io.ImageSequenceClip
imports the module that will be used to create the video from the image sequence
images = [image_folder_path+'/'+img for img in os.listdir(image_folder_path) if img.endswith(extension)
creates a list of all the images with specified extension in the folder path specified. The output will be a path for each of the images, including the extension.
movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
converts the list of images fetched into a video with the specified frames per second.
movie_clip.write_videofile(video_name+output_format)
writes the created video to a file with the specified name and format.
if __name__ =='__main__':
images_to_video('/Users/.../Spectra Figures', 54, '.jpeg', 'Spectra_video', '.mp4');
The above code runs the function, the frame rate per second chosen is 54, input format is jpeg and the output format is mp4.
Ensure the video has been completely written before closing the code. This is what the output looks like when completed:
I was able to convert my 833 images to a video that runs for the number of seconds I wanted. This is what it looks like:
Hope you enjoy my content!