Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
作者:sestien 时间:2021-11-24 05:29:30
python里面可以将路径里面的\替换成/避免转义。
os.walk方法可以将目标路径下文件的root,dirs,files提取出来。后面对每个文件进行操作。
切片操作[:]判断是否为.jpg或.JPG文件。
shutil的copy方法将文件从旧路径复制到新路径。
glob的glob方法提取目标文件夹的所有图片,对每张图片进行显示保存等操作。
详细代码及注释如下:
import os
import shutil
import glob
import cv2
path = 'C:/Users/deepw/Desktop/testfile'
new_path = 'D:/new'
for root,dirs,files in os.walk(path): #提取文件夹下所有jpg文件复制转移到新的文件夹
for i in range(len(files)):
if files[i][-3:] == 'jpg' or files[i][-3:] == 'JPG':
file_path = root + '/' + files[i]
new_file_path = new_path + '/' + files[i]
shutil.copy(file_path,new_file_path)
img_path = glob.glob('D:/new/*.jpg') #获取新文件夹下所有图片
i=1
for each in img_path:
img = cv2.imread(each, cv2.IMREAD_UNCHANGED)
cv2.imshow('Image', img) #顺次显示每一帧
k=cv2.waitKey(0) #每一帧等待时间为无穷大
if k == ord('s'): #当按s键时保存此帧,按其他键则不保存而跳到下一帧
cv2.imwrite('D:/want/%d.jpg'%i,img,[int( cv2.IMWRITE_JPEG_QUALITY), 100])
cv2.destroyAllWindows()
else:
cv2.destroyAllWindows()
i=i+1
来源:https://blog.csdn.net/malvas/article/details/80880790
标签:Python,文件夹,jpg,每一帧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
go语言睡眠排序算法实例分析
2023-07-15 17:42:56
CSS执行顺序与优先权的问题
2010-08-23 16:21:00
ASP长文章分页代码实例
2007-10-02 17:04:00
python使用PIL剪切和拼接图片
2022-06-26 07:41:55
![](https://img.aspxhome.com/file/2023/4/93604_0s.png)
Python实现截屏的函数
2023-05-19 11:16:06
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2021-04-26 22:34:55
![](https://img.aspxhome.com/file/2023/7/71567_0s.jpg)
Python QQBot库的QQ聊天机器人
2022-03-18 23:29:30
![](https://img.aspxhome.com/file/2023/0/64720_0s.jpg)
python获取本地计算机名字的方法
2022-01-26 10:04:32
20行代码教你用python给证件照换底色的方法示例
2023-04-03 23:48:32
![](https://img.aspxhome.com/file/2023/9/90309_0s.jpg)
基于python神经卷积网络的人脸识别
2023-11-03 17:37:17
![](https://img.aspxhome.com/file/2023/6/64356_0s.png)
Python字符串本身作为bytes进行解码的问题
2022-12-22 07:18:42
好习惯和坏习惯
2009-01-20 12:51:00
用滤镜使网页图片产生旋转效果
2007-11-03 11:36:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311370301s.jpg)
SQL Server 获取插入记录后的ID(自动编号)
2009-09-10 11:31:00
在Python的Django框架上部署ORM库的教程
2021-04-08 02:20:47
asp如何读取一个文件内容?
2009-11-19 17:23:00
Python实现笑脸检测+人脸口罩检测功能
2022-06-24 04:01:49
![](https://img.aspxhome.com/file/2023/4/87474_0s.png)
ASP生成html的新方法
2011-04-02 11:04:00
python使用js2py库运行js代码
2022-09-06 17:06:55
Python缓存技术实现过程详解
2023-08-03 12:31:30