import numpy as np
from PIL import Image

# 重新命名文件 加随机名字和标签
def FileReName(DogType, FilePath):
    type_counter = 0
    for types in DogType:
        file_counter = 0
        subfolder = os.listdir(FilePath+types) # Raw_Img/哈士奇
        for subclass in subfolder:
            file_counter += 1
            print(file_counter)
            print('Type_counter', type_counter)
            print(subclass)
            os.rename(FilePath+types+'/'+subclass, FilePath+types+'/'+str(type_counter)+'_'+str(file_counter)+'_'+subclass.split('.')[0]+'.jpg')
        type_counter += 1


# 重新图片尺寸
def FileResize(Output_folder, DogType, FilePath, Width=100, Height=100):
    for types in DogType:
        for i in os.listdir(FilePath+types):
            img_open = Image.open(FilePath+types+'/'+i)
            conv_RGB = img_open.convert('RGB')
            Resized_img = conv_RGB.resize((Width, Height), Image.BILINEAR)
            Resized_img.save(os.path.join(Output_folder, os.path.basename(i)))

    pass

# 读取图片返回array数组 进入神经网络的都是numpy array
def ReadImage(filename, train_folder):
    img = Image.open(train_folder+filename)
    return np.array(img)

# 图片加载到列表 图像 和 标签
def DataSet(train_folder):
    Train_list_img = []
    Train_list_label= []
    for file_1 in os.listdir(train_folder):
        file_img_to_array = ReadImage(filename=file_1, train_folder=train_folder)
        # 添加图片数组到主list里面
        Train_list_img.append(file_img_to_array)
        # 添加标签数组到主list里面
        Train_list_label.append(int(file_1.split('_')[0]))


    Train_list_img = np.array(Train_list_img) # 
    Train_list_label = np.array(Train_list_label) # numpy 可以使用 array.shape

    print(Train_list_img.shape) # X_train mnist
    print(Train_list_label.shape) # Y_train mnist







if __name__ == "__main__":
    DogType = ['哈士奇', '德国牧羊犬', '拉布拉多', '萨摩耶犬']
    #修改名字
    FileReName(DogType=DogType, FilePath='Raw_Img/')

    #修改尺寸
    FileResize(DogType=DogType, FilePath='Raw_Img/', Output_folder='train_img/')

    #准备好的数据
    DataSet(train_folder='train_img/')


