Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Change Mouse Cursor Style while parsing through an image in OpenCV

heisenberg_1995

New Coder
I want to change my cursor style from default to cross while parsing through an image. To do that I came across Tkinter, So, how can I integrate the following code with Tkinter? I can change the cursor style in a Tkinter window. Any other solution is also acceptable for me.



Code:
import cv2

clicks = []

def click_event(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
       clicks.append((x,y))
       if len(clicks) > 3:
          print(str(clicks).strip('[]').strip('()').replace('), (', ','))
          clicks.clear()
       
       
       
if __name__=="__main__":
    img = cv2.imread("/home/heisenberg/Study/Database/New_Rsearch/Data/train_img_608.jpg")
    cv2.imshow('image', img)
    cv2.setMouseCallback('image', click_event)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
 
Last edited by a moderator:
Not sure if this is what you want
Python:
import tkinter as tk
import cv2
from PIL import Image, ImageTk

clicks = []
def button_pressed(event):
    clicks.append((event.x, event.y))
    if len(clicks) > 2:
        clicks.clear()
    print(clicks)

root = tk.Tk()
cvimg = cv2.imread('light/light_on.png')
imgtk = Image.fromarray(cvimg)
img = ImageTk.PhotoImage(image=imgtk)
img.image = img
label = tk.Label(root)
label['image'] = img
label['cursor'] = 'crosshair'
label.pack()
label.bind('<Button-1>', lambda event: button_pressed(event))
root.mainloop()
 

New Threads

Buy us a coffee!

Back
Top Bottom