def undoCrop(self,event=None): """ Undoes crop and resets the raw image @type event: event @param event: Ctrl + Z event @rtype: None @return: None """ if event != None: self.x0 = None self.y0 = None self.x1 = None self.y1 = None self.draw_np = np.copy(self.org_np) self.img_im = tab_tools.np2im(self.draw_np) self.resized_im = tab_tools.resizeIm(self.img_im,self.org_width,self.org_height,self.t1c1i1_width,self.t1c1i1_height) self.img_tk = tab_tools.im2tk(self.resized_im) self.t1c1i1.configure(image=self.img_tk) self.crop_preview_im = self.img_im.copy() self.crop_preview_width,self.crop_preview_height = self.crop_preview_im.size self.crop_preview_resized_im = tab_tools.resizeIm(self.crop_preview_im,self.crop_preview_width,self.crop_preview_height,self.t1c1i1_width*self.crop_preview_img_ratio,self.t1c1i1_height*self.crop_preview_img_ratio) self.crop_preview_tk = tab_tools.im2tk(self.crop_preview_resized_im) self.t1c2i1.configure(image=self.crop_preview_tk)
def previousRaw(self,event): """ Requests and displays previous raw image @type event: event @param event: Left arrow event @rtype: None @return: None """ if not(self.loading): self.loading = True self.t1c2r2.configure(text="loading",foreground="red") # display that it's loading self.master.update() # update loading setting self.pingServer() if self.serverConnected: query = self.interface.getPrevRawImage() if query == None: self.noPreviousRaw() self.t1_functional = False else: self.t1_functional = True self.imageID = query[1] self.org_np = np.array(query[0]) #tab_tools.get_image('frame0744.jpg') timestamp = datetime.datetime.fromtimestamp(self.interface.getImageInfo(self.imageID).time_stamp) self.t1c2r1b.configure(text=timestamp.strftime('%H : %M : %S')) self.draw_np = np.copy(self.org_np) self.img_im = tab_tools.np2im(self.draw_np) self.crop_preview_im = self.img_im.copy() self.crop_preview_tk = tab_tools.im2tk(self.crop_preview_im) self.org_width,self.org_height = self.img_im.size self.crop_preview_width,self.crop_preview_height = self.img_im.size self.cropped = False self.resized_im = tab_tools.resizeIm(self.img_im,self.org_width,self.org_height,self.t1c1i1_width,self.t1c1i1_height) self.img_tk = tab_tools.im2tk(self.resized_im) self.t1c1i1.configure(image=self.img_tk) self.crop_preview_resized_im = tab_tools.resizeIm(self.crop_preview_im,self.crop_preview_width,self.crop_preview_height,self.t1c1i1_width*self.crop_preview_img_ratio,self.t1c1i1_height*self.crop_preview_img_ratio) self.crop_preview_tk = tab_tools.im2tk(self.crop_preview_resized_im) self.t1c2i1.configure(image=self.crop_preview_tk) self.t1c2r6b.configure(text="unsubmitted",foreground="red") # reset crop points to none self.x0 = None self.y0 = None self.x1 = None self.y1 = None # zooming variables #self.zoomPercent = 1.0 # (0.1,1.0) --> (10%,100%) of image shown #self.img_im_org = self.img_im.copy() # PIL raw image that won't be changed self.hourtime = datetime.datetime.now() self.t1c2r0b.configure(text="%d : %d : %d" % (self.hourtime.hour,self.hourtime.minute,self.hourtime.second)) self.t1c2r2.configure(text="loaded",foreground="green") # display done loading self.loading = False
def cropImage(self,x0,y0,x1,y1): """ Crops raw image @type x0: integer @param x0: pixel x location of first click @type y0: integer @param y0: pixel y location of first click @type x1: integer @param x1: pixel x location of second click @type y1: integer @param y1: pixel y location of second click @rtype: None @return: None """ if x0 < x1: self.cx0 = x0 self.cx1 = x1 else: self.cx0 = x1 self.cx1 = x0 if y0 < y1: self.cy0 = y0 self.cy1 = y1 else: self.cy0 = y1 self.cy1 = y0 self.crop_preview_im = self.crop_preview_im.crop((self.cx0,self.cy0,self.cx1,self.cy1)) self.crop_preview_width,self.crop_preview_height = self.crop_preview_im.size self.crop_preview_resized_im = tab_tools.resizeIm(self.crop_preview_im,self.crop_preview_width,self.crop_preview_height,self.t1c1i1_width*self.crop_preview_img_ratio,self.t1c1i1_height*self.crop_preview_img_ratio) self.crop_preview_tk = tab_tools.im2tk(self.crop_preview_resized_im) self.t1c2i1.configure(image=self.crop_preview_tk)
def resizeEventTab1(self,event=None): """ Resizes pictures on Tab1 @type event: event @param event: resize window event @rtype: None @return: None """ if self.initialized and (time.time()-self.resize_counter_tab1) > 0.050: if self.t1c1i1.winfo_width() > 1: self.resize_counter_tab1 = time.time() self.master.update() # main image self.t1c1i1_width = self.t1c1i1.winfo_width() #widget width self.t1c1i1_height = self.t1c1i1.winfo_height() # widget height self.resized_im = tab_tools.resizeIm(self.img_im,self.org_width,self.org_height,self.t1c1i1_width,self.t1c1i1_height) self.t1c1i1_img_width,self.t1c1i1_img_height = self.resized_im.size self.img_tk = tab_tools.im2tk(self.resized_im) self.t1c1i1.configure(image=self.img_tk) # cropped image self.crop_preview_resized_im = tab_tools.resizeIm(self.crop_preview_im,self.crop_preview_width,self.crop_preview_height,self.t1c1i1_width*self.crop_preview_img_ratio,self.t1c1i1_height*self.crop_preview_img_ratio) self.crop_preview_tk = tab_tools.im2tk(self.crop_preview_resized_im) self.t1c2i1.configure(image=self.crop_preview_tk)
def resizeEventTab0(self,event=None): """ Resizes picture on Tab0 @type event: event @param event: resize window event @rtype: None @return: None """ if self.initialized and (time.time()-self.resize_counter_tab0) > 0.050: if self.t0c2r0.winfo_width() > 1: self.resize_counter_tab0 = time.time() self.master.update() # get container label height and width t0c2i1_width = self.t0c2r0.winfo_width() t0c2i1_height = self.t0c2r0.winfo_height() logoW, logoH = self.logo_im.size self.logo_resized_im = tab_tools.resizeIm(self.logo_im, logoW, logoH, t0c2i1_width, t0c2i1_height) self.logo_tk = tab_tools.im2tk(self.logo_resized_im) self.t0c2r0.configure(image=self.logo_tk)
def resizeEventTab2(self, event=None): """ Resizes picture on Tab2 @type event: event @param event: resize window event @rtype: None @return: None """ if self.initialized and (time.time() - self.resize_counter_tab2) > 0.050: if self.t2c2i1.winfo_width() > 1: self.resize_counter_tab2 = time.time() self.master.update() self.t2c2i1_width = self.t2c2i1.winfo_width() self.t2c2i1_height = self.t2c2i1.winfo_height() self.cropped_resized_im = tab_tools.resizeIm( self.cropped_im, self.cropped_width, self.cropped_height, self.t2c2i1_width, self.t2c2i1_height) self.cropped_tk = tab_tools.im2tk(self.cropped_resized_im) self.t2c2i1.configure(image=self.cropped_tk)
def previousCropped(self, event): """ Requests and displays previous cropped image @type event: event @param event: Left arrow event @rtype: None @return: None """ if not (self.t2_entry_focus): focus = self.tab2.focus_get() self.serverConnected = self.interface.ping() if self.serverConnected: query = self.interface.getPrevCroppedImage() if query == None: self.t2_functional = False self.noPreviousCropped() else: self.t2_functional = True self.imageID = query[1] self.cropped_np = np.array(query[0]) yaw_angle = tab_tools.getYawAngle(self.interface, self.imageID) self.cropped_np = imutils.rotate_bound( self.cropped_np, yaw_angle) status = query[2] if status: self.t2c2lr48b.configure(text='submitted', foreground='green') else: self.t2c2lr48b.configure(text='unsubmitted', foreground='red') self.cropped_im = tab_tools.np2im(self.cropped_np) self.cropped_width, self.cropped_height = self.cropped_im.size self.cropped_resized_im = tab_tools.resizeIm( self.cropped_im, self.cropped_width, self.cropped_height, self.t2c2i1_width, self.t2c2i1_height) self.cropped_tk = tab_tools.im2tk(self.cropped_resized_im) self.t2c2i1.configure(image=self.cropped_tk)
def mouse_release(self,event): """ Saves pixel location of where the mouse clicks and creates crop preview @type event: event @param event: mouse event @rtype: None @return: None """ if self.cropped: self.undoCrop() self.t1c1i1.unbind("<Motion>") self.t1c1i1.unbind("<ButtonRelease-1>") disp_width,disp_height = self.resized_im.size # ratio between full-size image and displayed image self.sr = (self.org_width/float(disp_width) + self.org_height/float(disp_height))/2.0 self.draw_np = np.copy(self.org_np) # prevent going out of bounds x1 = event.x - self.offset_x y1 = event.y - self.offset_y if x1 > self.resized_im.size[0]: x1 = self.resized_im.size[0] elif x1 < 0: x1 = 0 if y1 > self.resized_im.size[1]: y1 = self.resized_im.size[1] elif y1 < 0: y1 = 0 if self.new_crop: self.x1 = x1 self.y1 = y1 else: self.pan_x1 = x1 self.pan_y1 = y1 xdif = int((self.pan_x1 - self.pan_x0)) ydif = int((self.pan_y1 - self.pan_y0)) self.x0_hat = self.x0 + xdif self.y0_hat = self.y0 + ydif self.x1_hat = self.x1 + xdif self.y1_hat = self.y1 + ydif # prevent panning out of bounds if self.x0_hat < self.x1_hat: if self.x0_hat < 0: self.x0_hat = 0 self.x1_hat = np.abs(self.x1-self.x0) elif self.x1_hat > self.resized_im.size[0]: self.x0_hat = self.resized_im.size[0]-np.abs(self.x1-self.x0) self.x1_hat = self.resized_im.size[0] else: if self.x1_hat < 0: self.x1_hat = 0 self.x0_hat = np.abs(self.x1-self.x0) elif self.x0_hat > self.resized_im.size[0]: self.x1_hat = self.resized_im.size[0]-np.abs(self.x1-self.x0) self.x0_hat = self.resized_im.size[0] if self.y0_hat < self.y1_hat: if self.y0_hat < 0: self.y0_hat = 0 self.y1_hat = np.abs(self.y1-self.y0) elif self.y1_hat > self.resized_im.size[1]: self.y0_hat = self.resized_im.size[1]-np.abs(self.y1-self.y0) self.y1_hat = self.resized_im.size[1] else: if self.y1_hat < 0: self.y1_hat = 0 self.y0_hat = np.abs(self.x1-self.x0) elif self.y0_hat > self.resized_im.size[1]: self.y1_hat = self.resized_im.size[1]-np.abs(self.y1-self.y0) self.y0_hat = self.resized_im.size[1] # save hat values as the new values self.x0 = self.x0_hat self.y0 = self.y0_hat self.x1 = self.x1_hat self.y1 = self.y1_hat # do nothing if it was a single click if self.x0 != self.x1 or self.y0 != self.y1: cv2.rectangle(self.draw_np,(int(self.sr*self.x0),int(self.sr*self.y0)),(int(self.sr*self.x1),int(self.sr*self.y1)),(255,0,0),2) self.cropImage(int(self.sr*self.x0),int(self.sr*self.y0),int(self.sr*self.x1),int(self.sr*self.y1)) self.img_im = tab_tools.np2im(self.draw_np) self.resized_im = tab_tools.resizeIm(self.img_im,self.org_width,self.org_height,self.t1c1i1_width,self.t1c1i1_height) self.img_tk = tab_tools.im2tk(self.resized_im) self.t1c1i1.configure(image=self.img_tk) # Crop Image self.cropped = True self.t1c2r6b.configure(text="unsubmitted",foreground="red")
def mouse_move(self,event): """ Gets pixel location of where the mouse is moving and show rectangle for crop preview @type event: event @param event: mouse event @rtype: None @return: None """ self.t1c1i1.bind("<ButtonRelease-1>",self.mouse_release) disp_width,disp_height = self.resized_im.size # ratio between full-size image and displayed image self.sr = (self.org_width/float(disp_width) + self.org_height/float(disp_height))/2.0 self.draw_np = np.copy(self.org_np) x1 = event.x - self.offset_x y1 = event.y - self.offset_y # prevent from going out of bounds if x1 > self.resized_im.size[0]: x1 = self.resized_im.size[0] elif x1 < 0: x1 = 0 if y1 > self.resized_im.size[1]: y1 = self.resized_im.size[1] elif y1 < 0: y1 = 0 if self.new_crop: self.x1 = x1 self.y1 = y1 cv2.rectangle(self.draw_np,(int(self.sr*self.x0),int(self.sr*self.y0)),(int(self.sr*self.x1),int(self.sr*self.y1)),(255,0,0),2) else: self.pan_x1 = x1 self.pan_y1 = y1 xdif = int((self.pan_x1 - self.pan_x0)) ydif = int((self.pan_y1 - self.pan_y0)) self.x0_hat = self.x0 + xdif self.y0_hat = self.y0 + ydif self.x1_hat = self.x1 + xdif self.y1_hat = self.y1 + ydif # prevent panning out of bounds if self.x0_hat < self.x1_hat: if self.x0_hat < 0: self.x0_hat = 0 self.x1_hat = np.abs(self.x1-self.x0) elif self.x1_hat > self.resized_im.size[0]: self.x0_hat = self.resized_im.size[0]-np.abs(self.x1-self.x0) self.x1_hat = self.resized_im.size[0] else: if self.x1_hat < 0: self.x1_hat = 0 self.x0_hat = np.abs(self.x1-self.x0) elif self.x0_hat > self.resized_im.size[0]: self.x1_hat = self.resized_im.size[0]-np.abs(self.x1-self.x0) self.x0_hat = self.resized_im.size[0] if self.y0_hat < self.y1_hat: if self.y0_hat < 0: self.y0_hat = 0 self.y1_hat = np.abs(self.y1-self.y0) elif self.y1_hat > self.resized_im.size[1]: self.y0_hat = self.resized_im.size[1]-np.abs(self.y1-self.y0) self.y1_hat = self.resized_im.size[1] else: if self.y1_hat < 0: self.y1_hat = 0 self.y0_hat = np.abs(self.x1-self.x0) elif self.y0_hat > self.resized_im.size[1]: self.y1_hat = self.resized_im.size[1]-np.abs(self.y1-self.y0) self.y0_hat = self.resized_im.size[1] cv2.rectangle(self.draw_np,(int(self.sr*self.x0_hat),int(self.sr*self.y0_hat)),(int(self.sr*self.x1_hat),int(self.sr*self.y1_hat)),(255,0,0),2) cv2.line(self.draw_np,(int(self.sr*self.pan_x0),int(self.sr*self.pan_y0)),(int(self.sr*self.pan_x1),int(self.sr*self.pan_y1)),(45,255,255),2) self.img_im = tab_tools.np2im(self.draw_np) self.resized_im = tab_tools.resizeIm(self.img_im,self.org_width,self.org_height,self.t1c1i1_width,self.t1c1i1_height) self.img_tk = tab_tools.im2tk(self.resized_im) self.t1c1i1.configure(image=self.img_tk)