python opencv 把视频转为图片

·  阅读 134

 

import cv2
def getFrame(videoPath, svPath):
    cap = cv2.VideoCapture(videoPath)
    numFrame = 0
    while True:
        if cap.grab():
            flag, frame = cap.retrieve()
            if not flag:
                continue
            else:
                numFrame += 1
                newPath = svPath + str(numFrame) + ".jpg"
                cv2.imencode('.jpg', frame)[1].tofile(newPath)
        if cv2.waitKey(10) == 27:
            break

videoPath = './1.mp4'
savePicturePath = './pic/'
getFrame(videoPath,savePicturePath)
复制代码

分类:
人工智能
标签:
收藏成功!
已添加到「」, 点击更改