×

Search anything:

Convert video to images in Python

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes | Coding time: 5 minutes

A video file is a collection of images. We can look at it as if video is text data, then images are characters. Extracting images (also called frames) from videos is important for various use cases such as image processing, analyzing a part of video in detail, video editing and much more.

In this article, we will take a look at how we can extract images from a video and save them in a folder sequentially. We will use Python as our Programming language and will use the OpenCV library for extracting images from a video.

To install OpenCV, use the following command:

pip install opencv-python

Download a sample video file from which you want to extract images like:

wget -c https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4 -O video.mp4

This will save a sample video named video.mp4. We will extract frames from this video. Following it, create a folder where you want to save the images:

mkdir image_data

Step 1: Load dependencies

In this step, we will load libraries which we will use. In this case, we have only one dependency that is OpenCV.

import cv2

Step 2: Load the video file

We will load the video file using OpenCV:

vidcap = cv2.VideoCapture('vid.mp4')

Step 3: Get next frame/ image

To get the next frame or image in the video, we need to use:

success, image = vidcap.read()

It returns two values:

  • success: true/ false; true denotes that an image has been loaded
  • image: the actual image data if success = true; if success = false, image is NULL

Step 4: Save the image

We can save the image as a jpeg image using the imwrite method of OpenCV.

cv2.imwrite("video_data/image_%d.jpg" % count, image)

Step 5: Read all images

To read all images, we need to call vidcap.read() repeatly until it returns false.

count = 1
while success:
  cv2.imwrite("video_data/image_%d.jpg" % count, image)    
  success, image = vidcap.read()
  print('Saved image ', count)
  count += 1

Complete Code:

import cv2
vidcap = cv2.VideoCapture('vid.mp4')
success, image = vidcap.read()
count = 1
while success:
  cv2.imwrite("video_data/image_%d.jpg" % count, image)    
  success, image = vidcap.read()
  print('Saved image ', count)
  count += 1

Output:

A section of the output:

...
Saved image  734
Saved image  735
Saved image  736
Saved image  737
Saved image  738
Saved image  739

Following is the view of the folder:

video_frame

With this, all frames have been extracted from the video and you can now go through each frame in detail. There are 739 images in total in our sample video.

Enjoy this new power.

OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


Convert video to images in Python
Share this