【OpenCV】How to convert png/jpg format images to mp4 video files in Python

English pages
スポンサーリンク

Hello, I’m Yuki (@engieerblog_Yu), studying data science in the Visualization Laboratory.

This time, I had a chance to connect images to make a video for my research, so I’ll leave a log.

スポンサーリンク

Installation

pip3 install opencv-python

For those who got ‘No module named skbuild’, upgrading pip will work.

pip3 install -U pip

Code to convert image to mp4

import glob

import cv2

import re

def atoi(text):
    return int(text) if text.isdigit() else text

def natural_keys(text):
    return [ atoi(c) for c in re.split(r'(\d+)', text) ]

img_array = []
for filename in sorted(glob.glob("images/*.png"), key=natural_keys):
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

name = 'project.mp4'
out = cv2.VideoWriter(name, cv2.VideoWriter_fourcc(*'mp4v'), 5.0, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

At the end

had a lot of fun making the video like a parapara manga.

I hope it will be useful for everyone.

コメント

タイトルとURLをコピーしました