How to extract images from video using Python OpenCV?

Featured Img Video 2 Image

Hello, there fellow learner! Today we will be learning how to extract images from video using the Python OpenCV module. So let’s begin!

The goal of the tutorial

We aim to extract each frame from a single video file with the help of the OpenCV module of the Python programming language.

Extract Images from Video using Python OpenCV

Now that we are aware of what are we going to do exactly. Let us start implementing the code.

1. Importing Modules

The first step just like any other project is to import the modules. We would be needing just the openCV module for the program.

Make sure you have the module installed in your system. And if you are unaware of what OpenCV is, do check out this tutorial. The tutorial will make you familiar with basic concepts on OpenCV.

2. Capturing the video

To capture the video, we will be using the VideoCapture function of the opencv module and store the recorded video into a variable.

Make sure the video is in the same folder as the code file or you need to enter the exact path of the video file.

Recommended read: How to edit videos using the moviepy module?

In case you don’t have a video with you, you can capture live data from your webcam after passing the value 0 inside the function.

import cv2
cam = cv2.VideoCapture("video.mp4")
currentframe = 0

3. Extracting each frame and saving the frame image

Let us look at a the code and then understand it line by line.

while(True):
	ret,frame = cam.read()
	if ret:
		name = 'Video to Images\Frame(' + str(currentframe) + ').jpg'
		cv2.imwrite(name, frame)
		currentframe += 1
	else:
		break

Line 1: Creating a loop that keeps working until no more frames are available in the video.

Line 2: With the help of read function we get two things: rel and frame.
rel – returns either True or False indicating if we are receiving a frame or not.
frame – returns the whole frame at the particular moment.

Line 3-6: A if condition which checks if a frame is available or not using the rel value. Then we set the path and name format of the current frame.

The currentframe variable keeps the frame count. Then we will be using the imwrite function to write the frame at the path mentioned earlier.

Line 7-8: The else condition is taken into consideration when no frames are available in order to break the loop.

4. Releasing and Destroying all Windows

The last and final step is to release all the the videocaptures objects which were created earlier using the release function.

Next we make use of destroyAllWindows function to destroy any windows created and exit the whole code.

cam.release()
cv2.destroyAllWindows()

The Final Code and Output

The complete code and a screenshot of the frames created is displayed below.

import cv2
import os
cam = cv2.VideoCapture("video.mp4")
currentframe = 0
while(True):
	ret,frame = cam.read()
	if ret:
		name = 'Video to Images\Frame(' + str(currentframe) + ').jpg'
		cv2.imwrite(name, frame)
		currentframe += 1
	else:
		break
cam.release()
cv2.destroyAllWindows()
Frame By Frame OpenCV. - extract images from video in Python
Frame By Frame OpenCV

Conclusion

Congratulations! You know can get extract images from video. Hope you liked it! Stay tuned to learn more!

Thank you for reading!