【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

二、结果展示:

相关推荐
彭祥.7 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
烛阴7 小时前
简单入门Python装饰器
前端·python
超龄超能程序猿7 小时前
(三)PS识别:基于噪声分析PS识别的技术实现
图像处理·人工智能·计算机视觉
好开心啊没烦恼7 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
面朝大海,春不暖,花不开8 小时前
使用 Python 实现 ETL 流程:从文本文件提取到数据处理的全面指南
python·etl·原型模式
Tony沈哲8 小时前
macOS 上为 Compose Desktop 构建跨架构图像处理 dylib:OpenCV + libraw + libheif 实践指南
opencv·算法
2301_805054569 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
万千思绪9 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
Chef_Chen10 小时前
从0开始学习计算机视觉--Day07--神经网络
神经网络·学习·计算机视觉
微风粼粼11 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo