python读取子目录图片并整理重命名转存

/home/jiuanlab1/July_Aug_Proj/datalabel 中有多个子目录,
每个子目录中有两张图片,
目标是提取每个子目录中的图片并且分别保存到两个文件夹org和lab中。
并且对图片重命名,从000.png到121.png,保证名字长度相同。

import os
import cv2

cnt_img=0

def rename_label(path,outpath1,outpath2):  
    global cnt_img
    files = os.listdir(path) 
    for f in files:  
        if(os.path.isdir(path + '/' + f)):  
            # 排除隐藏文件夹。因为隐藏文件夹过多  
            if(f[0] == '.'):  
                pass  
            else:  
                # 非隐藏文件夹
                subdir= os.path.join(path,f)  
                imgs_path=os.listdir(subdir)
                cnt=0
                for r in imgs_path:
                    if(cnt==2):
                        break                    
                    img_path_every=os.path.join(subdir,r)               #读取图片路径
                    img_Org = cv2.imread(img_path_every)                #读取图片
                    img_gray = cv2.cvtColor(img_Org,cv2.COLOR_RGB2GRAY) #转化为灰度图
                    if(cnt==0):
                        img_name=outpath1+'/' + format(str(cnt_img),'0>3s') + '.jpg'
                    else:
                        img_name=outpath2+'/' + format(str(cnt_img),'0>3s') + '.jpg'
                    cv2.imwrite(img_name,img_gray)
                    cnt=cnt+1
                cnt_img=cnt_img+1



  
if __name__ == '__main__': 
    label_path='/home/jiuanlab1/July_Aug_Proj/datalabel'                #Absolute path 
    outpath1='./output/org'
    outpath2='./output/lab'

    rename_label(label_path, outpath1,outpath2)  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值