python-opencv实现视频指定帧数间隔图像的保存功能
作者:_codeplus 时间:2021-05-28 16:51:50
使用opencv-python,把一段视频中指定帧频间隔的图像保存到新建的文件中
首先安装好python ,配置好opencv-python
# 导入所有必要的库
import cv2
import os
# 从指定的路径读取视频
cam = cv2.VideoCapture("F:/video1.mp4")
try:
# 创建名为data的文件夹
if not os.path.exists('data'):
os.makedirs('data')
# 如果未创建,则引发错误
except OSError:
print('Error: Creating directory of data')
# 定义保存图片函数
# image:要保存的图片名字
# addr;图片地址与相片名字的前部分
# num: 相片,名字的后缀。int 类型
def save_image(image, addr, num):
address = addr + str(num) + '.jpg'
cv2.imwrite(address, image)
# reading from frame
ret, frame = cam.read() # ret为布尔值 frame保存着视频中的每一帧图像 是个三维矩阵
i = 0
timeF = 15 # 设置要保存图像的间隔 15为每隔15帧保存一张图像
j = 0
while ret:
i = i + 1
# 如果视频仍然存在,继续创建图像
if i % timeF == 0:
# 呈现输出图片的数量
j = j + 1
save_image(frame, './data/', j)
print('save image:', j)
ret, frame = cam.read()
# 一旦完成释放所有的空间和窗口
cam.release()
cv2.destroyAllWindows()
结果如下:
查看新建的文件夹
来源:https://blog.csdn.net/qq_42593798/article/details/116204556
标签:python,opencv,视频帧数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中模块与包有相同名字的处理方法
2021-11-16 07:58:34
python判断字符串的前两个字母是否是"id"的示例代码
2021-05-02 20:39:12
python中zip()函数遍历多个列表方法
2022-03-09 21:40:00
如何使用Python Matplotlib绘制条形图
2023-09-21 04:41:46
![](https://img.aspxhome.com/file/2023/2/95852_0s.png)
python生成器用法实例详解
2023-01-24 12:46:58
python基础之集合
2022-04-21 05:03:50
![](https://img.aspxhome.com/file/2023/5/69055_0s.png)
如何用拦截表单的方法上传图片?
2010-06-16 09:50:00
python政策网字体反爬实例(附完整代码)
2021-04-27 19:51:32
![](https://img.aspxhome.com/file/2023/1/93751_0s.jpg)
用实例分析如何整理SQL Server输入数据
2009-01-20 15:16:00
javascript彩虹圈效果
2011-08-05 19:10:45
Flask-Sqlalchemy的基本使用详解
2023-06-16 19:40:02
![](https://img.aspxhome.com/file/2023/4/76044_0s.jpg)
用JS访问操作iframe框架里的dom
2008-11-10 13:05:00
![](https://img.aspxhome.com/file/UploadPic/200811/10/1-55s.gif)
python 读写、创建 文件的方法(必看)
2023-08-07 11:37:40
python 实现socket服务端并发的四种方式
2022-08-09 22:19:46
解决安装pycharm后不能执行python脚本的问题
2023-07-25 06:51:04
利用Python上传日志并监控告警的方法详解
2022-08-23 15:31:01
![](https://img.aspxhome.com/file/2023/7/69477_0s.png)
深入探讨SQL Server 2008商务智能(BI)
2008-12-23 13:56:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081223182357659.jpg)
Python中random模块常用方法的使用教程
2023-12-03 16:30:11
pyTorch深入学习梯度和Linear Regression实现
2023-05-12 01:00:45
![](https://img.aspxhome.com/file/2023/5/89435_0s.png)
python清除字符串中间空格的实例讲解
2023-12-29 22:40:24