python - index events on mouse click tkinter -


is there way index multiple clicks on tkinter canvas? trying write python script save 4 unique user-selected regions of image. each image has text overlaid on showing latitude, longitude, date , time. goal have user select 4 regions text , crop them create 4 new images.
script works far , has user open image, draw rectangles, , print coordinates of rectangles. when have coordinates saved variable, overwritten next rectangle.
how can index rectangle selections (or events) lat, lon, date , time can have unique variables can passed function crop parts of image?
here script (question in comments near end):

import tkinter tk tkinter import * import tkmessagebox pil import imagetk, image tkfiledialog import askopenfilename import os  class kanvas(frame):     def __init__ (self, master, **kwargs):         frame.__init__(self, master, **kwargs)          self.grid_rowconfigure(0, weight=1)         self.grid_columnconfigure(0, weight=1)          self.canv = canvas(self, width = 720, height = 480, bd=0, highlightthickness=0)         self.hscroll = scrollbar(self, orient='horizontal',                              command=self.canv.xview)         self.hscroll.grid(row=1, column=0, sticky='we')         self.vscroll = scrollbar(self, orient='vertical',                              command=self.canv.yview)         self.vscroll.grid(row=0, column=1, sticky='ns')         self.canv.grid(row=0, column=0, sticky='nsew', padx=4, pady=4)                 self.canv.configure(xscrollcommand=self.hscroll.set,                         yscrollcommand=self.vscroll.set)  class imgtk(tk.tk):     def __init__(self):     #this function asks user select image opens on tkinter window         tk.tk.__init__(self)         #above line calls super class's '__init__' method; resolved error of 'get attr'         button = tkmessagebox.showinfo(message = "draw rectangles around lat, lon, date , time")          self.main = kanvas(self)         self.main.grid(row=0, column = 0, sticky = 'nsew')         self.c = self.main.canv          self.currentimage = {}         self.load_imgfile(askopenfilename(parent = tk.toplevel(), initialdir = "d:/", title = "choose image:"))          self.c.bind('<buttonpress-1>', self.on_mouse_down)         self.c.bind('<b1-motion>', self.on_mouse_drag)         self.c.bind('<buttonrelease-1>', self.on_mouse_up)      def load_imgfile(self, filename):         self.img = image.open(filename)         self.currentimage['data'] = self.img          self.photo = imagetk.photoimage(self.img)         self.c.xview_moveto(0)         self.c.yview_moveto(0)         self.c.create_image(0, 0, image=self.photo, anchor='nw', tags='img')         self.c.config(scrollregion=self.c.bbox('all'))         self.currentimage['photo'] = self.photo       def on_mouse_down(self, event):                 self.anchor = (event.widget.canvasx(event.x),                    event.widget.canvasy(event.y))         self.item = none      def on_mouse_drag(self, event):                 bbox = self.anchor + (event.widget.canvasx(event.x),                           event.widget.canvasy(event.y))         if self.item none:             self.item = event.widget.create_rectangle(bbox, outline="red")         else:             event.widget.coords(self.item, *bbox)      def on_mouse_up(self, event):                 if self.item:             self.on_mouse_drag(event)              box = tuple((int(round(v)) v in event.widget.coords(self.item)))             roi = self.currentimage['data'].crop(box) # region of interest             values = roi.getdata() # pixel values             coords = [roi.size[0], roi.size[1], box[0], box[1]]             print coords          # main question: how save unique variables events?         # following able do:         # lat = coords event[0]         # lon = coords event[1]         # time = coords event[2]         # date = coords event[3]  app = imgtk() app.mainloop()   

here example image (rectangle selections in red).

i'm relatively new to python, please forgive newbie syntax mistakes. thank help!

edit figured out solution, in case else comes across this. create global list (crop_regions), append within last function.

import... cro_regions = [] class kanvas(...): ... class imgtk(...):     def __init__(...)     ...     def on_mouse_up(...)         if self.item:             self.on_mouse_drag(event)              box = tuple((int(round(v)) v in event.widget.coords(self.item)))             roi = self.currentimage['data'].crop(box) # region of interest             values = roi.getdata() # pixel values             coords = [roi.size[0], roi.size[1], box[0], box[1]]             lat =  roi.size[0],'x',roi.size[1],'+',box[0],'+',box[1]             lat = str(lat)             lat = lat.replace(',','')             lat = lat.replace("'", '')             lat = lat.replace(' ','')             global crop_regions             crop_regions.append(lat)  app = imgtk() app.mainloop() 


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -