def merge_mask_img(uid,img_type): import cv2 import numpy as np from PIL import Image try: n_file_name = uid+'/'+img_type+'.png' mask_file_name = uid + '/'+'mask_img.png' n_file = cv2.imread(n_file_name) mask = cv2.imread(mask_file_name) orig_h,orig_w,orig_rgb = n_file.shape #n_array = cv2.resize(n_array,(orig_w,orig_h)) h,w = orig_h,orig_w print(h) print(w) b_channel, g_channel, r_channel = cv2.split(n_file) b_white = b_channel g_white = g_channel r_white = r_channel area_pixels = 0 total_pixels = 0 alpha_channel = np.zeros([h,w]) for y in range(0,h): for x in range(0,w): if mask[y][x][0] == 255: total_pixels = total_pixels + 1 alpha_channel[y][x] = 255 #b_white[y][x] = 255 #g_white[y][x] = 255 #r_white[y][x] = 255 #print('caamh') if g_channel[y][x] == 255: area_pixels = area_pixels + 1 else: alpha_channel[y][x] = 0 b_white[y][x] = 0 g_white[y][x] = 0 r_white[y][x] = 0 if img_type == 'hybrid_blind'and b_white[y][x] == 111 and g_white[y][x] == 111 and r_white[y][x] == 111: alpha_channel[y][x] = 0 alpha_channel = alpha_channel.astype(np.uint8) img_RGBA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel)) #img_white = cv2.merge((b_white, g_white, r_white, alpha_channel)) #n_white_name = uid+'/'+img_type+'_white'+'.png' #sw2 = cv2.imwrite(n_white_name, img_white) sw2 = cv2.imwrite(n_file_name, img_RGBA) print(['area-total-pixels', area_pixels, total_pixels]) return area_pixels, total_pixels except Exception as e: print('merge error') print(e) return 0,1