【Python编程】将同一种图片分类到同一文件夹下,并且将其分类的路径信息写成txt文件进行保存

注:数据结构同上一篇博文类似

一、代码

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:\Date_Set_Apple\2022_9'
folders=os.listdir(path6)
print(folders)



# d_num = 0
# dr_num =0
# i_num = 0
# p_num = 0

r_num = 0
c_num = 0

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('RGB'):
                img=cv2.imread(path8+"/"+image)
                cv2.imshow('img',img)
                cv2.waitKey(1)
                cv2.destroyAllWindows()
                cv2.imwrite(r'C:\Users\Hasee\apple\apple\RGB\no'+ str(r_num) + '.png', img)
                m= open('rgb_confidence.txt', 'a')  # 没有则创建txt文件,与代码文件同目录
                m.write("confidence图对应的RGB图:"+'no'+str(r_num)+"   "+folder+"    "+f+"   "+image + '\n')
                m.close()
                r_num += 1
            elif image.startswith('confidence'):
                img=cv2.imread(path8+"/"+image)
                cv2.imshow('img',img)
                cv2.waitKey(1)
                cv2.destroyAllWindows()
                cv2.imwrite(r'C:\Users\Hasee\apple\apple\tof_confidence\no'+ str(c_num) + '.png', img)
                m= open('rgb_confidence.txt', 'a')  # 没有则创建txt文件,与代码文件同目录
                m.write("RGB图对应的confidence图:"+'no'+str(c_num)+"   "+folder+"    "+f+"   "+image + '\n')
                m.close()
                c_num += 1
                    
#                 elif image.startswith('depthRGB'):
#                     img=cv2.imread(path8+"/"+image)
#                     cv2.imshow('img',img)
#                     cv2.waitKey(1)
#                     cv2.destroyAllWindows()
#                     cv2.imwrite(r'C:\Users\Hasee\apple\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'C:\Users\Hasee\apple\apple\tof_intensity\no'+ str(i_num) + '.png', img)
#                     i_num += 1
                    
                
                    
#                 elif image.startswith('depth.png'):
#                     img=cv2.imread(path8+"/"+image)
#                     cv2.imshow('img',img)
#                     cv2.waitKey(1)
#                     cv2.destroyAllWindows()
#                     cv2.imwrite(r'C:\Users\Hasee\apple\apple\tof_depth\no'+ str(d_num) + '.png', img)
#                     d_num += 1
                    
#                 elif image.endswith('.pcd'):
#                     img=path8+'/'+image
#                     shutil.copy(img,r'C:\Users\Hasee\apple\apple\tof_points\no'+ str(p_num) + '.pcd')
#                     p_num +=1

二、结果展示:

相关推荐
程序小K30 分钟前
OpenCV的CUDA模块进行图像处理
图像处理·人工智能·opencv
jndingxin30 分钟前
OpenCVCUDA 模块中在 GPU 上对图像或矩阵进行 边界填充(padding)函数copyMakeBorder()
人工智能·opencv
q_q王34 分钟前
‌FunASR‌阿里开源的语音识别工具
python·大模型·llm·语音识别
不学无术の码农1 小时前
《Effective Python》第1章 Pythonic 思维总结——编写优雅、高效的 Python 代码
开发语言·python
zhou1851 小时前
MySQL保姆级安装教程(附资源包+5分钟极速配置+环境变量调试技巧)
java·python·mysql·php
lczdyx2 小时前
PNG转ico图标(支持圆角矩形/方形+透明背景)Python脚本 - 随笔
图像处理·python
lgily-12252 小时前
常用的设计模式详解
java·后端·python·设计模式
鸿蒙布道师2 小时前
宇树科技安全漏洞揭示智能机器人行业隐忧
运维·网络·科技·安全·机器学习·计算机视觉·机器人
aminghhhh2 小时前
多模态融合【十九】——MRFS: Mutually Reinforcing Image Fusion and Segmentation
人工智能·深度学习·学习·计算机视觉·多模态
格林威2 小时前
Baumer工业相机堡盟工业相机的工业视觉是否可以在室外可以做视觉检测项目
c++·人工智能·数码相机·计算机视觉·视觉检测