#################
#cao
import cv2
import os
def process_image(img, min_side):
    size = img.shape
    h, w = size[0], size[1]
    #长边缩放为min_side
    scale = max(w, h) / float(min_side)
    new_w, new_h = int(w/scale), int(h/scale)
    resize_img = cv2.resize(img, (new_w, new_h))
    # 填充至min_side * min_side
    if new_w % 2 != 0 and new_h % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2, (min_side-new_h)/2, (min_side-new_w)/2 + 1, (min_side-new_w)/2
    elif new_h % 2 != 0 and new_w % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2 + 1, (min_side-new_h)/2, (min_side-new_w)/2, (min_side-new_w)/2
    elif new_h % 2 == 0 and new_w % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2, (min_side-new_h)/2, (min_side-new_w)/2, (min_side-new_w)/2
    else:
        top, bottom, left, right = (min_side-new_h)/2 + 1, (min_side-new_h)/2, (min_side-new_w)/2 + 1, (min_side-new_w)/2
    pad_img = cv2.copyMakeBorder(resize_img, int(top), int(bottom), int(left), int(right), cv2.BORDER_CONSTANT, value=[0,0,0]) #从图像边界向上,下,左,右扩的像素数目

    return pad_img

if __name__ == '__main__':
    #支持多文件夹
    rootpath = "E:\\\\"
    savepath ="E:\\\2\\"
    for i in os.listdir(rootpath):

        imgpath = os.path.join(rootpath,i)
        saveimgpath = os.path.join(savepath, i)
        if not os.path.exists(saveimgpath):
            os.mkdir(saveimgpath)
        for j in os.listdir(imgpath):
            images = os.path.join(imgpath, j)
            print(images)
            image = cv2.imread(images)
            img_new = process_image(image, 96)
            cv2.imwrite(saveimgpath +'\\'+j,img_new)