首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蟒蛇的颜色分类算法

蟒蛇的颜色分类算法
EN

Stack Overflow用户
提问于 2022-03-02 18:21:19
回答 1查看 170关注 0票数 -1

我需要一种分类颜色的算法。我从图像中刮过颜色,当然,结果是6000种不同的颜色,这对任何统计分析都是无用的。

下面是一个尝试,但代码发展得如此之快和混乱,以至于我的头脑爆发,我只是无法再处理它了,但是,仍然有许多颜色看起来一点也不像蓝色,红色,绿色,黄色,青色,洋红色。

有什么想法吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-03-03 14:38:25

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

太简单了。这是我唯一想到的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71327440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档