import os 
from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# import seaborn as sns
import pandas as pd
import numpy as np
import random

train_path = './AgriculturalDisease_trainingset/'
valid_path = './AgriculturalDisease_validationset/'


def genImage(gpath, datatype):

    

    if datatype == 'train':
        gen_number = 0  # 统计生成的图片数量
        if not os.path.exists(gpath+'delete'):
            os.makedirs(gpath+'delete')

        label = pd.read_csv(gpath + 'label.csv')
        label_gen_dict = {'img_path':[], 'label':[]}  # 生成图片label
        for i in range(61):
            li = label[label['label'] == i]
            imagenum = li['label'].count()
            print('第%d个，总共有有%d个图片'%(i, imagenum))
            imagelist = np.array(li['img_path']).tolist()
            img_path_gen, label_gen = [], []

            # for imagefile in imagelist:
            for aa in range(len(imagelist)):
                if aa <= 40:
                    print(aa)
                    path, imagename = os.path.split(imagelist[aa])

                    im = Image.open(imagelist[aa])
                    im = im.convert('RGB')
                    im_detail = im.transpose(Image.ROTATE_180)
                    # im_detail = im.filter(ImageFilter.DETAIL)  # 细节增强

                    img_path_gen.append(gpath + 'delete/' +'idetail_'+imagename)

                    label_gen.extend([int(i)])


                    im_detail.save(gpath + 'delete/' +'idetail_'+imagename)

                    gen_number += 1

            label_dict = {'img_path':img_path_gen, 'label':label_gen}
            label_gen_dict['img_path'].extend(img_path_gen)
            label_gen_dict['label'].extend(label_gen)
            label_gen_pd = pd.DataFrame(label_dict)
            # label = label.append(label_gen_pd)  # 将生成的图片label加入原先的label
            # label['label'] = label[['label']].astype('int64')  # 转化为int64
            # print(label)
        label_gen_p = pd.DataFrame(label_gen_dict)
        label_gen_p.to_csv(gpath + 'label_delete.csv', index=False)

        # label_gen_p = pd.DataFrame(label_gen_dict)
        # label_gen_p.to_csv(gpath + 'label_gen.csv', index=False)

        print('训练集总共生成%d个图片'%gen_number)


    if datatype == 'valid':
        gen_number = 0
        if not os.path.exists(gpath+'delete'):
            os.makedirs(gpath+'delete')
        label = pd.read_csv(gpath + 'label.csv')
        label_gen_dict = {'img_path':[], 'label':[]}
        for i in range(61):
            li = label[label['label'] == i]
            imagenum = li['label'].count()
            print('第%d个，总共有有%d个图片'%(i, imagenum))
            imagelist = np.array(li['img_path']).tolist()
            img_path_gen, label_gen = [], []
            # for imagefile in imagelist:
            for aa in range(len(imagelist)):
                if aa <= 20:
                    print(aa)
                    path, imagename = os.path.split(imagelist[aa])

                    im = Image.open(imagelist[aa])
                    im = im.convert('RGB')
                    im_detail = im.transpose(Image.ROTATE_180)

                    #im_detail = im.filter(ImageFilter.DETAIL)  # 细节增强

                    img_path_gen.append(gpath + 'delete/' + 'idetail_' + imagename)

                    label_gen.extend([int(i)])

                    im_detail.save(gpath + 'delete/' + 'idetail_' + imagename)

                    gen_number += 1

            label_dict = {'img_path': img_path_gen, 'label': label_gen}
            label_gen_dict['img_path'].extend(img_path_gen)
            label_gen_dict['label'].extend(label_gen)
            label_gen_pd = pd.DataFrame(label_dict)
            # label = label.append(label_gen_pd)  # 将生成的图片label加入原先的label
            # label['label'] = label[['label']].astype('int64')  # 转化为int64
            # print(label)
        label_gen_p = pd.DataFrame(label_gen_dict)

        label_gen_p.to_csv(gpath + 'label_delete.csv', index=False)

        print('验证集总共生成%d个图片'%gen_number)
if __name__ == '__main__':
    genImage(train_path, 'train')
    genImage(valid_path, 'valid')

[((23, 16, 23, 48), 'pandas.read_csv', 'pd.read_csv', ({(23, 28, 23, 47): "gpath + 'label.csv'"}, {}), "(gpath + 'label.csv')", True, 'import pandas as pd\n'), ((59, 22, 59, 50), 'pandas.DataFrame', 'pd.DataFrame', ({(59, 35, 59, 49): 'label_gen_dict'}, {}), '(label_gen_dict)', True, 'import pandas as pd\n'), ((72, 16, 72, 48), 'pandas.read_csv', 'pd.read_csv', ({(72, 28, 72, 47): "gpath + 'label.csv'"}, {}), "(gpath + 'label.csv')", True, 'import pandas as pd\n'), ((107, 22, 107, 50), 'pandas.DataFrame', 'pd.DataFrame', ({(107, 35, 107, 49): 'label_gen_dict'}, {}), '(label_gen_dict)', True, 'import pandas as pd\n'), ((20, 15, 20, 45), 'os.path.exists', 'os.path.exists', ({(20, 30, 20, 44): "(gpath + 'delete')"}, {}), "(gpath + 'delete')", False, 'import os\n'), ((21, 12, 21, 39), 'os.makedirs', 'os.makedirs', ({(21, 24, 21, 38): "(gpath + 'delete')"}, {}), "(gpath + 'delete')", False, 'import os\n'), ((55, 27, 55, 51), 'pandas.DataFrame', 'pd.DataFrame', ({(55, 40, 55, 50): 'label_dict'}, {}), '(label_dict)', True, 'import pandas as pd\n'), ((70, 15, 70, 45), 'os.path.exists', 'os.path.exists', ({(70, 30, 70, 44): "(gpath + 'delete')"}, {}), "(gpath + 'delete')", False, 'import os\n'), ((71, 12, 71, 39), 'os.makedirs', 'os.makedirs', ({(71, 24, 71, 38): "(gpath + 'delete')"}, {}), "(gpath + 'delete')", False, 'import os\n'), ((103, 27, 103, 51), 'pandas.DataFrame', 'pd.DataFrame', ({(103, 40, 103, 50): 'label_dict'}, {}), '(label_dict)', True, 'import pandas as pd\n'), ((29, 24, 29, 48), 'numpy.array', 'np.array', ({(29, 33, 29, 47): "li['img_path']"}, {}), "(li['img_path'])", True, 'import numpy as np\n'), ((36, 38, 36, 66), 'os.path.split', 'os.path.split', ({(36, 52, 36, 65): 'imagelist[aa]'}, {}), '(imagelist[aa])', False, 'import os\n'), ((38, 25, 38, 50), 'PIL.Image.open', 'Image.open', ({(38, 36, 38, 49): 'imagelist[aa]'}, {}), '(imagelist[aa])', False, 'from PIL import Image, ImageFilter\n'), ((78, 24, 78, 48), 'numpy.array', 'np.array', ({(78, 33, 78, 47): "li['img_path']"}, {}), "(li['img_path'])", True, 'import numpy as np\n'), ((84, 38, 84, 66), 'os.path.split', 'os.path.split', ({(84, 52, 84, 65): 'imagelist[aa]'}, {}), '(imagelist[aa])', False, 'import os\n'), ((86, 25, 86, 50), 'PIL.Image.open', 'Image.open', ({(86, 36, 86, 49): 'imagelist[aa]'}, {}), '(imagelist[aa])', False, 'from PIL import Image, ImageFilter\n')]