“Python-OpenCV” How to Make Images of Movie’s (gif&mp4) All Frame.

AI
higashi
higashi

Hi, I’m higashi.

 

This time, I introduce how to make images of movie’s all frame.

Specifically, by using below movie,

I make these images that is composed in above movie.

動画ファイルの各フレームを画像化した様子

Supported formats is gif and mp4.

 

I think it will be useful when we make teacher data for AI from movie.

 

So, let’s get started!!

 

Sponsored Links

Make Image from “gif” Format Movie

At first, I make images from gif format movie.

The program is shown below.

It assume that there is a base movie in same folder with this program.

#import library
import cv2
os
#make folder for images
folder_name='gif_pic'
os.makedirs(folder_name, exist_ok=True)
#load base movie
movie = cv2.VideoCapture('01_gif_test.gif')
#get frame number
nframe = int(movie.get(cv2.CAP_PROP_FRAME_COUNT))
#making images
for i in range(nframe):
    ret, frame = movie.read()
    cv2.imwrite(folder_name+'/pic_'+str(i).zfill(3)+'.jpg', frame)

The result is below.

gif動画を画像化した結果

It is likely no problem.

 

Sponsored Links

Make Image from “mp4” Format Movie

Next is mp4 format.

It also assume that there is a base movie in same folder with this program.

#import library
import cv2
os
#make folder for images
folder_name='mp4_pic'
os.makedirs(folder_name, exist_ok=True)
#load base movie
movie = cv2.VideoCapture('02_mp4_test.mp4')
#get frame number
nframe = int(movie.get(cv2.CAP_PROP_FRAME_COUNT))
#making images
for i in range(nframe):
    ret, frame = movie.read()
    cv2.imwrite(folder_name+'/pic_'+str(i).zfill(3)+'.jpg', frame)

The result is below.

mp4動画を画像化した結果

It is likely no problem too.

 

That’s all. Thank you!!

コメント