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

相关推荐
子午7 分钟前
基于YOLO的车牌识别检测~Python+YOLOV8算法+车牌定位+车牌检测+深度学习
python·算法·yolo
weixin_307779138 分钟前
OCR图片文本提取代码
图像处理·python·opencv·自动化·ocr
ZC跨境爬虫9 分钟前
模块化烹饪小程序开发日记 Day3:(Flask后端初始化、数据库配置与自定义日志系统搭建)
前端·javascript·数据库·后端·python·flask
格林黄9 分钟前
语音电子病历python_websocket实现
开发语言·python·websocket
JavaEdge.9 分钟前
07-LangChain Toolkit 实战:从工具函数到 Python Agent,再到 SQL Agent
python·sql·langchain
AI人工智能+14 分钟前
基于OCR与深度学习的发票识别技术,重构报销系统效率
计算机视觉·自然语言处理·ocr·发票识别
Chase_______22 分钟前
【Java杂项】为什么 b += 1 可以,但 b = b + 1 会报错?类型提升与复合赋值详解
java·开发语言·python
Wiktok25 分钟前
【Wit智慧引擎】亲测可用国内pytorch镜像
人工智能·pytorch·python
旦莫34 分钟前
一个完美的AI测试Agent应该是什么样的
人工智能·python·测试开发·pytest·ai测试
勤自省40 分钟前
ROS2 + OpenCV 实战教程:人脸识别、物体跟踪、ArUco 二维码识别初级
人工智能·opencv·ubuntu·计算机视觉·ros2