神剑山庄资源网 Design By www.hcban.com
如果你在文件夹里有很多视频,并且文件夹里还有文件夹,文件夹里的文件夹也有视频,怎么能逐个读取并且保存。。所以我写了个代码用了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遍历文件夹里所有视频文件并保存成图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python使用openCV遍历文件夹里所有视频文件并保存成图片的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】