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

import cv2
vidcap = cv2.VideoCapture('video1.mp4')
success,image =
count = 0;
print "I am in success"
while success:
  success,image =
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  if cv2.waitKey(10) == 27:                     # exit if Escape is hit
  count += 1

Here i am trying to get the image from video frame by frame and save it as frame1,frame2,frame3

Best answer

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 =
        if success:
            cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
            count += 1

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