import cv2
import os
def save_img():
video_path = ''\\文件夹路径
videos = os.listdir(video_path)
cut_video = []
for video_name in videos:
if video_name.endswith(".mp4"):\\选择视频后缀名一般是mp4、avi
cut_video.append(video_name)
count = 1
for video in cut_video:
file_name = video.split('.')[0]
folder_name = video_path + file_name
cutvideo_name = video_path + video
os.makedirs(folder_name, exist_ok=True)\\建立保存的文件夹
vc = cv2.VideoCapture(cutvideo_name)
c = 0
rval = vc.isOpened()
while rval:
rval, frame = vc.read()
pic_path = folder_name + '/'
if rval:
%%if c%15==0:\\取帧的间隔
save_path = pic_path + ''+ str(count) + str(c).zfill(4) + '.png'\\按顺序命名保存的图像
cv2.imwrite(save_path, frame)
cv2.waitKey(1)
else:
break
c = c + 1
vc.release()
count = count + 1
print('save_success')
print(folder_name)
save_img()
实用脚本一:python将文件夹中的视频文件按帧分割为图像并命名保存
最新推荐文章于 2024-09-27 08:52:32 发布