python利用opencv按帧提取视频中的图片

import cv2
import os
#要提取视频的文件名,隐藏后缀
sourceFileName='a2'
#在这里把后缀接上
video_path = os.path.join("", "", sourceFileName+'.mp4')
times=0
#提取视频的频率,每25帧提取一个
frameFrequency=25
#输出图片到当前目录vedio文件夹下
outPutDirName='vedio/'+sourceFileName+'/'
if not os.path.exists(outPutDirName):
    #如果文件目录不存在则创建目录
    os.makedirs(outPutDirName) 
camera = cv2.VideoCapture(video_path)
while True:
    times+=1
    res, image = camera.read()
    if not res:
        print('not res , not image')
        break
    if times%frameFrequency==0:
        cv2.imwrite(outPutDirName + str(times)+'.jpg', image)
        print(outPutDirName + str(times)+'.jpg')
print('图片提取结束')
camera.release()

设置每隔(frameFrequency)25帧保存一张图片

python利用opencv按帧提取视频中的图片_第1张图片 代码和视频文件位置关系

 

python利用opencv按帧提取视频中的图片_第2张图片 带图像的文字
python利用opencv按帧提取视频中的图片_第3张图片 输出结果

 

你可能感兴趣的:(python)