Skip to content Skip to sidebar Skip to footer

How To Get Image From Video Using Opencv Python

I want to get image from video and store it in '.jpeg' or '.png' format please help me how to do this with opencv My code is Here i am trying to get the image from video frame by

Solution 1:

This is what I use to read in a video and save off the frames:

import cv2
import os

def video_to_frames(video, path_output_dir):
    # extract frames from a video and save to directory as 'x.png' where 
    # x is the frame index
    vidcap = cv2.VideoCapture(video)
    count = 0
    while vidcap.isOpened():
        success, image = vidcap.read()
        if success:
            cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
            count += 1
        else:
            break
    cv2.destroyAllWindows()
    vidcap.release()

video_to_frames('../somepath/myvid.mp4', '../somepath/out')

Solution 2:

This is my code for get image from video with opencv

import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture("Malar.mp4")
print vidcap.read()
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  print 'Read a new frame: ', success
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  count += 1

Post a Comment for "How To Get Image From Video Using Opencv Python"