python3.5 cv2 获取视频特定帧生成jpg图片
作者:weixin_38336920 时间:2022-04-21 03:40:13
假如文件夹有大量视频文件,需求目标是想从每个视频中提取一帧作为视频的一个封面图片,本文利用opencv-python模块实现需求。结合自己的工作,做一下简单的记录,原本想生成可传参数的exe文件,以方便同事使用。但 import cv2 用pyinstaller生成exe时就会报错,暂时没有找到解决方案 ,报错如下:
(如哪位朋友碰到类似问题,并且解决,请给我留言,谢谢大神)
1、安装模块
pip install opencv-python
安装完成后 import cv2 出现异常,ImportError:DLL load fail:找不到指定模块,最后在github上找到解决办法
It works well on windows 7.Thanks! @kenliang18
download api-ms-win-downlevel-shlwapi-l1-1-0.dll file and put it in ‘C:\Windows\Sysytem32'
then run command regsvr32 api-ms-win-downlevel-shlwapi-l1-1-0.dll /s in cmd
2、逻辑实现
import cv2
import sys
from pathlib import Path
#没有输入文件夹地址,默认文件夹
filepath = sys.argv[1] if sys.argv[1:] else 'C:/Users/Administrator/Desktop/video'
#没有输入帧数,默认帧数为180
frameNum = sys.argv[2] if sys.argv[2:] else 180
for item in Path(filepath).rglob('*.mp4'):
filename = Path(item).stem #不带后缀文件名
newPath = Path(item).parent #文件路径
cap = cv2.VideoCapture(str(item)) # 读取视频文件
cap.set(cv2.CAP_PROP_POS_FRAMES, float(frameNum))
if cap.isOpened(): #判断是否正常打开
rval , frame = cap.read()
newFile = str(newPath)+ '/' + str(filename) +'.jpg'
cv2.imencode('.jpg', frame)[1].tofile(newFile)
print(newFile) #打印生成的路径名
cap.release()
3、结果展示
获取 F:/video 文件夹里面视频的第5帧
C:\Users\Administrator\Desktop\python\2018\201810>python 2018101201.py F:/video 5
F:\video/test.jpg
F:\video/测试.jpg
来源:https://blog.csdn.net/weixin_38336920/article/details/83027317
标签:python3.5,cv2,视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解python中groupby函数通俗易懂
2023-03-08 12:27:36
![](https://img.aspxhome.com/file/2023/7/112777_0s.png)
用python写PDF转换器的实现
2022-07-29 21:32:28
![](https://img.aspxhome.com/file/2023/1/87681_0s.png)
Python编程实现二叉树及七种遍历方法详解
2022-10-31 13:28:28
![](https://img.aspxhome.com/file/2023/4/76714_0s.jpg)
python 梯度法求解函数极值的实例
2023-08-11 07:33:05
用 Python 绘制全国鸿星尔克门店分布图
2022-03-07 11:26:12
![](https://img.aspxhome.com/file/2023/5/103155_0s.png)
怎么用FSO生成一个UNICODE格式的文本文件
2009-05-26 15:40:00
python偏函数partial用法
2023-09-24 22:25:06
JavaScript 实现 Konami Code
2009-05-26 12:44:00
![](https://img.aspxhome.com/file/UploadPic/20095/26/facebook-55s.jpg)
网页设计布局原则
2010-04-20 17:18:00
在Python的Flask框架中验证注册用户的Email的方法
2022-12-03 04:42:35
![](https://img.aspxhome.com/file/2023/3/88933_0s.jpg)
Python深拷贝浅拷贝图文示例清晰整理
2022-05-05 11:26:40
![](https://img.aspxhome.com/file/2023/1/99691_0s.jpg)
PyQt5笔记之弹出窗口大全
2021-06-30 06:53:14
![](https://img.aspxhome.com/file/2023/4/99084_0s.jpg)
Python搜索引擎实现原理和方法
2023-06-26 05:35:32
![](https://img.aspxhome.com/file/2023/7/67327_0s.png)
提升Python运行速度的5个小技巧
2021-07-28 20:25:24
Python中使用双下划线防止类属性被覆盖问题
2021-05-04 04:02:44
![](https://img.aspxhome.com/file/2023/2/87722_0s.jpg)
在Django中实现添加user到group并查看
2021-12-08 21:47:07
asp 快钱网关接口 支付宝接口 财付通接口代码
2011-03-08 10:55:00
检测SQL Server是否有特洛伊木马
2009-02-24 15:19:00
golang实践-第三方包为私有库的配置方案
2023-06-25 06:05:32
![](https://img.aspxhome.com/file/2023/3/92033_0s.jpg)
Python使用crontab模块设置和清除定时任务操作详解
2023-02-07 15:13:37
![](https://img.aspxhome.com/file/2023/6/80776_0s.png)