【Python编程】将同一种图片分类到同一文件夹中

一、数据结构如下:



二、编程工具:Jupyter-Notebook

三、代码:

python 复制代码
import os
import cv2
import shutil

path0=os.getcwd()+'\\apple\\RGB'
path1=os.getcwd()+'\\apple\\tof_confidence'
path2=os.getcwd()+'\\apple\\tof_depth'
path3=os.getcwd()+'\\apple\\tof_depthRGB'
path4=os.getcwd()+'\\apple\\tof_points'
path5=os.getcwd()+'\\apple\\tof_intensity'

if not os.path.exists(path0):
    os.makedirs(path0)
if not os.path.exists(path1):
    os.makedirs(path1)
if not os.path.exists(path2):
    os.makedirs(path2)
if not os.path.exists(path3):
    os.makedirs(path3)
if not os.path.exists(path4):
    os.makedirs(path4)
if not os.path.exists(path5):
    os.makedirs(path5)
    
path6=r'E:\Jupyter-Notebook\Apple-1'
folders=os.listdir(path6)
print(folders)


c_num = 1
dr_num =1
i_num = 1
r_num = 1
d_num = 1
p_num = 1


for folder in folders:
    path7=path6+'/'+folder
    fd=os.listdir(path7)
    print(fd)
    
    for f in fd:
        path8=path7+'/'+f
        images=os.listdir(path8)
    
        for image in images:
                
                if image.startswith('confidence'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_confidence\no'+ str(c_num) + '.png', img)
                    c_num += 1
                    
                elif image.startswith('depthRGB'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_depthRGB\no'+ str(dr_num) + '.png', img)
                    dr_num += 1
                    
                elif image.startswith('intensity'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_intensity\no'+ str(i_num) + '.png', img)
                    i_num += 1
                    
                elif image.startswith('RGB'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\RGB\no'+ str(r_num) + '.png', img)
                    r_num += 1
                    
                elif image.startswith('depth.png'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_depth\no'+ str(d_num) + '.png', img)
                    d_num += 1
                                
                elif image.endswith('.pcd'):
                    img=path8+'/'+image
                    shutil.copy(img,r'E:\Jupyter-Notebook\apple\tof_points\no'+ str(p_num) + '.pcd')
                    p_num +=1

注:代码文件与图片文件同处一个文件夹,如上例,在Jupyter-Notebook文件夹

相关推荐
iNeuOS工业互联网9 分钟前
多模态知识库,打通文本、图像与视频资源的协同实践与应用
人工智能·python·音视频
一晌小贪欢10 分钟前
python-第20天:关键字参数与不定长参数
java·开发语言·前端·python·数据可视化·函数参数·python办公
Metaphor69213 分钟前
如何在 Python 环境中裁剪 PDF 页面
python·pdf
半亩码田18 分钟前
C#转Python第4.4篇:JSON 处理:json 模块 vs System.Text.Json
python·c#·json
SelectDB技术团队28 分钟前
统一全文检索与 SQL 分析:Apache Doris 日志分析实践
大数据·数据结构·后端·python·全文检索·doris·日志分析
向哆哆30 分钟前
PCB电子元器件目标检测数据集:23类别 | 目标检测
人工智能·目标检测·计算机视觉
心中有你021436 分钟前
Python爬虫实战:京东商品数据爬取+可视化分析
开发语言·爬虫·python
jay神2 小时前
2026年YOLO还有哪些创新点可以做?
python·yolo·毕业设计·科研·课程设计·创新
傲笑风10 小时前
【openvino】tinybert基于openvino服务化部署(四)
人工智能·python·自然语言处理·nlp·bert·openvino
维基框架11 小时前
WIKI 知识库 v1.1.1 正式发布
人工智能·python