2

I need to get the color of a pixels in a specifict region of image. Im using this script in python:

import cv2

image = cv2.imread('abc.jpg')

color = image[100,50]

print(color) # gives me the RGB color (12,156,222)

and if a need to get the hex of it:

hex = (color[0] << 16) + (color[1] << 8) + (color[2])

my question is: There is a way to tell me what color is it? (12,156,222) thank you.

2
  • "color" meaning as a hue in degrees on a color wheel, or color described as a word? Commented Nov 29, 2020 at 18:28
  • Not all rgb colors have names. You would have to create a list of color names and their associated rgb values and find the closest named color to your rgb values. See the CSS list of named colors. Commented Nov 29, 2020 at 19:04

2 Answers 2

1

I found another way to solve this problem using webcolor i was able to detect the closest color

import webcolors
import time
start = time.time()
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 = (255, 0, 0)
actual_name, closest_name = get_colour_name(requested_colour)

print("Actual colour name:", actual_name, ", closest colour name:", closest_name)
print("Tempo: ", time.time() - start)

if anyone have another bester method please send here ^^

Sign up to request clarification or add additional context in comments.

Comments

0

You can look it up at: https://shallowsky.com/colormatch/index.php?r=12&g=156&b=222

[spoiler alert: "DeepSkyBlue3"]

In general, you want to start with

  1. a list of name → R,G,B values, and
  2. a distance function

Then it's a matter of computing distance between each of those and your target value. Return the corresponding name for whichever list entry has minimum distance to target.

A previous SO answer offers such an algorithm: Python - Find similar colors, best way

Using Euclidean distance (L2 norm) in some random color space is less than principled, though often sufficient. If you want to sweat the details, consider relying on https://python-colormath.readthedocs.io. (You might even want to discuss maintainership with the author.)

If "perceptually similar" matters to you, definitely use Lab: https://en.wikipedia.org/wiki/CIELAB_color_space

2 Comments

its to advanced for me :( could you help me or give me some python example?
I cited a brief python solution: stackoverflow.com/questions/8863810/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.