dropdown menu

Back To Home

Thursday, November 14, 2019

python- get x,y location of an image

Get x,y location of an image is very useful. We can do colour detection on certain fixed point of a product in industry or many others applications. This tutorial is just to show how to get the x,y coordinate of an image.

 Python code:

from tkinter import *
from PIL import Image, ImageTk
import cv2

root = Tk()

labelFrame =Frame(root,width=10,height=10)
labelFrame.grid(row=0, column=0)

img=cv2.imread("md_5aba147bcacf2.png")
cv2.imwrite("md_5aba147bcacf2.png",img)
photo=PhotoImage(file="md_5aba147bcacf2.png")
label=Label(labelFrame,image=photo)
label.grid(row=1, column=1)


# Determine the origin by clicking
def getorigin(eventorigin):
    global x0,y0
    x0 = eventorigin.x
    y0 = eventorigin.y
    print(x0,y0)
 
#mouseclick event
label.bind("<Button 1>",getorigin)

root.mainloop()

                                                                 


No comments:

Post a Comment