Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

take screenshot of video python

import cv2
import os
import time

def get_frames(inputFile,outputFolder,step,count):

  '''
  Input:
    inputFile - name of the input file with directoy
    outputFolder - name and path of the folder to save the results
    step - time lapse between each step (in seconds)
    count - number of screenshots
  Output:
    'count' number of screenshots that are 'step' seconds apart created from video 'inputFile' and stored in folder 'outputFolder'
  Function Call:
    get_frames("test.mp4", 'data', 10, 10)
  '''

  #initializing local variables
  step = step
  frames_count = count

  currentframe = 0
  frames_captured = 0

  #creating a folder
  try:  
      # creating a folder named data 
      if not os.path.exists(outputFolder): 
          os.makedirs(outputFolder) 
    
  #if not created then raise error 
  except OSError: 
      print ('Error! Could not create a directory') 
  
  #reading the video from specified path 
  cam = cv2.VideoCapture(inputFile) 

  #reading the number of frames at that particular second
  frame_per_second = cam.get(cv2.CAP_PROP_FPS)

  while (True):
      ret, frame = cam.read()
      if ret:
          if currentframe > (step*frame_per_second):  
              currentframe = 0
              #saving the frames (screenshots)
              name = './data/frame' + str(frames_captured) + '.jpg'
              print ('Creating...' + name) 
              
              cv2.imwrite(name, frame)       
              frames_captured+=1
              
              #breaking the loop when count achieved
              if frames_captured > frames_count-1:
                ret = False
          currentframe += 1           
      if ret == False:
          break
  
  #Releasing all space and windows once done
  cam.release()
  cv2.destroyAllWindows()
Comment

PREVIOUS NEXT
Code Example
Python :: selenium open inspect 
Python :: url settings 
Python :: find the index of a character in a string python 
Python :: basic games to code in python 
Python :: file.open("file.txt); 
Python :: convert pdf to csv python 
Python :: how do i turn a tensor into a numpy array 
Python :: python program to add two numbers using function 
Python :: plot multiple axes matplotlib 
Python :: python acf and pacf code 
Python :: check if point is inside polygon python 
Python :: create app in django 
Python :: how to create enter pressed for qlineedit in pyqt5 
Python :: python program to find largest number in a list 
Python :: how to get the duration of audio python 
Python :: install different python version debian 
Python :: pandas convert column to datetime 
Python :: keras lstm example 
Python :: Python datetime to string using strftime() 
Python :: django check user admin 
Python :: python remove all elemnts in list containing string 
Python :: urllib 
Python :: pandas earliest date in column 
Python :: how to use get-pip.py 
Python :: groupby and sort python 
Python :: headless chrome python 
Python :: check if a value is nan pandas 
Python :: showing specific columns pandas 
Python :: pyspark now 
Python :: how to capitalize first letter in python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =