栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 图片背景颜色识别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 图片背景颜色识别

imimport colorsys
import PIL.Image as Image
 
# 函数生成的是RGB
# RGB转16进制 https://www.sioe.cn/yingyong/yanse-rgb-16/
def get_dominant_color(image):
    max_score = 0.0001
    dominant_color = None
    for count,(r,g,b) in image.getcolors(image.size[0]*image.size[1]):
        # 转为HSV标准
        saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1]
        y = min(abs(r*2104+g*4130+b*802+4096+131072)>>13,235)
        y = (y-16.0)/(235-16)
        #忽略高亮色
        if y > 0.9:
            continue
        score = (saturation+0.1)*count
        if score > max_score:
            max_score = score
            dominant_color = (r,g,b)
    return dominant_color


# RGB转16进制函数
def RGB_to_Hex(tmp):
    rgb = tmp.split(',')#将RGB格式划分开来
    strs = '#'
    for i in rgb:
        num = int(i)#将str转int
        #将R、G、B分别转化为16进制拼接转换并大写
        strs += str(hex(num))[-2:].replace('x','0').upper()
    return strs

if __name__ == '__main__':
    image = Image.open('blue.jpg')
    image = image.convert('RGB')
    r=get_dominant_color(image)
    print(r)
    rgb_str=",".join([ str(x) for x in list(r)] )
    print(rgb_str)
    hexstr=RGB_to_Hex(rgb_str)
    print(hexstr)

效果还可以。先记录一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号