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

def transform(input_path, output_path):
    for root, dirs, files in os.walk(input_path):
        for name in files:
            print("2_1_正在检索", os.path.join(root, name))
            # 如果图片是jpg图片
            if name.endswith('.jpg') or name.endswith('.JPG') :
                # convert一下图片
                print("convert \""+os.path.join(root, name)+"\" \""+os.path.join(input_path, name)+"\"")
                os.system("convert \""+os.path.join(root, name)+"\" \""+os.path.join(input_path, name)+"\"")
                file = os.path.join(root, name)
                if os.path.basename(root) == 'error':
                    break
                try:
                    # 读取图片并且改变存储方式
                    im = cv2.imread(file)
                    if output_path:
                        # 压缩度调为0
                        cv2.imwrite(os.path.join(output_path, name.replace('jpg', 'png').replace('JPG', 'png')), im, [cv2.IMWRITE_PNG_COMPRESSION, 100, cv2.IMWRITE_PNG_COMPRESSION, 0])
                    else:
                        print('transform：' + file.replace('jpg', 'png').replace('JPG', 'png'))
                        os.system("rm \""+file+"\"")
                        # 压缩度调为0
                        cv2.imwrite(file.replace('jpg', 'png').replace('JPG', 'png'), 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'))
                # 如果图片是jpg图片
            if name.endswith('.bmp') or name.endswith('.BMP') :
                # convert一下图片
                print("convert "+os.path.join(root, name)+" "+os.path.join(input_path, name))
                os.system("convert "+os.path.join(root, name)+" "+os.path.join(input_path, name))
                file = os.path.join(root, name)
                if os.path.basename(root) == 'error':
                    break
                try:
                    # 读取图片并且改变存储方式
                    im = cv2.imread(file)
                    if output_path:
                        # 压缩度调为0
                        cv2.imwrite(os.path.join(output_path, name.replace('bmp', 'png').replace('BMP', 'png')), im, [cv2.IMWRITE_PNG_COMPRESSION, 100, cv2.IMWRITE_PNG_COMPRESSION, 0])
                    else:
                        print('transform：' + os.path.join(root, name.replace('bmp', 'png').replace('BMP', 'png')))
                        os.system("rm \""+file+"\"")
                        # 压缩度调为0
                        cv2.imwrite(os.path.join(root, name.replace('bmp', 'png').replace('BMP', 'png')), 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
    if not os.path.exists(input_path):
        print("文件夹不存在!")
    else:
        print("Start to transform 2_1_Trans_to_png!")
        transform(input_path, output_path)
        print("Transform end 2_1_Trans_to_png!")
        print()
