How to Convert RGB Color to English Color Name in Python?
Convert RGB Color to English Color Name in Python
Given a tuple of RGB values, you may want to convert it to a human-readable English color name. How can this be accomplished using Python?
Solution
To convert an RGB color to its corresponding English color name, consider using the webcolors library. It offers the rgb_to_name() function, which takes an RGB triplet as input and returns the normalized color name if a match exists.
Example
<code class="python">import webcolors # Get RGB color from an image im = Image.open("test.jpg") n, color = max(im.getcolors(im.size[0]*im.size[1])) # Convert to English color name color_name = webcolors.rgb_to_name(color, spec='css3') print(color_name) # Output: 'cadetblue'</code>
Closest Color Name
If a perfect match is not found, you can find the closest color name using the following code:
<code class="python">def closest_colour(requested_colour): # Iterate through RGB space and calculate Euclidian distance min_distances = {} for hex_code, name in webcolors.CSS3_HEX_TO_NAMES.items(): r, g, b = webcolors.hex_to_rgb(hex_code) dist = sum((n - c for n, c in zip(requested_colour, (r, g, b))))**2 min_distances[dist] = name # Return the closest color name return min_distances[min(min_distances.keys())]</code>
Complete Function
To handle both cases, you can define a function that returns the actual and closest color names:
<code class="python">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</code>
Sample Usage
<code class="python">color = (119, 172, 152) actual_name, closest_name = get_colour_name(color) print("Actual:", actual_name, ", Closest:", closest_name)</code>
Output
Actual: None, Closest: cadetblue
The above is the detailed content of How to Convert RGB Color to English Color Name in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used
