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

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

一、代码

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

二、结果展示:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen1906

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值