Convert RGB color to English color name, like 'green' with Python

PythonCssImageColorsTuples

Python Problem Overview


I want to convert a color tuple to a color name, like 'yellow' or 'blue'

>>> im = Image.open("test.jpg")
>>> n, color = max(im.getcolors(im.size[0]*im.size[1]))
>>> print color
(119, 172, 152)

Is there a simple way in python to do this?

Python Solutions


Solution 1 - Python

It looks like webcolors will allow you to do this:

> rgb_to_name(rgb_triplet, spec='css3') > >Convert a 3-tuple of integers, suitable for use in an rgb() color triplet, to its corresponding normalized color name, if any such name exists; valid values are html4, css2, css21 and css3, and the default is css3.

>Example:

>>> rgb_to_name((0, 0, 0))
'black'

it is vice-versa-able:

>>> name_to_rgb('navy')
(0, 0, 128)

#To find the closest colour name:

However webcolors raises an exception if it can't find a match for the requested colour. I've written a little fix that delivers the closest matching name for the requested RGB colour. It matches by Euclidian distance in the RGB space.

import webcolors

def closest_colour(requested_colour):
    min_colours = {}
    for key, name in webcolors.css3_hex_to_names.items():
        r_c, g_c, b_c = webcolors.hex_to_rgb(key)
        rd = (r_c - requested_colour[0]) ** 2
        gd = (g_c - requested_colour[1]) ** 2
        bd = (b_c - requested_colour[2]) ** 2
        min_colours[(rd + gd + bd)] = name
    return min_colours[min(min_colours.keys())]

def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name

requested_colour = (119, 172, 152)
actual_name, closest_name = get_colour_name(requested_colour)

print "Actual colour name:", actual_name, ", closest colour name:", closest_name

Output:

Actual colour name: None , closest colour name: cadetblue

Solution 2 - Python

There is a program called pynche which can change RGB to colour name in English for Python.

You can try to use the method ColorDB.nearest() in ColorDB.py which can do what you want.

You can find more information about this method here : ColorDB Pynche

Solution 3 - Python

For those who, like me, want a more familiar colour name, you can use the CSS 2.1 colour names, also provided by webcolors:

  • aqua: #00ffff
  • black: #000000
  • blue: #0000ff
  • fuchsia: #ff00ff
  • green: #008000
  • grey: #808080
  • lime: #00ff00
  • maroon: #800000
  • navy: #000080
  • olive: #808000
  • purple: #800080
  • red: #ff0000
  • silver: #c0c0c0
  • teal: #008080
  • white: #ffffff
  • yellow: #ffff00
  • orange: #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())]

Solution 4 - Python

A solution to your problem consists in mapping the RGB values to the HSL color space.

Once you have the color in the HSL color space you can use the H (hue) component to map it the color. Note that color is a somewhat subjective concept, so you would have to define which ranges of H corresponds to a given color.

Solution 5 - Python

The best solution I've found to solve this problem is the one provided by tux21b on this post:
https://stackoverflow.com/questions/2453344/find-the-colour-name-from-a-hexadecimal-colour-code/65074672#65074672<br /> With the fix I've made (to avoid the division by zero error).
It is (from my understanding) an approximation calculation, that doesn't require to load a large set of data table to get an approaching value, and that allow to set your own set of needed colors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeVaughanView Question on Stackoverflow
Solution 1 - PythonfraxelView Answer on Stackoverflow
Solution 2 - PythonChapMicView Answer on Stackoverflow
Solution 3 - PythonfiatjafView Answer on Stackoverflow
Solution 4 - PythonAlceu CostaView Answer on Stackoverflow
Solution 5 - PythonCutMaster ProdView Answer on Stackoverflow