RBG values to colour name Python

坚强是说给别人听的谎言 提交于 2020-02-06 08:46:09

问题


I was looking to convert given RGB values to the actual colour name in Python or atleast the close match of the colour.

Given the values, [50.2862498 72.08996663 70.66173433] I want to be able to distinguish these values from [185.89894822 186.47802004 185.94613642] as a shade of green.

Reason I need to do that is to remove shades of green from an image.


回答1:


Look at webcolors python package here: https://pypi.org/project/webcolors/1.3/. It looks like the rgb_to_name(rgb_triplet, spec='css3') function does exactly what you want.

From the documentation,

Convert a 3-tuple of integers, suitable for use in an rgb() color triplet, to its corresponding normalized color name, if any such name exists. The optional keyword argument spec determines which specification’s list of color names will be used; valid values are html4, css2, css21 and css3, and the default is css3. If there is no matching name, ValueError is raised.

Example provided:

>>> rgb_to_name((0, 0, 0))
'black'


来源:https://stackoverflow.com/questions/51354637/rbg-values-to-colour-name-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!