【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文件夹

相关推荐
forEverPlume几秒前
golang如何排查大量goroutine性能问题_golang大量goroutine性能排查详解
jvm·数据库·python
2301_814809861 分钟前
踩坑实战pywebview:用 Python + Web 技术打造轻量级桌面应用
开发语言·前端·python
思绪无限1 分钟前
YOLOv5至YOLOv12升级:疲劳驾驶检测系统的设计与实现(完整代码+界面+数据集项目)
人工智能·深度学习·yolo·目标检测·计算机视觉
2401_883600252 分钟前
Golang怎么CGO交叉编译_Golang如何在交叉编译时处理CGO依赖问题【避坑】
jvm·数据库·python
smj2302_796826523 分钟前
解决leetcode第3906题统计网格路径中好整数的数目
python·算法·leetcode
财经资讯数据_灵砚智能21 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月23日
大数据·人工智能·python·信息可视化·自然语言处理
yuanmazhiwu21 分钟前
计算机毕业设计:Python股市行情可视化与ARIMA预测系统 Flask框架 ARIMA 数据分析 可视化 大数据 大模型(建议收藏)✅
大数据·python·数据分析·django·flask·课程设计
AC赳赳老秦28 分钟前
OpenClaw与Excel联动:批量读取/写入数据,生成可视化报表
开发语言·python·excel·产品经理·策略模式·deepseek·openclaw
覆东流30 分钟前
第5天:Python字符串操作进阶
开发语言·后端·python