将视频分成一帧一帧python_视频按帧截取图片python脚本

import cv2

import os

def mov2img():

#视频所在文件夹此处为相对路径,建议填相对路径

files = os.listdir("./mov_clear/")

counter = 0

for i in range(len(files)):

name_tmp = files[i]

path = './img_clear/' #输出图片的路径,建议填相对路径

print(path)

print(name_tmp)

vc = cv2.VideoCapture('./mov_clear/'+ name_tmp) #读入视频文件

print('./mov_clear/'+ name_tmp)

c=1

if vc.isOpened(): #判断是否正常打开

rval , frame = vc.read()

else:

rval = False

print(rval) #读取到视频则输出True

timeF = 1 #视频帧计数间隔频率

while rval: #循环读取视频帧

rval, frame = vc.read()

if(c%timeF == 0): #每隔timeF帧进行存储操作

counter += 1

#下面是命名方式,可以根据需要修改

if len(str(counter)) == 1:

file_name = "0000" + str(counter) + '.jpg'

if len(str(counter)) == 2:

file_name = "000" + str(counter) + '.jpg'

if len(str(counter)) == 3:

file_name = "00" + str(counter) + '.jpg'

if len(str(counter)) == 4:

file_name = "0" + str(counter) + '.jpg'

if len(str(counter)) == 5:

file_name = str(counter) + '.jpg'

cv2.imwrite(path + file_name, frame) #存储为图像

c = c + 1

vc.release()

mov2img()

你可能感兴趣的:(将视频分成一帧一帧python)