python使用openCV遍历文件夹里所有视频文件并保存成图片
在Python编程中,有时我们需要处理大量的视频数据,例如将视频文件转换为静态图像。这篇内容主要讲解了如何使用OpenCV库来遍历指定文件夹及其子文件夹中的所有MP4视频文件,并将每一帧按一定间隔保存为图片。OpenCV(Open Source Computer Vision Library)是一个强大的计算机视觉库,支持多种图像和视频处理功能。 导入必要的库,包括os用于文件操作和目录遍历,以及cv2(OpenCV的Python接口)用于视频读取和图像保存: ```python import os import cv2 ``` 接着,定义一个变量`cut_frame`来设定每多少帧截取一张图片,例如250帧。`save_path`则定义了保存图片的目标路径。然后使用`os.walk()`遍历指定目录及其所有子目录中的文件: ```python cut_frame = 250 save_path = "C:\文献与资料\手持红外\图片" for root, dirs, files in os.walk(r"C:\文献与资料\手持红外"): ``` 在遍历过程中,对于每一个文件,检查其是否为MP4视频文件: ```python for file in files: if ('.mp4' in file): path = os.path.join(root, file) ``` 使用`cv2.VideoCapture()`打开视频文件,并获取其帧率`video_fps`: ```python video = cv2.VideoCapture(path) video_fps = int(video.get(cv2.CAP_PROP_FPS)) print(video_fps) ``` 接下来,进入循环读取视频帧,每`cut_frame`帧保存一张图片: ```python current_frame = 0 while (True): ret, image = video.read() current_frame = current_frame + 1 if ret is False: video.release() break if current_frame % cut_frame == 0: # 使用imencode以适应包含中文的路径和文件名 cv2.imencode('.jpg', image)[1].tofile(save_path + '/' + file[:-4] + str(current_frame) + '.jpg') print('正在保存' + file + save_path + '/' + file[:-4] + str(current_frame)) ``` 此外,代码中还给出了一个简单的遍历文件夹的函数`walkFile()`,用于展示如何使用os库遍历文件和子目录: ```python def walkFile(file): for root, dirs, files in os.walk(file): for f in files: print(os.path.join(root, f)) for d in dirs: print(os.path.join(root, d)) def main(): walkFile("f:/ostest/") if __name__ == '__main__': main() ``` 这段代码可以帮助我们了解文件系统的基本操作,如遍历文件夹和读取视频文件。在实际应用中,可以根据需求调整`cut_frame`值来控制图片的生成频率,或修改`save_path`来指定保存图片的自定义位置。此外,为了兼容不同类型的视频文件,可以扩展代码以支持其他视频编码格式。确保在运行代码时,文件路径正确无误,以免出现找不到文件或路径错误的问题。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/release/download_crawler_static/12856345/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 6
- 资源: 933
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 打包和分发Rust工具.pdf
- SQL中的CREATE LOGFILE GROUP 语句.pdf
- C语言-leetcode题解之第172题阶乘后的零.zip
- C语言-leetcode题解之第171题Excel列表序号.zip
- C语言-leetcode题解之第169题多数元素.zip
- ocr-图像识别资源ocr-图像识别资源
- 图像识别:基于Resnet50 + VGG16模型融合的人体细胞癌症分类模型实现-图像识别资源
- C语言-leetcode题解之第168题Excel列表名称.zip
- C语言-leetcode题解之第167题两数之和II-输入有序数组.zip
- C语言-leetcode题解之第166题分数到小数.zip
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)