def merge_contour_images(uid,img_type,mask,centery,centerx,fieldid,imagedate): import numpy as np import cv2 from PIL import Image import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents n_file_name = uid+'_'+img_type+'.png' etci_file_name = uid+'_ETCI.png' n_file = cv2.imread(n_file_name) etci = cv2.imread(etci_file_name) mask_h,mask_w = mask.shape orig_h,orig_w,orig_rgb = etci.shape h,w = orig_h,orig_w # print(mask_h) # print(mask_w) new_img = np.zeros([h,w,3]) mask_half_height = round(mask_h/2) mask_half_width= round(mask_w/2) centerx = round(w/2) centery = round(h/2) y_start_mask = centery-mask_half_height y_end_mask = centery+mask_half_height x_start_mask = centerx-mask_half_width x_end_mask = centerx+mask_half_width # print(y_start_mask) # print(y_end_mask) # print(x_start_mask) # print(x_end_mask) temp_x = 0 temp_y = 0 # print('hw') # print(h) # print(w) health_count = 0 mask_pixel_area = 0 for py in range(0,h): for px in range(0,w): if (py>=y_start_mask) and (py<=y_end_mask-2) and (px>=x_start_mask) and (px<=x_end_mask-2): #print(px-x_start_mask) if mask[py-y_start_mask][px-x_start_mask]==255: new_img[py][px][0]=n_file[py][px][0] new_img[py][px][1]=n_file[py][px][1] new_img[py][px][2]=n_file[py][px][2] mask_pixel_area = mask_pixel_area+1 if (n_file[py][px][0] == 6) and (n_file[py][px][1]==101) and (n_file[py][px][2]==61): health_count = health_count+1 elif (n_file[py][px][0] == 17) and (n_file[py][px][1]==167) and (n_file[py][px][2]==95): health_count = health_count+0.9 elif (n_file[py][px][0] == 129) and (n_file[py][px][1]==191) and (n_file[py][px][2]==108): health_count = health_count+0.8 elif (n_file[py][px][0] == 186) and (n_file[py][px][1]==227) and (n_file[py][px][2]==131): health_count = health_count+0.7 elif (n_file[py][px][0] == 230) and (n_file[py][px][1]==243) and (n_file[py][px][2]==164): health_count = health_count+0.6 elif (n_file[py][px][0] == 255) and (n_file[py][px][1]==240) and (n_file[py][px][2]==181): health_count = health_count+0.5 elif (n_file[py][px][0] == 251) and (n_file[py][px][1]==192) and (n_file[py][px][2]==126): health_count = health_count+0.4 elif (n_file[py][px][0] == 247) and (n_file[py][px][1]==136) and (n_file[py][px][2]==90): health_count = health_count+0.3 elif (n_file[py][px][0] == 234) and (n_file[py][px][1]==79) and (n_file[py][px][2]==59): health_count = health_count+0.2 else: health_count = health_count+0.1 else: new_img[py][px][0]=etci[py][px][0] new_img[py][px][1]=etci[py][px][1] new_img[py][px][2]=etci[py][px][2] else: new_img[py][px][0]=etci[py][px][0] new_img[py][px][1]=etci[py][px][1] new_img[py][px][2]=etci[py][px][2] if mask_pixel_area ==0: health_percentage=0 else: health_percentage = round(health_count*100/mask_pixel_area) db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').child(img_type).child(imagedate).set(str(health_percentage)) mn_file_name = uid+'_m'+img_type+'.png' #new_img = Image.fromarray(new_img) #status = cv2.imwrite(mn_file_name,new_img) #new_img.save(mn_file_name,"PNG") #new_img = Image.open(mn_file_name) #etci_orig = Image.open(etci_file_name) #etci_orig = etci_orig.convert("RGBA") #new_img = new_img.convert("RGBA") #new_img = new_img.resize((orig_w,orig_h),Image.NEAREST) #new_img = Image.blend(etci_orig,new_img,0.6) #new_img.save(mn_file_name,"PNG") return