from PIL import Image def average_color(x, y, n, image): r, g, b = 0, 0, 0 count = 0 for s in range(x, x + n + 1): for t in range(y, y + n + 1): pixlr, pixlg, pixlb = image[s, t] r += pixlr g += pixlg b += pixlb count += 1 return ((r / count), (g / count), (b / count)) def differences(rgb_tuple1, rgb_tuple2): r1, g1, b1 = rgb_tuple1 r2, g2, b2 = rgb_tuple2 return ((r1-r2), (g1-g2), (b1-b2)) def threshold(diff_tuple, trigger=4): dr, dg, db = diff_tuple return (((( dr**4 + dg**4 + db**4 ) / 3)**0.25) > trigger) # Thanks to Thomas Atkins, math whiz # # reference_image = Image.open('./lib/test-images/lot-empty.jpg').load() # compare_image = Image.open('./lib/test-images/lot-1-car.jpg').load() # # stall_1 = { # 'location': (54, 79, 109), # 'reference': average_color(54, 79, 109, reference_image), # 'compare': average_color(54, 79, 109, compare_image) # } # # stall_2 = { # 'location': (186, 81, 116), # 'reference': average_color(186, 81, 116, reference_image), # 'compare': average_color(186, 81, 116, compare_image) # } # # stall_3 = { # 'location': (317, 71, 127), # 'reference': average_color(317, 71, 127, reference_image), # 'compare': average_color(317, 71, 127, compare_image) # } # # stall_4 = { # 'location': (469, 76, 100), # 'reference': average_color(469, 76, 100, reference_image), # 'compare': average_color(469, 76, 100, compare_image) # } # # stall_5 = { # 'location': (27, 237, 119), # 'reference': average_color(27, 237, 119, reference_image), # 'compare': average_color(27, 237, 119, compare_image) # } # # stall_6 = { # 'location': (172, 240, 130), # 'reference': average_color(172, 240, 130, reference_image), # 'compare': average_color(172, 240, 130, compare_image) # } # # stall_7 = { # 'location': (317, 234, 144), # 'reference': average_color(317, 234, 144, reference_image), # 'compare': average_color(317, 234, 144, compare_image) # } # # stall_8 = { # 'location': (481, 233, 112), # 'reference': average_color(481, 233, 112, reference_image), # 'compare': average_color(481, 233, 112, compare_image) # } # # print(threshold(differences(stall_1['reference'], stall_1['compare']))) # print(threshold(differences(stall_2['reference'], stall_2['compare']))) # print(threshold(differences(stall_3['reference'], stall_3['compare']))) # print(threshold(differences(stall_4['reference'], stall_4['compare']))) # print(threshold(differences(stall_5['reference'], stall_5['compare']))) # print(threshold(differences(stall_6['reference'], stall_6['compare']))) # print(threshold(differences(stall_7['reference'], stall_7['compare']))) # print(threshold(differences(stall_8['reference'], stall_8['compare'])))