python实现从一组颜色中找出与给定颜色最接近颜色的方法
作者:liuli 时间:2023-04-23 01:48:44
本文实例讲述了python实现从一组颜色中找出与给定颜色最接近颜色的方法。分享给大家供大家参考。具体分析如下:
这段代码非常有用,可以找到指定颜色相似的颜色,比如有一组8个颜色,现在给定一个rgb格式的演示,找出它与8个颜色中的哪一个最接近,如果你需要做一个按照图片颜色搜索图片的程序,这个就非常有用了。
from colorsys import rgb_to_hsv
colors = dict((
((196, 2, 51), "RED"),
((255, 165, 0), "ORANGE"),
((255, 205, 0), "YELLOW"),
((0, 128, 0), "GREEN"),
((0, 0, 255), "BLUE"),
((127, 0, 255), "VIOLET"),
((0, 0, 0), "BLACK"),
((255, 255, 255), "WHITE"),))
def to_hsv( color ):
""" converts color tuples to floats and then to hsv """
return rgb_to_hsv(*[x/255.0 for x in color]) #rgb_to_hsv wants floats!
def color_dist( c1, c2):
""" returns the squared euklidian distance between two color vectors in hsv space """
return sum( (a-b)**2 for a,b in zip(to_hsv(c1),to_hsv(c2)) )
def min_color_diff( color_to_match, colors):
""" returns the `(distance, color_name)` with the minimal distance to `colors`"""
return min( # overal best is the best match to any color:
(color_dist(color_to_match, test), colors[test]) # (distance to `test` color, color name)
for test in colors)
color_to_match = (255,255,0)
print min_color_diff( color_to_match, colors)
希望本文所述对大家的Python程序设计有所帮助。
标签:python,颜色
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
sql server中Select count(*)和Count(1)的区别和执行方式
2024-01-23 11:47:39
![](https://img.aspxhome.com/file/2023/8/110938_0s.png)
调整优化您的LAMP应用程序的5种简单方法
2024-06-05 09:38:57
![](https://img.aspxhome.com/file/2023/6/122536_0s.gif)
python 基于selenium实现鼠标拖拽功能
2022-10-30 17:03:07
利用pandas合并多个excel的方法示例
2021-05-28 00:02:00
![](https://img.aspxhome.com/file/2023/0/86410_0s.png)
Python模拟登陆淘宝并统计淘宝消费情况的代码实例分享
2023-05-01 17:28:00
![](https://img.aspxhome.com/file/2023/8/86218_0s.gif)
JavaScript的for循环中嵌套一个点击事件的问题解决
2024-04-22 13:01:49
python中的集合及集合常用的使用方法
2023-05-04 11:14:11
![](https://img.aspxhome.com/file/2023/2/77832_0s.png)
asp会员系统如何实现“忘记密码”
2007-09-19 12:17:00
如何优化SQL语句的心得浅谈
2024-01-24 00:39:09
python原类、类的创建过程与方法详解
2023-01-26 06:59:27
python3 批量获取对应端口服务的实例
2021-07-14 11:52:05
使用NetBox 编译 asp 为exe应用程序
2009-11-29 16:13:00
Python实现将数据框数据写入mongodb及mysql数据库的方法
2021-10-07 02:24:18
windows下python安装paramiko模块和pycrypto模块(简单三步)
2021-11-29 01:18:40
JQuery获取表单值
2009-11-19 13:17:00
python框架django项目部署相关知识详解
2023-10-26 02:26:15
![](https://img.aspxhome.com/file/2023/8/64658_0s.png)
浏览器用户体验:Firefox初体验 VS The world
2008-08-02 11:58:00
![](https://img.aspxhome.com/file/UploadPic/20088/2/20088212422163s.jpg)
经典的退出浏览器弹窗代码
2008-07-30 12:48:00
带你理解vue中的v-bind
2023-07-02 16:52:44
Python的Flask框架中web表单的教程
2023-05-17 06:11:06
![](https://img.aspxhome.com/file/2023/4/86634_0s.jpg)