【opencv-python】 截取视频指定帧数并保存

(1)读取文件:

video = cv.VideoCapture('video.avi') # 读取视频文件

(2)分帧读取视频:

ret,frame = video.read()

(3)对每一个帧图像进行保存:

cv.imwrite('D:\\save\\'+str(i)+'.png',frame)

看下面的完整程序

import cv2 as cv


#截图图像
def cutVideo():
    i = 0
    video = cv.VideoCapture('video.avi') # 读取视频文件
    while(True):
        ret,frame = video.read()
        cv.imshow('video',frame)
        c = cv.waitKey(50)
        if c == 27:
            break
        i=i+1
        if i%5==0:
            cv.imwrite('D:\\save\\'+str(i)+'.png',frame)


cutVideo()
cv.destroyAllWindows()

看下效果

【opencv-python】 截取视频指定帧数并保存