Detecting color where the mouse cursor click

I wrote a code to detect the RGB code of the point that I click by mouse.
to test my program I draw some shapes in paint software of windows and each shape has a specific color (RGB).
when I click in different places on the paint I get data but it’s not true and generally, I get (255,255,255).
why is that? and how should I solve it?
thank you
<I’am new to python and opencv but I have basic knowledge of c++.>
here is my code :

import cv2

def click_data(event, x, y, flags, param):

  if (event == cv2.EVENT_LBUTTONDOWN):
     print(x,' , ', y)
     font = cv2.FONT_HERSHEY_SIMPLEX;
     blue = img[x,y,0];
     green = img[x,y,1];
     red = img[x,y,2];
     text = str(red) + ',' + str(green) + ',' + str(blue);
     font = cv2.FONT_HERSHEY_SIMPLEX;
     cv2.putText(img,text,(x,y),font,1,(0,255,255),1,cv2.LINE_AA);
     cv2.imshow('imagename',img);

path = 'D:\opencv\paint.jpg';
img = cv2.imread(path); 
cv2.imshow('imagename',img); 
cv2.setMouseCallback('imagename',click_data);
cv2.waitKey(0);
cv2.destroyAllWindows();

#end of the code

numpy array indexing is [row,column] or [y,x], like a matrix.

2 Likes

Thank you.
this is the answer.