'color classification algorithm for python

I am in need of an algorithm that classifies the colors. I have scraped colors from images and of course, the result is 6000 different colors, which is useless for any statistical analysis.

Below is a try but the code grew up so fast and messy that my mind exploded and I just can't handle it anymore and still, there are many colors that do not look at all like blue,red,green,yellow,cyan,magenta .

any ideas?

from contextlib import redirect_stderr
from genericpath import samefile
from pickle import TRUE
from PIL import ImageColor
from yachalk import chalk
import os

with open('colors.txt', 'r') as f:
    colors = f.readlines()


global chosen_color
# chosen_color = "#fff200"
chosen_color = ""

def get_rgb(color):
    global red 
    red = 0
    red = ImageColor.getcolor(color, "RGB")[0]
    global green 
    green = 0
    green = ImageColor.getcolor(color, "RGB")[1]
    global blue 
    blue = 0
    blue = ImageColor.getcolor(color, "RGB")[2]

    print(red, green, blue)

    global same
    if ((abs(abs(red) - abs(green))) <=20) and ((abs(abs(red) - abs(blue))) <=20) and ((abs(abs(green) - abs(blue))) <=20):
        same = 1
    else:
        same = 0

    if ((red >= 30) and (green >= 30) and (blue >= 30)) and ((red <= 220) and (green <= 220) and (blue <= 220)) and (same == 1):
        # print("grey")
        print(chalk.bg_hex(chosen_color).black("grey"))
    elif ((red < 30) and (green < 30) and (blue < 30)) and (same == 1):
        # print("black")
        print(chalk.bg_hex(chosen_color).black("black"))
    elif ((red > 220) and (green > 220) and (blue > 220)) and (same == 1):
        # print("white")
        print(chalk.bg_hex(chosen_color).black("white"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and (red > green) and (red > blue) and (not(abs(abs(red) - abs(green))) <=50):
        print(chalk.bg_hex(chosen_color).black("red"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and (green > red) and (green > blue) and (not(abs(abs(blue) - abs(green))) <=50):
        print(chalk.bg_hex(chosen_color).black("green"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and (blue > red) and (blue > green) and (not(abs(abs(blue) - abs(red))) <=50) and (not(abs(abs(blue) - abs(green))) <=50):
        print(chalk.bg_hex(chosen_color).black("blue"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and ((abs(abs(red) - abs(green))) <=50) and ((red > blue) or (green > blue)) and abs(abs(blue) - abs(red) >=50) and abs(abs(blue) - abs(green) >=50):
        print(chalk.bg_hex(chosen_color).black("yellow"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and ((abs(abs(blue) - abs(green))) <=50) and ((blue > red) or (green > red)):
        print(chalk.bg_hex(chosen_color).black("cyan"))
    elif ((red >= 30) or (green >= 30) or (blue >= 30)) and ((red <= 220) or (green <= 220) or (blue <= 220)) and ((abs(abs(blue) - abs(red))) <=50) and ((blue > green) or (red > green)):
        print(chalk.bg_hex(chosen_color).black("magenta"))
    else:
        print("not grey")
        print(chalk.bg_hex(chosen_color).black("other"))



for color in colors:
    chosen_color = "#"+color.split('\n')[0]
    try:
        get_rgb(chosen_color)
    except:
        # print('wrong color code')
        continue


Solution 1:[1]

https://webcolors.readthedocs.io/en/1.11.1/

so simple. That was the only thing I neded.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giannis Koutivas