My Coding > Programming language > Python > Exercise > Python: How to make video from images

Python: How to make video from images

step by step making video from images

Very common problem, convert series of images, taken for example with some interval, into a video. Of course, it is possible to use some third party programs, but today I will explain how to make video from images with python scripts.

Libraries for making video from images

Numpy - it is necessary to represent image an a numpy array for adding to video-library

CV2 - OpenCV (Open Source Computer Vision Library) is an open-source library that includes several hundreds of computer vision algorithms, and one of them is an algorithm of making video.

PIL – Pillow library to working with existing images


import numpy as np
import cv2 
from PIL import Image, ImageDraw

Let’s assume, that all our file names are stored in the list data and this list already sorted by the time of images should linked to the video. How to read content of the directory into a list I will tell somewhere else.

I will not do any checks, to be sure that files are exists and all images are the same size etc. I will assume, that all given files are perfect

Identify image size and create video data

Let’s read first image into a PIL object and check size of this object.


# read first image to PIL object
imP = Image.open(data[0])
# read image size to W and H variables
w, h = imP.size
# store this information in the tuple 
videodims = (w, h)
# this code can be shorten
videodims = (Image.open(data[0]).size)

Now we need to select codec for making video. I prefer to use mp4v codec. And we need to think about preferable Frame Per Second rate for our video, let’s use 30, which is pretty common


fourcc = cv2.VideoWriter_fourcc(*"mp4v")
FPS = 30

And finally, it is time to initialize our video object


video = cv2.VideoWriter('output.mp4', fourcc, FPS, videodims)

Reading and combining images into a video

We will read every image and add it to video data structure


# cycle trough all images. We need index just to make nice output about progress
for fidx, f in enumerate(data):
    # Performace monitor – in the same line we will print % of completeness and current file
    print( f'Done: {round(fidx * 100 / len(data), 1)} % - {f}', end="\r")
    # Now reading current image with PIL library
    img = Image.open(f)
    # for writing, we need to convert this image to numpy array and specify colour sheme 
    video.write(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
# after all images are stored in video file we need to write proper file end marks
video.release()

Very simple script

Full python script to make video from images

All code in one place:


import numpy as np
import cv2 
from PIL import Image, ImageDraw

data = ['file_1.jpg','file_2.jpg','file_3.jpg','file_4.jpg','file_5.jpg','file_6.jpg']
videodims = (Image.open(data[0]).size)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
FPS = 30
video = cv2.VideoWriter('output.mp4', fourcc, FPS, videodims)

for fidx, f in enumerate(data):
    print( f'Done: {round(fidx * 100 / len(data), 1)} % - {f}', end="\r")
    img = Image.open(f)
    video.write(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
video.release()


Published: 2022-05-06 03:26:09

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.