Displaying the coordinates of the points clicked on the image using Python-OpenCV

08 Oct, 2021
OpenCV helps us to control and manage different types of mouse events and gives us the flexibility to operate them. There are many types of mouse events. These events can be displayed by running the following code segment :
import cv2
[print(i) for i in dir(cv2) if 'EVENT' in i]
Output : 
EVENT_FLAG_ALTKEY
EVENT_FLAG_CTRLKEY
EVENT_FLAG_LBUTTON
EVENT_FLAG_MBUTTON
EVENT_FLAG_RBUTTON
EVENT_FLAG_SHIFTKEY
EVENT_LBUTTONDBLCLK
EVENT_LBUTTONDOWN
EVENT_LBUTTONUP
EVENT_MBUTTONDBLCLK
EVENT_MBUTTONDOWN
EVENT_MBUTTONUP
EVENT_MOUSEHWHEEL
EVENT_MOUSEMOVE
EVENT_MOUSEWHEEL
EVENT_RBUTTONDBLCLK
EVENT_RBUTTONDOWN
EVENT_RBUTTONUP
Now let us see how to display the coordinates of the points clicked on the image. We will be displaying both the points clicked by right-click as well as left-click.
Algorithm : 
Import the cv2 module.Import the image using the cv2.imread() function.Display the image the image using the cv2.imshow() function.Call the cv2.setMouseCallback() function and pass the image window and the user-defined function as parameters.In the user-defined function, check for left mouse clicks using the cv2.EVENT_LBUTTONDOWN attribute.Display the coordinates on the Shell.Display the coordinates on the created window.Do the same for right mouse clicks using the cv2.EVENT_RBUTTONDOWN attribute. Change the color while displaying the coordinates on the image to distinguish from left clicks.Outside the user-defined function, use the cv2.waitKey(0) and the cv2.destroyAllWindows() functions to close the window and terminate the program.
Import the cv2 module.
Import the image using the cv2.imread() function.
Display the image the image using the cv2.imshow() function.
Call the cv2.setMouseCallback() function and pass the image window and the user-defined function as parameters.
In the user-defined function, check for left mouse clicks using the cv2.EVENT_LBUTTONDOWN attribute.
Display the coordinates on the Shell.
Display the coordinates on the created window.
Do the same for right mouse clicks using the cv2.EVENT_RBUTTONDOWN attribute. Change the color while displaying the coordinates on the image to distinguish from left clicks.
Outside the user-defined function, use the cv2.waitKey(0) and the cv2.destroyAllWindows() functions to close the window and terminate the program.
We will be using the colored version of the Lena image.  
Python3
# importing the moduleimport cv2  # function to display the coordinates of# of the points clicked on the imagedef click_event(event, x, y, flags, params):     # checking for left mouse clicks    if event == cv2.EVENT_LBUTTONDOWN:         # displaying the coordinates        # on the Shell        print(x, ' ', y)         # displaying the coordinates        # on the image window        font = cv2.FONT_HERSHEY_SIMPLEX        cv2.putText(img, str(x) + ',' +                    str(y), (x,y), font,                    1, (255, 0, 0), 2)        cv2.imshow('image', img)     # checking for right mouse clicks        if event==cv2.EVENT_RBUTTONDOWN:         # displaying the coordinates        # on the Shell        print(x, ' ', y)         # displaying the coordinates        # on the image window        font = cv2.FONT_HERSHEY_SIMPLEX        b = img[y, x, 0]        g = img[y, x, 1]        r = img[y, x, 2]        cv2.putText(img, str(b) + ',' +                    str(g) + ',' + str(r),                    (x,y), font, 1,                    (255, 255, 0), 2)        cv2.imshow('image', img) # driver functionif __name__=="__main__":     # reading the image    img = cv2.imread('lena.jpg', 1)     # displaying the image    cv2.imshow('image', img)     # setting mouse handler for the image    # and calling the click_event() function    cv2.setMouseCallback('image', click_event)     # wait for a key to be pressed to exit    cv2.waitKey(0)     # close the window    cv2.destroyAllWindows()
Output :
 
adnanirshad158
surindertarika1234
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python