How to get image from video using opencv python

后端 未结 2 1174
独厮守ぢ
独厮守ぢ 2021-02-14 09:12

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

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 10:15

    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')
    

提交回复
热议问题