The Wayback Machine - https://web.archive.org/web/20240627004252/https://www.geeksforgeeks.org/extract-images-from-video-in-python/
Open In App

Extract images from video in Python

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

OpenCV comes with many powerful video editing functions. In current scenario, techniques such as image scanning, face recognition can be accomplished using OpenCV.

Image Analysis is a very common field in the area of Computer Vision. It is the extraction of meaningful information from videos or images. OpenCv library can be used to perform multiple operations on videos.

Modules Needed:

import cv2
import os

Function Used :

VideoCapture(File_path) : Read the video(.mp4 format)

read() : Read data depending upon the type of object that calls

imwrite(filename, img[, params]) : Saves an image to a specified file.

Below is the implementation:




# Importing all necessary libraries
import cv2
import os
  
# Read the video from specified path
cam = cv2.VideoCapture("C:\\Users\\Admin\\PycharmProjects\\project_1\\openCV.mp4")
  
try:
      
    # creating a folder named data
    if not os.path.exists('data'):
        os.makedirs('data')
  
# if not created then raise error
except OSError:
    print ('Error: Creating directory of data')
  
# frame
currentframe = 0
  
while(True):
      
    # reading from frame
    ret,frame = cam.read()
  
    if ret:
        # if video is still left continue creating images
        name = './data/frame' + str(currentframe) + '.jpg'
        print ('Creating...' + name)
  
        # writing the extracted images
        cv2.imwrite(name, frame)
  
        # increasing counter so that it will
        # show how many frames are created
        currentframe += 1
    else:
        break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()


Output:
image

All the extracted images will be saved in a folder named “data” on the system.
img



Previous Article
Next Article

Similar Reads

Extract Video Frames from Webcam and Save to Images using Python
There are two libraries you can use: OpenCV and ImageIO. Which one to choose is situation-dependent and it is usually best to use the one you are already more familiar with. If you are new to both then ImageIO is easier to learn, so it could be a good starting point. Whichever one you choose, you can find examples for both below: ImageIOInstallatio
2 min read
How to Extract Text from Images with Python?
OCR (Optical Character Recognition) is the process of electronical conversion of Digital images into machine-encoded text. Where the digital image is generally an image that contains regions that resemble characters of a language. OCR is a field of research in pattern recognition, artificial intelligence and computer vision. This is due to the fact
4 min read
How to extract images from PDF in Python?
The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python. To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow. pip install PyMuPDF Pillow PyMuPDF is used to access PDF files. To extract images from
3 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 min read
Convert Audio to Video using Static Images in Python
In this article, we are going to convert an audio file(mp3) to a video file(mp4) using the images provided by the user to be shown during the duration of the video using Python. To do this, we will first convert the images to a GIF file and then combining with the audio file to produce the final video file. Packages Required Mutagen: This Python pa
5 min read
Python - Process images of a video using OpenCV
Processing a video means, performing operations on the video frame by frame. Frames are nothing but just the particular instance of the video in a single point of time. We may have multiple frames even in a single second. Frames can be treated as similar to an image.So, whatever operations we can perform on images can be performed on frames as well
4 min read
Extract speech text from video in Python
Nowadays, videos have become an integral part of our lives. Videos educate us and provide the necessary information. In this article, we will learn how to extract text speech from video using Python. Extract Speech Text from the Video To extract speech text from video in Python, we require the following modules to install, Here we are using Python
3 min read
Pafy - Getting Video ID of the given Video
In this article we will see how we can get video ID of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A Youtube Video ID is an unique ID to identify a video which is uploaded to Youtube. The Youtube video
2 min read
Pafy - Getting Best Video Stream of the given Video
In this article we will see how we can get best video stream of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Best video stream is basically a video stream which is available and in highest resolution. T
2 min read
Practice Tags :
three90RightbarBannerImg