I want to convert a color tuple to a color name, like \'yellow\' or \'blue\'
>>> im = Image.open(\"test.jpg\")
>>> n, color = max(im.getcol
For those who, like me, want a more familiar colour name, you can use the CSS 2.1 colour names, also provided by webcolors
:
#00ffff
#000000
#0000ff
#ff00ff
#008000
#808080
#00ff00
#800000
#000080
#808000
#800080
#ff0000
#c0c0c0
#008080
#ffffff
#ffff00
#ffa500
Just use fraxel's excellent answer and code for getting the closest colour, adapted to CSS 2.1:
def get_colour_name(rgb_triplet):
min_colours = {}
for key, name in webcolors.css21_hex_to_names.items():
r_c, g_c, b_c = webcolors.hex_to_rgb(key)
rd = (r_c - rgb_triplet[0]) ** 2
gd = (g_c - rgb_triplet[1]) ** 2
bd = (b_c - rgb_triplet[2]) ** 2
min_colours[(rd + gd + bd)] = name
return min_colours[min(min_colours.keys())]