简体   繁体   English

如何在 python 中将 hsv 转换为 rgb?

[英]How to convert hsv to rgb in python?

I don't know why, but it doesn't work.我不知道为什么,但它不起作用。

I've tried the following code:我试过以下代码:

matplotlib.colors.hsv_to_rgb([[[220 / 255, 255 / 255, 255 / 255]]])

matplotlib.colors.hsv_to_rgb([[[(220 % 360) / 255, 255 / 255, 255 / 255]]])

matplotlib.colors.hsv_to_rgb([[[220, 255, 255]]])

# same with
cv2.cvtColor(np.uint8([[[120, 255, 255]]]), cv2.COLOR_HSV2RGB)

but when I plot it:但是当我 plot 时:

import matplotlib.pyplot as plt
plt.imshow(converted_color)
plt.show()

It's showing some random stuff它显示了一些随机的东西

getting values from GIMP :GIMP获取值:

GIMP 的值


final result最后结果

h, s, v = 270, 100, 100
r, g, b = np.multiply((colorsys.hsv_to_rgb(h/360, s/100, v/100)), 255)
crop[mask == 0] = [r, g, b]

ex e len t!例如!


here is fixed cv2 solution.这是固定的 cv2 解决方案。 h - having 180 maximum. h - 最大 180。 opencv as usual doing thing on his own way... -___- opencv 像往常一样我行我素... -___-

h, s, v = 270, 100, 100
r, g, b = cv2.cvtColor(np.uint8([[[h / 2, (s/100)*255, (v/100)*255]]]), cv2.COLOR_HSV2RGB)[0][0]
crop[mask == 0] = [r, g, b]

Try using colorsys:尝试使用 colorsys:

import colorsys
colorsys.hsv_to_rgb(0.5, 0.5, 0.4)

Output: Output:

(0.2, 0.4, 0.4)

Edit编辑

An example could be:一个例子可能是:

import matplotlib
import matplotlib.pyplot as plt
import colorsys

h, s, v = 0.83, 1.0, 0.50
#RGB from 0 to 1 NO from 0 to 255
r, g, b = colorsys.hsv_to_rgb(h, s, v)
print(r, g, b)

rect1 = matplotlib.patches.Rectangle((-200, -100), 400, 200, color=[r, g, b])
plt.axes().add_patch(rect1)
plt.show()

在此处输入图像描述

To see why you may be having troubles in displaying color using imshow refer to: Printing one color using imshow要了解使用 imshow 显示颜色时可能遇到问题的原因,请参阅: 使用 imshow 打印一种颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM