import os
import shutil
from PIL import Image
import cv2

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',
#          'gold_oe',
#          'gold_oo',
names = ['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
    for dir in os.listdir(image_forder):
        image_bar_folder = os.path.join(image_forder, dir)
        for file in os.listdir(image_bar_folder):
            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)
            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}"')
            # 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}"')
