from PIL import Image  
import os 

def merge_imgs(path1, path2, save_path):
    for img_name in os.listdir(path1):
        # img_dir1 = os.path.join(path1, img_dir)
        # if os.path.isdir(img_dir1):
        # img_dir2 = os.path.join(path2, img_dir)
        # for img_name in os.listdir(img_dir1):
        try:
            img1_path = os.path.join(path1, img_name)
            img2_path = os.path.join(path2, img_name)

            img1 = Image.open(img1_path)
            img2 = Image.open(img2_path)
            print('img1_path',img1)
            print('img2_path', img2)
            assert img1.height == img2.height


            new_img = Image.new('RGB', (img1.width + img2.width+10, img1.height))

            # print('new_img', new_img)
            new_img.paste(img1, (0, 0))
            new_img.paste(img2, (img1.width+10, 0))
            # save_dir = os.path.join(save_path, img_name)
            os.makedirs(save_path, exist_ok=True)
            img_save = os.path.join(save_path, img_name)
            # print('img-save', img_save)
            new_img.save(img_save)
        except Exception as e:
            print(e)
            #print(img_name)
        
        
#path1 = '/home/yujia/yj/yolov5-6.1/0518_cls10_v5s_new_delTP0.5/labelFn_5/'
# path1 = 'predict5_ori_v10s/'
# path1 = 'predict_best0524/'
# path2 = 'predict_0613_epoch27/'

# path1 = 'predict_labels110_0524/'
# path2 = 'predict_labels110_0613/'

path1 = '/home/lc/ieemoo-ai-gift/confusion_gift_cls2_0.45/FP_bg/FP_bg_1'
path2 = '/home/lc/ieemoo-ai-gift/confusion_gift_cls2_0.45/allBox/allBox_1'

#save_path = '/home/yujia/yj/yolov5-6.1/0518_cls10_v5s_new_delTP0.5/labelFn_5_allBox_merge/'
save_path = '/home/lc/ieemoo-ai-gift/confusion_gift_cls2_0.45/FP_bg/FP_bg_1_joint/'
# os.makedirs(save_path, exist_ok=True)
merge_imgs(path1, path2, save_path)
