把视频变成一帧一帧的图片放到对应的文件夹下 python代码
代码
import cv2 # 安装见 https://blog.csdn.net/qq_43657442/article/details/109276236
from skimage import io # 若安装不上,或很慢,见https://blog.csdn.net/qq_43657442/article/details/109526813
import os
videos_path = r'D:\videos' # 视频所在文件夹的路径位置
videos_name = os.listdir(videos_path) # 从视频文件夹中获取各个视频的名字
for j, i in enumerate(videos_name): # 遍历所有的视频
# 以下两行代码的作用是为每段视频帧创建一个文件夹
path = r'D:\videos\%s' %(i[0:-4])
os.makedirs(path)
video_path = os.path.join(videos_path, i) # 获取每个视频的路径
if os.path.isdir(video_path): # 如果遇见文件夹就跳过
continue
camera = cv2.VideoCapture(video_path)
if camera.isOpened(): # 判断视频是否打开
print('Open')
else:
print('视频打开错误')
a = 0 # 视频帧图片的起始编号
while True:
success, frame_lwpCV = camera.read() # 读取视频流 https://blog.csdn.net/qq_43657442/article/details/109277467
if success==False: # 当从视频中读取不到视频帧就退出
break
io.imsave(r'D:\videos\%s\%s.jpg' %(i[0:-4],a), frame_lwpCV) # 保存图片
a += 1
# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()
这是我的放视频的文件夹
结果