python使用openCV遍历文件夹里所有视频文件并保存成图片

作者:桜見 时间:2023-05-30 18:14:22 

如果你在文件夹里有很多视频,并且文件夹里还有文件夹,文件夹里的文件夹也有视频,怎么能逐个读取并且保存。。所以我写了个代码用了os,walk,这个可以遍历所有文件夹里的文件和文件夹


import os
import cv2
cut_frame = 250 # 多少帧截一次,自己设置就行
save_path = "C:\文献与资料\手持红外\图片"
for root, dirs, files in os.walk(r"C:\文献与资料\手持红外"): # 这里就填文件夹目录就可以了
for file in files:
# 获取文件路径
if ('.mp4' in file):
 path = os.path.join(root, file)
 video = cv2.VideoCapture(path)
 video_fps = int(video.get(cv2.CAP_PROP_FPS))
 print(video_fps)
 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:
  # cv2.imwrite(save_path + '/' + file[:-4] + str(current_frame) + '.jpg',
  #  image) # file[:-4]是去掉了".mp4"后缀名,这里我的命名格式是,视频文件名+当前帧数+.jpg,使用imwrite就不能有中文路径和中文文件名
  cv2.imencode('.jpg', image)[1].tofile(save_path + '/' + file[:-4] + str(current_frame) + '.jpg') #使用imencode就可以整个路径中可以包括中文,文件名也可以是中文
  print('正在保存' + file + save_path + '/' + file[:-4] + str(current_frame))

ps:下面看下python 遍历文件夹


import os
# 遍历文件夹
def walkFile(file):
for root, dirs, files in os.walk(file):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
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()

python使用openCV遍历文件夹里所有视频文件并保存成图片

总结

以上所述是小编给大家介绍的python使用openCV遍历文件夹里所有视频文件并保存成图片网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://blog.csdn.net/weixin_43446161/article/details/103929236

标签:python,opencv,文件夹
0
投稿

猜你喜欢

  • :hover在IE6下的问题

    2009-06-18 21:09:00
  • python淘宝抢购脚本程序实现

    2023-08-02 11:29:52
  • Python自动生产表情包

    2022-04-13 05:25:36
  • Python编写条件分支代码方法

    2021-08-16 12:31:17
  • 深入浅析Python 中的sklearn模型选择

    2023-05-15 19:12:00
  • Python数据分析之使用matplotlib绘制折线图、柱状图和柱线混合图

    2023-09-16 23:18:09
  • Python使用sklearn实现的各种回归算法示例

    2021-02-18 10:00:55
  • 保护你的ASP页面的两种办法

    2008-06-10 16:53:00
  • 详解python读取matlab数据(.mat文件)

    2021-03-04 19:29:29
  • Python heapq使用详解及实例代码

    2023-03-07 14:36:56
  • 瀑布流布局浅析

    2011-09-16 20:18:09
  • python 实现 hive中类似 lateral view explode的功能示例

    2021-08-20 13:51:45
  • Python进度条的制作代码实例

    2022-01-01 23:17:34
  • python装饰器使用方法实例

    2022-12-14 11:24:07
  • Python正则表达式re.search()用法详解

    2021-08-28 03:24:46
  • python selenium UI自动化解决验证码的4种方法

    2022-10-09 20:43:06
  • php+mysql查询优化简单实例

    2023-07-21 18:36:32
  • Python爬取酷狗MP3音频的步骤

    2022-04-07 03:15:21
  • PHP警告Cannot use a scalar value as an array的解决方法

    2023-11-14 20:43:04
  • Python PyQt4实现QQ抽屉效果

    2023-10-08 08:06:42
  • asp之家 网络编程 m.aspxhome.com