python3抓取图片视频_Python opencv提取视频中的图片
#-*- coding: utf-8 -*-
importosimport cv2 ##加载OpenCV模块
def video2frames(pathIn='',
pathOut='',
only_output_video_info=False,
extract_time_points=None,
initial_extract_time=0,
end_extract_time=None,
extract_time_interval= -1,
output_prefix= 'frame',
jpg_quality= 100,
isColor=True):'''pathIn:视频的路径,比如:F:\python_tutorials\test.mp4
pathOut:设定提取的图片保存在哪个文件夹下,比如:F:\python_tutorials\frames1\。如果该文件夹不存在,函数将自动创建它
only_output_video_info:如果为True,只输出视频信息(长度、帧数和帧率),不提取图片
extract_time_points:提取的时间点,单位为秒,为元组数据,比如,(2, 3, 5)表示只提取视频第2秒, 第3秒,第5秒图片
initial_extract_time:提取的起始时刻,单位为秒,默认为0(即从视频最开始提取)
end_extract_time:提取的终止时刻,单位为秒,默认为None(即视频终点)
extract_time_interval:提取的时间间隔,单位为秒,默认为-1(即输出时间范围内的所有帧)
output_prefix:图片的前缀名,默认为frame,图片的名称将为frame_000001.jpg、frame_000002.jpg、frame_000003.jpg......
jpg_quality:设置图片质量,范围为0到100,默认为100(质量最佳)
isColor:如果为False,输出的将是黑白图片'''cap= cv2.VideoCapture(pathIn) ##打开视频文件
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) ##视频的帧数
fps = cap.get(cv2.CAP_PROP_FPS) ##视频的帧率
dur = n_frames/fps ##视频的时间
##如果only_output_video_info=True, 只输出视频信息,不提取图片
ifonly_output_video_info:print('only output the video information (without extract frames)::::::')print("Duration of the video: {} seconds".format(dur))print("Number of frames: {}".format(n_frames))print("Frames per second (FPS): {}".format(fps))##提取特定时间点图片
elif extract_time_points is notNone:if max(extract_time_points) > dur: ##判断时间点是否符合要求
raise NameError('the max time point is larger than the video duration....')try:
os.mkdir(pathOut)exceptOSError:passsuccess=True
count=0while success and count
cap.set(cv2.CAP_PROP_POS_MSEC, (1000*extract_time_points[count]))
success,image=cap.read()ifsuccess:if notisColor:
image= cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ##转化为黑白图片
print('Write a new frame: {}, {}th'.format(success, count+1))
cv2.imwrite(os.path.join(pathOut,"{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) #save frame as JPEG file
count = count + 1
else:##判断起始时间、终止时间参数是否符合要求
if initial_extract_time >dur:raise NameError('initial extract time is larger than the video duration....')if end_extract_time is notNone:if end_extract_time >dur:raise NameError('end extract time is larger than the video duration....')if initial_extract_time >end_extract_time:raise NameError('end extract time is less than the initial extract time....')##时间范围内的每帧图片都输出
if extract_time_interval == -1:if initial_extract_time >0:
cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time))try:
os.mkdir(pathOut)exceptOSError:pass
print('Converting a video into frames......')if end_extract_time is notNone:
N= (end_extract_time - initial_extract_time)*fps + 1success=True
count=0while success and count
success,image=cap.read()ifsuccess:if notisColor:
image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))
cv2.imwrite(os.path.join(pathOut,"{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) #save frame as JPEG file
count = count + 1
else:
success=True
count=0whilesuccess:
success,image=cap.read()ifsuccess:if notisColor:
image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))
cv2.imwrite(os.path.join(pathOut,"{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) #save frame as JPEG file
count = count + 1
##判断提取时间间隔设置是否符合要求
elif extract_time_interval > 0 and extract_time_interval < 1/fps:raise NameError('extract_time_interval is less than the frame time interval....')elif extract_time_interval > (n_frames/fps):raise NameError('extract_time_interval is larger than the duration of the video....')##时间范围内每隔一段时间输出一张图片
else:try:
os.mkdir(pathOut)exceptOSError:pass
print('Converting a video into frames......')if end_extract_time is notNone:
N= (end_extract_time - initial_extract_time)/extract_time_interval + 1success=True
count=0while success and count
cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval))
success,image=cap.read()ifsuccess:if notisColor:
image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))
cv2.imwrite(os.path.join(pathOut,"{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) #save frame as JPEG file
count = count + 1
else:
success=True
count=0whilesuccess:
cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval))
success,image=cap.read()ifsuccess:if notisColor:
image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))
cv2.imwrite(os.path.join(pathOut,"{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) #save frame as JPEG file
count = count + 1
##### 测试
pathIn = 'test.mp4'video2frames(pathIn, only_output_video_info=True)
pathOut= './frames1/'video2frames(pathIn, pathOut)
pathOut= './frames2'video2frames(pathIn, pathOut, extract_time_points=(1, 2, 5))
pathOut= './frames3'video2frames(pathIn, pathOut,
initial_extract_time=1,
end_extract_time=3,
extract_time_interval= 0.5)
pathOut= './frames4/'video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), isColor =False)
pathOut= './frames5/'video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), jpg_quality=50)
python3抓取图片视频_Python opencv提取视频中的图片相关推荐
- python抓取图片数字_Python OCR提取普通数字图形验证中的数字
1 #-*- coding: UTF-8 -*- 2 ''' 3 Created on 2016年7月4日4 5 @author: xuxianglin6 ''' 7 importos8 import ...
- opencv提取视频帧,将多张图片组合成视频(C++)
OpenCV-视频处理-视频处理5191 2 opencv提取视频帧,将多张图片组合成视频(C++) 作者:bit452 参考:http://blog.sina.com.cn/s/blog_5f593 ...
- Moviepy自动化视频处理:提取视频里的音乐(eg:MV音乐提取,抖音热门音乐提取)
Moviepy自动化视频处理:提取视频里的音乐(eg:MV音乐提取,抖音热门音乐提取) 本文将讲述的工作: 提取单个短视频中的音乐 整个文件夹下所有视频的音乐,分别输出mp3文件 假想的用途场景: 你 ...
- Python 批量提取Excel中的图片,图片文件名按指定列存储
openpyxl 以及 openpyxl_image_loader,conda或者pip无法安装的话,官网下载whl,然后pip安装本地包 [注意]:图片必须在单元格内,不能压过单元格边界 from ...
- Python提取PPT中的图片
一.前言 今天要带大家实现的是PPT图片的提取.在我们学习工作中,PPT的使用还是非常频繁的,但是自己做PPT是很麻烦的,所以就需要用到别人的模板或者素材,这个时候提取PPT图片就可以减少我们很多工作 ...
- 批量提取Word中的图片
我在写学习笔记的时候喜欢用word或Evernote直接排版做笔记,好处是快速方便,直接截图插入.但是再刊载在CSND上的时候就有麻烦了,因为不能直接粘贴图片,只能上传到相册中再选取.这时我又不愿意一 ...
- C# Pdf转Png,提取Pdf中的图片
把Pdf转为图片png格式 命名空间: using Aspose.Pdf; using System.IO; using Aspose.Pdf.Devices; 需要NuGet的包:Aspose.Pd ...
- pandoc提取word中的图片
pandoc提取word中的图片 pandoc -i xxx.docx -o xxx.tex --extract-media=pathName 会将word中的图片提取到 pathName下的medi ...
- python截取图片的ROI+OpenCV 在坐标中显示图片+鼠标点击图片显示点坐标
又来做下笔记了.现在我的毕业设计进行到数据处理的阶段,要在乳腺图像上截取4张相同大小的1024*1024图片,并且每张图片都必须覆盖微钙化点,以下是我现在用到的一些简单程序. 1.首先是截图程序如下: ...
最新文章
- 不属于未来计算机范畴,智慧职教云课堂工程数学-2020年春网课答案
- 5 加盐_洗花甲时,别只放盐了!老渔民教您一招,5分钟就搞定,太省事了
- java获得网页的编码方式_求一段代码,可以获得编码格式是gb2312的网页的页面源码,java实现!...
- 图解HashMap(一)
- shader weaver_Oracle通过邀请Weaver和Chin推动JavaFX向前发展
- MySQL 添加列,修改列,删除列 的SQL写法
- 使用Dockerfile构建自己的etcd镜像
- 【数据结构笔记02】什么是算法
- 将Nginx加入service服务中
- kingroot android 7,KingRoot全球实现Android 7.0一键 Root
- tuxedo中间件tmadmin的命令使用
- TCP粘包/拆包--利用LineBasedFrameDecoder解决TCP粘包问题
- css背景颜色渐变 从左到右 从下到上
- 基于单片机的自行车里程监测系统的设计(自行车码表)
- php老虎杠子鸡虫条件,老虎、杠子、鸡——在游戏中学习
- 在VMware ESXi中使用固态硬盘
- 置液晶显示器的台式计算机,台式电脑液晶显示器怎么购买
- glew, glee与 gl glu glut glx glext的区别和关系
- 【Dvhop定位】基于加权双曲线定位的Dvhop算法附matlab代码
- 【exe4j】如何利用exe4j把java桌面程序生成exe文件