根据图片名字生成文件夹并归类+批量重命名照片名

最近在处理手头的部分图片数据,所以码了点code,也许质量不高,供大家参考,
欢迎各路大神纠正问题!!

1.该代码根据照片的名字来生成文件夹,并且将同类照片放置同一文件夹creat_dir.py

#-*-coding: utf-8-*-
#Author: AIBC-MrH

#该代码为数据处理用,根据照片的名字来生成文件夹;
#并且将source_dir目录下同一类别的照片保存至对应的文件夹内;

import os
import shutil
from cv2 import cv2

source_dirs = 'pic_ori'
target_dirs = 'name/'

def create_dir(source_file, target_dirs):
    temp = []
    temp1 = []
    temp2 = []
    img = os.listdir(source_file)
    for name in img:
        temp_line = name.split('_0')
        temp.append(temp_line)
    for j in range(len(temp)):
        temp1.append(temp[j][0])
    for name in temp1:
        if name not in temp2:
            temp2.append(name)
    
    for dirs in temp2:
        if os.path.exists(target_dirs+dirs):
            break
        else:
            os.mkdir(target_dirs+dirs)

    #copy图片到指定文件夹
    dir_name = os.listdir(target_dirs)
    name_temp = []
    for dir in dir_name:
        name_temp.append(dir)
        for i in range(len(name_temp)):
            for n in img:
                pic = n.split('_0')
                if pic[0] == name_temp[i]:
                    shutil.copy(source_dirs+'/'+n, target_dirs+name_temp[i])    
    return img

if __name__=='__main__':
    print('begin to clone pics')
    dirs = create_dir(source_dirs, target_dirs)
    print('finish copy !!!!!!!!!!')

2.该代码用于重命名文件夹内各个子文件夹内的照片rename.py

import os
from time import time
#Author: AIBC-MrH
'''
批量重命名单个文件夹内多个子文件夹内的照片
'''
def rename(source_dirs):
    count = 1
    file = os.listdir(source_dirs)
    for i in range(len(file)):
        pic = os.listdir(source_dirs+'/'+file[i])
        for img in pic:
            src = os.path.join(source_dirs+'/'+file[i], img)
            dst = os.path.join(source_dirs+'/'+file[i], str(count)+'.jpg')
            count += 1
            os.rename(src, dst)
    print('pic number is:{}'.format(count))
    return file

if __name__=='__main__':
    start = time()
    print('Begin!!!!')
    rename('../train_ori')
    end = time()
    print('cost time {}'.format(end-start))

3.将处理好的文件合并至一个文件内merge.py

#-*-coding: utf-8-*-
#Author: AIBC-MrH
'''
该代码是将两个文件夹内同类别的子文件夹内的照片合并至一个文件夹内
'''
import os
from time import time
import shutil

src = 'train/'
dst = 'train_all/'

def merge(source_dir, target_dir):
    temp1 = []
    temp2 = []
    dir1 = os.listdir(source_dir)
    dir2 = os.listdir(target_dir)
    for name1 in dir1:
        temp1.append(name1)
    for name2 in dir2:
        temp2.append(name2.split('.'))
    for i in range(len(temp1)):
        for j in range(len(temp2)):
            if temp1[i].upper() == temp2[j][1].upper():
                pic1 = os.listdir(source_dir+temp1[i])
                #pic2 = os.listdir(target_dir+temp2[j][0]+'.'+temp2[j][1])
                for img1 in pic1:
                    #for img2 in pic2:
                    shutil.move(source_dir+temp1[i]+'/'+img1, target_dir+temp2[j][0]+'.'+temp2[j][1])
    return 0

                       
if __name__=='__main__':
    start = time()
    print('Begin !!!')

    merge(src, dst)

    end = time()
    print('Finished cost time:{}'.format(end-start))

欢迎指出问题!互相学习!代码需要根据自己的处理文件名稍做修改使用!

你可能感兴趣的:(日常小脚本)