ABOUT ME

notou10@yonsei.ac.kr

Today
Yesterday
Total
  • 이미지에 숫자 (이쁘게) 삽입하기
    카테고리 없음 2023. 3. 15. 17:02

    결과 

     

     

     

    def insert_value(pil_imgs, val, plot_path):
        pil_img = pil_imgs.copy() #pil_imgs는 img = Image.open(img_path)로 연거
        draw = ImageDraw.Draw(pil_img)
        text = val[0].detach().cpu().numpy() 
        #val = val[0].detach().cpu().numpy() 
        texts =text.astype(int)
    
        font = ImageFont.truetype("./arial.ttf", 70)
        color = (0,0,255)
    
        draw.text((5,5),f"{texts}", color,font=font ,align ="left")
        #import pdb; pdb.set_trace()
        return np.array(pil_img)
        
        
        
      def save_grid_imgs(sorted_dict, save_img_num,experiment_type,n_attr,real_dir):
        plot_path = f"/workspace/dataset/dongkyun/PROJECTS/entangle_fid/exp4/{experiment_type}/n_attr_{n_attr}/{real_dir.split('/')[-1]}"
      
        plt.close()
        if not os.path.exists(plot_path):
            os.makedirs(plot_path)
        #transform = transforms.ToTensor()
        transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Resize((256,256))
        ])
        best_dir, worst_dir = [], []
    
        for index, stats in tqdm(enumerate(range(save_img_num)),total = save_img_num):
            best = sorted_dict[index] 
            worst = sorted_dict[-(index+1)] 
            best_dir.append((best[0],best[1]['score']))
            worst_dir.append((worst[0],worst[1]['score']))
        
        #for i in range(save_img_num):
        output_tensor_array = torch.zeros(save_img_num, 3, 256, 256)
        #best
        for index, (img_path,score) in enumerate(best_dir):
            img = Image.open(img_path)
            #import pdb; pdb.set_trace()
            img = insert_value(img, score, plot_path) 
            img = transform(img)
            img.to('cuda')
            output_tensor_array[index] = img
        grid = utils.make_grid(output_tensor_array, nrow = 10)
        plt.imshow(grid.permute(1, 2, 0))
        plt.axis('off')
        plt.savefig(f"{plot_path}/top_oods.png", dpi = 500)
        #worst
    
        plt.close()
        for index, (img_path,score) in enumerate(worst_dir):
            img = Image.open(img_path)
            img = insert_value(img, score, plot_path)
            img = transform(img)
            img.to('cuda')
            output_tensor_array[index] = img
        grid = utils.make_grid(output_tensor_array, nrow = 10)
        plt.imshow(grid.permute(1, 2, 0))
        plt.axis('off')
        plt.savefig(f"{plot_path}/bottom_oods.png", dpi = 500)
    
    #main 문에서
    save_grid_imgs(sorted_dict, save_img_num, experiment_type, n_attr, img_dir_g)

     

Designed by Tistory.