From bb45aef4fe5e5bb33be4bf3f142e0e73196aabfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E5=BB=BA=E6=98=8E?= Date: Fri, 11 Aug 2017 16:29:03 +0800 Subject: [PATCH] change the divided difference calculation method --- demo/demo.py | 6 ++---- haishoku/alg.py | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/demo/demo.py b/demo/demo.py index fa6c7d8..59ce8b6 100644 --- a/demo/demo.py +++ b/demo/demo.py @@ -4,22 +4,20 @@ from haishoku.haishoku import Haishoku def main(): - path = "demo_01.png" + path = "/Users/wujianming/Desktop/WechatIMG18547.jpeg" # path = "http://wx2.sinaimg.cn/large/89243dfbly1ffoekfainzj20dw05k0u7.jpg" # getPalette api palette = Haishoku.getPalette(path) - print(palette) # getDominant api dominant = Haishoku.getDominant(path) - print(dominant) # showPalette api Haishoku.showPalette(path) # showDominant api - Haishoku.showDominant(path) + # Haishoku.showDominant(path) # Haishoku object h = Haishoku.loadHaishoku(path) diff --git a/haishoku/alg.py b/haishoku/alg.py index 9527313..f3d7a9c 100644 --- a/haishoku/alg.py +++ b/haishoku/alg.py @@ -13,13 +13,52 @@ def sort_by_rgb(colors_tuple): sorted_tuple = sorted(colors_tuple, key=lambda x:x[1]) return sorted_tuple +def rgb_maximum(colors_tuple): + """ + colors_r max min + colors_g max min + colors_b max min + + """ + r_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][0]) + g_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][1]) + b_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][2]) + + r_min = r_sorted_tuple[0][1][0] + g_min = g_sorted_tuple[0][1][1] + b_min = b_sorted_tuple[0][1][2] + + r_max = r_sorted_tuple[len(colors_tuple)-1][1][0] + g_max = g_sorted_tuple[len(colors_tuple)-1][1][1] + b_max = b_sorted_tuple[len(colors_tuple)-1][1][2] + + return { + "r_max":r_max, + "r_min":r_min, + "g_max":g_max, + "g_min":g_min, + "b_max":b_max, + "b_min":b_min, + "r_dvalue":(r_max-r_min)/3, + "g_dvalue":(g_max-g_min)/3, + "b_dvalue":(b_max-b_min)/3 + } + def group_by_accuracy(sorted_tuple, accuracy=3): """ group the colors by the accuaracy was given the R G B colors will be depart to accuracy parts default accuracy = 3 - - [0, 85), [85, 170), [170, 256) + d_value = (max-min)/3 + [min, min+d_value), [min+d_value, min+d_value*2), [min+d_value*2, max) """ + rgb_maximum_json = rgb_maximum(sorted_tuple) + r_min = rgb_maximum_json["r_min"] + g_min = rgb_maximum_json["g_min"] + b_min = rgb_maximum_json["b_min"] + r_dvalue = rgb_maximum_json["r_dvalue"] + g_dvalue = rgb_maximum_json["g_dvalue"] + b_dvalue = rgb_maximum_json["b_dvalue"] + rgb = [ [[[], [], []], [[], [], []], [[], [], []]], [[[], [], []], [[], [], []], [[], [], []]], @@ -30,9 +69,9 @@ def group_by_accuracy(sorted_tuple, accuracy=3): r_tmp_i = color_tuple[1][0] g_tmp_i = color_tuple[1][1] b_tmp_i = color_tuple[1][2] - r_idx = 0 if r_tmp_i < 85 else 1 if r_tmp_i < 170 else 2 - g_idx = 0 if g_tmp_i < 85 else 1 if g_tmp_i < 170 else 2 - b_idx = 0 if b_tmp_i < 85 else 1 if b_tmp_i < 170 else 2 + r_idx = 0 if r_tmp_i < (r_min+r_dvalue) else 1 if r_tmp_i < (r_min+r_dvalue*2) else 2 + g_idx = 0 if g_tmp_i < (g_min+g_dvalue) else 1 if g_tmp_i < (g_min+g_dvalue*2) else 2 + b_idx = 0 if b_tmp_i < (b_min+b_dvalue) else 1 if b_tmp_i < (b_min+b_dvalue*2) else 2 rgb[r_idx][g_idx][b_idx].append(color_tuple) return rgb