Skip to content
Advertisement

Sampling from video frames and save them as an image – python openCV

I have a video file, I know how to save all of the frames,but my question is how to save only one frame of every 20 frames as an image? Thanks.

Advertisement

Answer

This is based on this tutorial: https://theailearner.com/2018/10/15/extracting-and-saving-video-frames-using-opencv-python/

import cv2

# Opens the Video file
cap= cv2.VideoCapture('C:/New/Videos/Play.mp4')
i=0
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
        break
    if i % 20 == 0: # this is the line I added to make it only save one frame every 20
        cv2.imwrite('kang'+str(i)+'.jpg',frame)
    i+=1

cap.release()
cv2.destroyAllWindows()
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement