import os
import shutil
from PIL import Image
import cv2

# # 변환하고자 하는 폴더의 경로
# folder_path = 'C:/Users/pinb/Desktop/image/silver_oe/'

# # 변경하고자 하는 문자열과 변경될 문자열
# old_string = 'ie'
# new_string = 'oe'

# # 폴더 내의 모든 파일에 대해 반복
# for dir in os.listdir(folder_path):
#     bar_folder = os.path.join(os.path.dirname(folder_path), dir)
#     for filename in os.listdir(bar_folder):
#         if old_string in filename:
#             # 파일의 새 이름 생성
#             new_filename = filename.replace(old_string, new_string)
            
#             # 파일 이름 변경
#             old_file_path = os.path.join(bar_folder, filename)
#             new_file_path = os.path.join(bar_folder, new_filename)
            
#             # 파일 이름 변경 실행
#             os.rename(old_file_path, new_file_path)
#             print(f'Renamed "{filename}" to "{new_filename}"')


image_path = 'C:/Users/pinb/Desktop/image/'
mask_path = 'C:/Users/pinb/Desktop/mask/'

#names = ['gold_ii',
        # 'gold_ie',
#          'gold_io',
#          'gold_ee',
#          'gold_ei',
#          'gold_eo',
#          'gold_oi',
names = ['gold_oe',
         'gold_oo']
        #  'silver_ii',
        #  'silver_ie',
        #  'silver_io',
        #  'silver_ee',
        #  'silver_ei',
        #  'silver_eo',
        #  'silver_oi',
        #  'silver_oe',
        #  'silver_oo']


for name in names:
    image_forder = image_path + name
    # mask_forder = mask_path + name
    # mask_origin = mask_path + name + '_mask.png'
    for dir in os.listdir(image_forder):
        image_bar_folder = os.path.join(image_forder, dir)
        # mask_bar_folder = os.path.join(mask_forder, dir)
        # if not os.path.exists(mask_bar_folder):
        #     os.makedirs(mask_bar_folder)
        for file in os.listdir(image_bar_folder):
            # filename = os.path.splitext(os.path.basename(file))[0] + '_mask.png'
            # new_filename = os.path.join(mask_bar_folder, filename)
            # shutil.copy2(mask_origin, new_filename)
            # print(f'Copied "{mask_origin}" to "{new_filename}"')

            new_filename = os.path.join(image_bar_folder, file)
            png_filename = os.path.splitext(os.path.basename(file))[0] + '.png'
            new_png_file = os.path.join(image_bar_folder, png_filename)
            # with Image.open(new_filename) as img:
            #     # PNG로 저장
            #     img.save(new_png_file, 'PNG')
            #     img.close()
            #     os.remove(new_filename)
            #     print(f'Converted "{new_filename}" to "{new_png_file}"')
            try:
                split = os.path.splitext(os.path.basename(file))[1]
                if split == '.bmp':
                    img = cv2.imread(new_filename, cv2.IMREAD_GRAYSCALE)
                    cv2.imwrite(new_png_file, img)
                    os.remove(new_filename)
                    print(f'Converted "{new_filename}" to "{new_png_file}"')
            except:
                os.remove(new_filename)
                pass
