Convert Images (PNG, JPG) to Video Using Python OpenCV – Python OpenCV Tutorial

By | March 19, 2021

We can use python moviepy to convert images to a video file. Here are tutorials:

Python MoviePy Convert Images (PNG, JPG) to Video – Python MoviePy Tutorial

Python MoviePy Convert Different Size Images (PNG, JPG) to Video – Python MoviePy Tutorial

However, the converted effect is not good. Fortunately, we also can use python opencv to convert images to a video file. In this tutorial, we will introduce how to do.

Preliminary

In order to conver to images to video using python opencv, you should resize all images to the same width and height. Otherwsie, you may get error.

Resize image width and height

We will use python opencv to resize all images to make them same size. Here is a function.

def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
 
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
 
    return img_array, (_width, _height)

Of course, you also can use python pillow to resize all images.

Best Pracice to Python Resize Images with Pillow – Python Tutorial

Convert images to video using python opencv

After having resizing all images, we can convert all images to a video file. Here is a function.

def images_to_video(images, video_file):
    # make all image size is same
    img_array = []
    for i in images:
        img = cv2.imread(i)
        if img is None:
            continue
        img_array.append(img)
    img_array, size = resize(img_array, 'largest')
    fps = 1
    out = cv2.VideoWriter(video_file, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
 
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()

As to images_to_video() function, it will merge some images and create a video_file.

Here is the full example code:

# coding: utf-8
import cv2
 
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
 
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
 
    return img_array, (_width, _height)
 
def images_to_video(images, video_file):
    # make all image size is same
    img_array = []
    for i in images:
        img = cv2.imread(i)
        if img is None:
            continue
        img_array.append(img)
    img_array, size = resize(img_array, 'largest')
    fps = 1
    out = cv2.VideoWriter(video_file, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
 
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()

files = ['1.png', '2.png', '3.png', '4.png']
images_to_video(files, 'video.avi')

Run this code, you may get this result:

Convert Images (PNG, JPG) to Video Using Python OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *