def on_show_frame(self, event=None): # generate polar coordinates and avg diameter of circumferences data_circumferences = circumferences_to_polar_and_avg_diameter() # create plot figure = Figure(figsize=(5,5), dpi=100) ax = figure.add_subplot(111, projection="polar") # ax.set_title("Circumferences' polar coordinates") # plot both circumferences for (r, theta, _) in data_circumferences: ax.plot(theta, r) # Create a Tk canvas of the plot self.polar_plot = FigureCanvasTkAgg(figure, self) self.polar_plot.show() self.polar_plot.get_tk_widget().grid(row=1, column=1, sticky=NSEW, padx=20) # Show some controls for the figure self.toolbar_container = Frame(self) self.plot_toolbar = NavigationToolbar(self.polar_plot, self.toolbar_container) self.plot_toolbar.update() self.toolbar_container.grid(row=0, column=1, sticky=NSEW, padx=20, pady=20) # original image with both circumferences outlined self.image = get_slice_roi() self.responsive_image = ResponsiveImage(self, self.image) self.responsive_image.grid(row=1, column=0, sticky=NSEW, padx=20, pady=20)
def show_circumference(self, index): self.current_circumference_var.set(index) image = self.circumferences[index] if self.responsive_image is not None: self.responsive_image.destroy() self.responsive_image = ResponsiveImage(self, image) self.responsive_image.grid(row=1, column=0, rowspan=4, columnspan=3, sticky=NSEW, pady=20)
def load_image(self): # open a file chooser dialog and allow the user to select a source image temp_path = filedialog.askopenfilename( title="Select an image to process", filetypes=(("JPG", "*.jpg"), ("JPEG", "*.jpeg"), ("PNG", "*.png"), ("TIF", "*.tif"), ("TIFF", "*.tiff"), ("Windows bitmaps", "*.bmp"))) # ensure a file path was selected if len(temp_path) > 0: # disable button before processing self.begin_button.configure(state=DISABLED, cursor="wait") # Process image self.circumferences_found = process_image(temp_path) # update image path, message, and begin button self.image_path.set(temp_path) # image not found if self.circumferences_found is None: # show error message messagebox.showerror( "Could not process image", "The image may have been moved or renamed, or you may not have access to it." ) else: # Not a fresh session if self.visit_counter > 1: # reset the BSC GUI except this frame self.controller.reset_BSC_GUI(ignored=[type(self)]) # make this the 1st visit self.visit_counter = 1 # user image with all detected circumferences outlined image = get_config_image() # make it responsive self.responsive_image.destroy() self.responsive_image = ResponsiveImage(self, image) self.responsive_image.grid(row=0, column=0, rowspan=4, sticky=NSEW, pady=20)
def initialize_widgets(self): # Watchers # on image path change self.image_path = StringVar() self.image_path.trace("w", self.on_image_path_change) # responsive image container self.placeholder_image = Image.open("assets/placeholder_image.png") self.responsive_image = ResponsiveImage(self, self.placeholder_image) self.responsive_image.grid(row=0, column=0, rowspan=4) # choose image button self.choose_button = GreenButton(self, text="Choose an image", command=self.load_image) self.choose_button.grid(row=0, column=1, sticky=S) # selected image path self.path_entry = Entry(self, textvariable=self.image_path, state="readonly") self.path_entry.grid(row=1, column=1, sticky=EW, padx=20) # status message in row 2 self.message_var = StringVar() self.message = Label(self, textvariable=self.message_var, font=self.controller.header_font) # begin button self.begin_button = YellowButton(self, text="BEGIN", command=self.begin, image=self.controller.arrow_right, compound=RIGHT) self.begin_button.grid(row=3, column=1, sticky=SE, padx=20, pady=20) # Update widgets self.on_image_path_change() # visited flag self.visit_counter = 0 make_rows_responsive(self) make_columns_responsive(self)
def on_show_frame(self, event=None): final_circumferences = translate_coordinates() # create plot figure = Figure(figsize=(5, 5), dpi=100) ax = figure.add_subplot(111) # colors of plot series (outer = red, inner = blue) colors = ("r", "b") for ((contour_x, contour_y), (centroid_x, centroid_y), avg_diameter), color in zip(final_circumferences, colors): # plot the contours ax.plot(contour_x, contour_y, color=color) # plot the centroids ax.plot(centroid_x, centroid_y, color=color, marker="o") # Create a Tk canvas of the plot self.plot = FigureCanvasTkAgg(figure, self) self.plot.show() self.plot.get_tk_widget().grid(row=1, column=1, sticky=NSEW, padx=20) # Show some controls for the figure self.toolbar_container = Frame(self) self.plot_toolbar = NavigationToolbar2TkAgg(self.plot, self.toolbar_container) self.plot_toolbar.update() self.toolbar_container.grid(row=0, column=1, sticky=NSEW, padx=20, pady=20) # original image with both circumferences outlined self.image = get_slice_roi() self.responsive_image = ResponsiveImage(self, self.image, anchor=CENTER) self.responsive_image.grid(row=1, column=0, sticky=NSEW, padx=20, pady=20)
def update_image(self, *args): if self.box is not None: image = self.box[self.dimension_type.get()][0] if self.responsive_image is not None: self.responsive_image.destroy() self.responsive_image = ResponsiveImage(self, image) # stage 2 if self.selected_object_var.get(): self.responsive_image.grid(row=1, column=0, rowspan=8, columnspan=3, sticky=NSEW, pady=20) # stage 1 else: self.responsive_image.grid(row=1, column=0, rowspan=2, columnspan=3, sticky=NSEW, pady=20)
def set_placeholder_image(self): self.responsive_image.destroy() self.responsive_image = ResponsiveImage(self, self.placeholder_image) self.responsive_image.grid(row=0, column=0, rowspan=4)
def __init__(self): Tk.__init__(self) # START SHARED # Global fonts self.global_font_family = font.Font(family="Segoe UI Emoji") self.common_font = font.Font(family="Segoe UI Emoji", size=14) self.bold_font = font.Font(family="Segoe UI Emoji", size=13, weight="bold") self.header_font = font.Font(family="Segoe UI Emoji", size=16, weight="bold") self.title_font = font.Font(family="Segoe UI Emoji", size=28, weight="bold") self.important_font = font.Font(family="Segoe UI Emoji", size=28, weight="bold", underline=True) # update widget fonts self.option_add("*Font", self.global_font_family) self.option_add("*Button.Font", self.bold_font) self.option_add("*Label.Font", self.common_font) # Common images self.arrow_left = ImageTk.PhotoImage( Image.open("assets/arrow_left.png")) self.arrow_right = ImageTk.PhotoImage( Image.open("assets/arrow_right.png")) self.save_icon = ImageTk.PhotoImage(Image.open("assets/save.png")) # END SHARED # Main page layout: hosts page content and global widgets self.container = Frame(self) # Container fills the entire window self.container.pack(side=TOP, fill=BOTH, expand=True) # Navigation bar self.navbar = Frame(self.container, bg="#35AD35") self.navbar.grid(row=0, columnspan=2, sticky=NSEW) # Home button home_image = Image.open("assets/home.png") # resize home button image home_image = resize_keep_aspect(home_image, max_w=100, max_h=37) home_image = ImageTk.PhotoImage(home_image) self.home_button = Label(self.navbar, image=home_image, cursor="hand2", text="Home") self.home_button.image = home_image self.home_button.grid(row=0, column=0, sticky=NSEW) # bind to click event self.home_button.bind("<Button-1>", self.go_home) self.home_button.bind("<Enter>", self.on_enter_home_btn) self.home_button.bind("<Leave>", self.on_leave_home_btn) # Back button self.back_button = Button(self.navbar, image=self.arrow_left, cursor="hand2", text="Go back", compound=LEFT, command=self.go_back, bg="#35AD35", fg="#FFFFFF", relief=GROOVE, padx=10) self.back_button.grid(row=0, column=1, sticky=NSEW) # Page title self.title_var = StringVar() self.page_title = Label(self.navbar, textvariable=self.title_var, bg="#35AD35", fg="#FFFFFF", font=self.title_font) self.page_title.grid(row=0, column=2, sticky=W + E, padx=10) # Step indicator self.step = StringVar() self.page_step = Label(self.navbar, textvariable=self.step, bg="#35AD35", fg="#FFFFFF") self.page_step.grid(row=0, column=3, sticky=E, padx=10) # only the page title is responsive make_columns_responsive(self.navbar, ignored=[0, 1, 3]) # Bamboo decor on left side of screen bamboo_image = Image.open("assets/bamboo.png") self.bamboo = ResponsiveImage(self.container, bamboo_image, tag="bamboo", anchor=NW) self.bamboo.grid(row=1, column=0, sticky=NSEW) # The class names for both BSC and BPC self.bsc_pages = (ConfigBSC, PickCircumferencesBSC, RefObjectBSC, ResultsBSC) self.bpc_pages = (ConfigBPC, MeasureBPC, ResultsBPC) # Initialize all pages and keep their references accessible self.frames = {} for F in (() + (Home, ) + self.bsc_pages + self.bpc_pages): page_name = F.__name__ frame = F(parent=self.container, controller=self) self.frames[page_name] = frame # Add all frames to the container, on top of each other frame.grid(row=1, column=1, sticky=NSEW) # make the window responsive, except the navbar row make_rows_responsive(self.container, ignored=[0]) make_columns_responsive(self.container) # Start on the home page self.active_frame = None self.show_frame("Home")