【发布时间】:2021-12-02 08:01:22
【问题描述】:
我有这段代码为给定的百分比值绘制一个米。如何更改它以获得底部颜色和顶部颜色的两个参数(即从 color1 到 color2 的阴影,如本例中的浅绿色到深绿色)。另外,x, y 参数指定放置此仪表的坐标?
我尝试了一些更改,但没有成功。
import cv2
import numpy
def draw_indicator(img, percentage):
def percentage_to_color(p):
return 0, 255 * p, 255 - (255 * p)
# config
levels = 10
indicator_width = 80
indicator_height = 220
level_width = indicator_width - 20
level_height = int((indicator_height - 20) / levels - 5)
# draw
img_levels = int(percentage * levels)
cv2.rectangle(img, (10, img.shape[0] - (indicator_height + 10)), (10 + indicator_width, img.shape[0] - 10), (0, 0, 0), cv2.FILLED)
for i in range(img_levels):
level_y_b = int(img.shape[0] - (20 + i * (level_height + 5)))
cv2.rectangle(img, (20, level_y_b - level_height), (20 + level_width, level_y_b), percentage_to_color(i / levels), cv2.FILLED)
# test code
img = cv2.imread('a.jpg')
draw_indicator(img, 0.7)
cv2.imshow("test", img)
cv2.waitKey(10000)
【问题讨论】:
-
根据自己的喜好更改
percent_to_color的实现。 -
是的,需要做出改变
标签: python python-3.x opencv cv2 imshow