#!/usr/bin/env python3
# -*- coding: utf-8 -*
import cv2, sys
import os

break_up = None
break_up_point = False

# 重新改变图像大小
def Resize(input_path, output_path, interpolation=False, default_height=1920, default_width=1080):
    global break_up, break_up_point
    # 遍历输入路径
    for root, dirs, files in os.walk(input_path):
        # 遍历所有文件
        for name in files:
            file = os.path.join(root, name)
            if(name == break_up or break_up == None): # 如果没到达断点或者没有断点
                break_up_point = True
            if break_up_point == False:
                continue
            
            if not name.endswith(('.png', '.jpg', '.PNG', '.JPG')):
                continue
                
            if os.path.basename(root) == 'error':
                break
            print("2_2_正在处理", file)
            try:
                # print("convert "+os.path.join(root, name)+" "+os.path.join(input_path, name))
                print("Processing: ","convert \""+file+"\" \""+file+"\"")
                status = os.system("convert \""+file+"\" \""+file+"\"") # 改变文件格式
                # 如果返回状态不为0，则移动对应图片到Error文件夹中
                if status != 0:
                    os.system("echo \""+file+"\" >> error.txt")
                    # 检查文件夹是否存在
                    if not os.path.exists(os.path.join(root, 'error')):
                        # 如果不存在，创建文件夹
                        os.mkdir(os.path.join(root, 'error'))
                    os.system("mv \""+file+"\" \""+os.path.join(root, 'error')+'\"')
                    print("此文件是问题文件 ","mv \""+file+"\" \""+os.path.join(root, 'error')+'\"')
                    continue
                    
                
                # 读取图片
                im = cv2.imread(file)
                height, width, channels = im.shape
                # 判断高度和宽度是否符合要求
                if height == default_height and width == default_width:
                    print("符合要求")
                    continue
                else: 
                    # 是否满足最近临要求
                    if interpolation == False:
                        im = cv2.resize(im, (default_width, default_height))
                    else:
                        im = cv2.resize(im, (default_width, default_height),interpolation = cv2.INTER_NEAREST)
                    # 输出影像
                    if output_path:
                        cv2.imwrite(os.path.join(output_path, name), im,  [cv2.IMWRITE_PNG_COMPRESSION, 100, cv2.IMWRITE_PNG_COMPRESSION, 0])
                    else:
                        print('Resize：' + file)
                        os.system("rm \""+file+"\"") # TODO
                        # 压缩度调为0
                        cv2.imwrite(file, im, [cv2.IMWRITE_PNG_COMPRESSION, 100, cv2.IMWRITE_PNG_COMPRESSION, 0])
            except:
                os.system("echo \""+file+"\" >> error.txt")
                # 检查文件夹是否存在
                if not os.path.exists(os.path.join(root, 'error')):
                    # 如果不存在，创建文件夹
                    os.mkdir(os.path.join(root, 'error'))
                os.system("mv \""+file+"\" \""+os.path.join(root, 'error')+"\"")

if __name__ == '__main__':
    input_path = sys.argv[1] # './2.C组/Group_label_C0'
    output_path = None
    try:
        default_width = int(sys.argv[3])
    except:
        default_width = 1920 # 默认宽度
    try:
        default_height = int(sys.argv[4])
    except:
        default_height = 1080 # 默认高度
    if default_width == 0 or default_height == 0:
        print("发生错误，default_width、default_height不应该为0")
        sys.exit()
    interpolation = sys.argv[2] # 最近临插值
    if interpolation == "False":
        interpolation = False
    elif interpolation == "True":
        interpolation = True
    else:
        print("interpolation must be True or False!")
        quit

    if not os.path.exists(input_path):
        print(input_path)
        print("文件夹不存在!")
    else:
        print("Start to transform_2_2_Resize.py!")
        print(input_path, output_path, default_height,default_width)
        Resize(input_path, output_path, interpolation=interpolation, default_height=default_height, default_width=default_width)
        print("Transform end_2_2_Resize.py!")
