I wrote the following code, but I have a problem that I want the name to be displayed in addition to the color code, but I do not know how I also tested the web color, but for a closer color function, I do not know how to work in the following code What are the code snippet to enter the color name?
import cv2
import webcolors
img_path = r"C:\Users\MJBakhshi\Downloads\color palette.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (700, 500))
clicked = False
r = g = b = xpos = ypos = 0
def draw_function(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
global b, g, r, xpos, ypos, clicked
clicked = True
xpos = x
ypos = y
b, g, r = img[y, x]
b = int(b)
g = int(g)
r = int(r)
cv2.namedWindow('color')
cv2.setMouseCallback('color', draw_function)
colorname = webcolors.rgb_to_name((255, 255, 255))
text=colorname+'r='+str(r)+'b='+str(b)+'g='+str(g)
while(1):
cv2.imshow("color",img)
if (clicked):
text = ' R=' + str(r) + ' G=' + str(g) + ' B=' + str(b)
cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1)
cv2.putText(img, text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA)
if(r+g+b>=600):
cv2.putText(img, text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA)
clicked=False
if cv2.waitKey(20) & 0xFF ==27:
break
cv2.destroyAllWindows()