99import os
1010from canvas import ImageOnCanvas
1111from draw_poly import DrawPoly
12+ from draw_rect import DrawRect
1213from PIL import Image ,ImageTk
1314from log_debug import logger ,debug
1415
@@ -32,51 +33,58 @@ def __init__(self):
3233 self .bottom_frame = Frame (self .root ,width = INIT_WIDTH - BUTTON_WIDTH )
3334
3435 self .load_image_directory_button = Button (self .top_frame1 ,text = 'Open Folder' ,command = self .load_directory ,width = int (BUTTON_WIDTH ), style = "Bold.TButton" )
35- self .load_image_directory_button .grid (row = 0 ,columnspan = 2 ,sticky = tk .W + tk .E )
36+ self .load_image_directory_button .grid (row = OPEN_FOLDER_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
3637
3738 self .prev_img_button = Button (self .top_frame1 ,text = '← Prev' ,command = self .previous_img ,state = tk .DISABLED ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
38- self .prev_img_button .grid (row = 1 , column = 0 ,sticky = tk .W + tk .E )
39+ self .prev_img_button .grid (row = PREV_ROW , column = 0 ,sticky = tk .W + tk .E )
3940
4041 self .next_img_button = Button (self .top_frame1 ,text = 'Next → ' ,command = self .next_img ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
41- self .next_img_button .grid (row = 1 ,column = 1 ,sticky = tk .W + tk .E )
42+ self .next_img_button .grid (row = NEXT_COL ,column = 1 ,sticky = tk .W + tk .E )
4243
4344 self .save_image_button = Button (self .top_frame1 , text = 'Save ' , command = self .saver ,width = int (BUTTON_WIDTH ), style = "Bold.TButton" )
44- self .save_image_button .grid (row = 2 , columnspan = 2 ,sticky = tk .W + tk .E )
45+ self .save_image_button .grid (row = SAVE_ROW , columnspan = 2 ,sticky = tk .W + tk .E )
4546
4647 self .delete_poly_button = Button (self .top_frame ,text = 'Delete Selected' ,command = self .delete_selected ,width = int (BUTTON_WIDTH ), style = "Bold.TButton" )
47- self .delete_poly_button .grid (row = 0 ,columnspan = 2 ,sticky = tk .W + tk .E )
48+ self .delete_poly_button .grid (row = DEL_SELECTED_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
4849
4950 self .type_choices = TYPE_CHOICES
5051 self .variable = StringVar (self .top_frame )
5152 self .variable .set (self .type_choices [0 ])
5253 self .type_options = OptionMenu (self .top_frame ,self .variable ,* self .type_choices ,style = "Bold.TButton" )
5354 self .type_options .config (width = int (BUTTON_WIDTH / 2 ))
54- self .type_options .grid (row = 1 ,column = 0 )
55+ self .type_options .grid (row = DROP_DOWN_ROW ,column = 0 )
5556
5657 self .save_type_button = Button (self .top_frame , text = 'Save Type' , command = self .save_type ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
57- self .save_type_button .grid (row = 1 , column = 1 ,sticky = tk .W + tk .E )
58+ self .save_type_button .grid (row = SAVE_TYPE_ROW , column = 1 ,sticky = tk .W + tk .E )
5859
5960 self .deselect_all_button = Button (self .top_frame ,text = 'Deselect All' ,command = self .deselect_all ,width = BUTTON_WIDTH , style = "Bold.TButton" )
60- self .deselect_all_button .grid (row = 2 ,columnspan = 2 ,sticky = tk .W + tk .E )
61+ self .deselect_all_button .grid (row = DESELECT_ALL_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
6162
6263 self .select_all_button = Button (self .top_frame ,text = 'Select All' , command = self .select_all , width = BUTTON_WIDTH , style = "Bold.TButton" )
63- self .select_all_button .grid (row = 3 ,columnspan = 2 ,sticky = tk .W + tk .E )
64+ self .select_all_button .grid (row = SELECT_ALL_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
6465
6566 self .draw_poly_button = Button (self .top_frame ,text = 'Draw Poly' ,command = self .draw_poly_func ,width = BUTTON_WIDTH , style = "Bold.TButton" )
66- self .draw_poly_button .grid (row = 4 ,columnspan = 2 ,sticky = tk .W + tk .E )
67+ self .draw_poly_button .grid (row = DRAW_POLY_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
68+
69+ self .draw_rect_button = Button (self .top_frame ,text = 'Draw Rectangle' ,command = self .draw_rect_func ,width = BUTTON_WIDTH , style = "Bold.TButton" )
70+ self .draw_rect_button .grid (row = DRAW_RECT_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
6771
6872 self .delete_all_button = Button (self .top_frame ,text = 'Delete All' ,command = self .delete_all ,width = BUTTON_WIDTH , style = "Bold.TButton" )
69- self .delete_all_button .grid (row = 5 ,columnspan = 2 ,sticky = tk .W + tk .E )
73+ self .delete_all_button .grid (row = DELETE_ALL_ROW ,columnspan = 2 ,sticky = tk .W + tk .E )
7074
7175 self .save_poly_button = Button (self .top_frame ,text = 'Save Poly' ,command = self .save_drawing ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
7276
7377 self .discard_poly_button = Button (self .top_frame ,text = 'Discard Poly' ,command = self .discard_drawing ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
7478
79+ self .save_rect_button = Button (self .top_frame ,text = 'Save Rect' ,command = self .save_drawing ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
80+
81+ self .discard_rect_button = Button (self .top_frame ,text = 'Discard Rect' ,command = self .discard_drawing ,width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
82+
7583 self .show_type_button = Button (self .top_frame , text = 'Show Type' , command = self .show_type , width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
76- self .show_type_button .grid (row = 6 , column = 0 , columnspan = 1 , sticky = tk .W + tk .E )
84+ self .show_type_button .grid (row = SHOW_TYPE_ROW , column = 0 , columnspan = 1 , sticky = tk .W + tk .E )
7785
7886 self .hide_type_button = Button (self .top_frame , text = 'Hide Type' , command = self .hide_type , width = int (BUTTON_WIDTH / 2 ), style = "Bold.TButton" )
79- self .hide_type_button .grid (row = 6 , columnspan = 1 , column = 1 , sticky = tk .W + tk .E )
87+ self .hide_type_button .grid (row = HIDE_TYPE_ROW , columnspan = 1 , column = 1 , sticky = tk .W + tk .E )
8088
8189 self .canvas = Canvas (self .bottom_frame ,width = INIT_WIDTH - BUTTON_WIDTH , height = INIT_HEIGHT , borderwidth = 1 )
8290 self .image_name = None
@@ -126,6 +134,8 @@ def show_buttons(self):
126134 self .delete_all_button .config (state = "normal" )
127135 self .show_type_button .config (state = "normal" )
128136 self .hide_type_button .config (state = "normal" )
137+ self .draw_poly_button .config (state = "normal" )
138+ self .draw_rect_button .config (state = "normal" )
129139
130140 def select_all (self ):
131141 for poly in self .img_cnv .polygons :
@@ -167,12 +177,12 @@ def load_new_img(self):
167177 self .img_cnv = None
168178 path = os .path .join (self .image_dir , self .image_name )
169179 self .img_cnv = ImageOnCanvas (self .root ,self .canvas ,path )
170- logger ("LOADING : " + self .img_cnv .image_path )
180+ logger ("LOADED : " + self .img_cnv .image_path )
171181
172182 def load_directory (self ):
173183 while True :
174184 selection = filedialog .askdirectory ()
175- if selection == ():
185+ if selection == () or selection == '' :
176186 return
177187 self .root .directory = selection
178188 self .image_dir = self .root .directory
@@ -246,11 +256,22 @@ def draw_poly_func(self):
246256 self .deselect_all ()
247257 self .img_cnv .drawing_polygon = True
248258 self .draw_poly_button .grid_forget ()
249- self .save_poly_button .grid (row = 4 , column = 0 ,sticky = tk .W + tk .E )
250- self .discard_poly_button .grid (row = 4 , column = 1 ,sticky = tk .W + tk .E )
259+ self .save_poly_button .grid (row = DRAW_POLY_ROW , column = 0 ,sticky = tk .W + tk .E )
260+ self .discard_poly_button .grid (row = DRAW_POLY_ROW , column = 1 ,sticky = tk .W + tk .E )
251261 self .hide_buttons ()
262+ self .draw_rect_button .config (state = tk .DISABLED )
252263 self .drawing_obj = DrawPoly (self .bottom_frame ,self .canvas ,self .img_cnv ,RADIUS )
253264
265+ def draw_rect_func (self ):
266+ self .deselect_all ()
267+ self .img_cnv .drawing_polygon = True
268+ self .draw_rect_button .grid_forget ()
269+ self .save_rect_button .grid (row = DRAW_RECT_ROW , column = 0 ,sticky = tk .W + tk .E )
270+ self .discard_rect_button .grid (row = DRAW_RECT_ROW , column = 1 ,sticky = tk .W + tk .E )
271+ self .hide_buttons ()
272+ self .draw_poly_button .config (state = tk .DISABLED )
273+ self .drawing_obj = DrawRect (self .bottom_frame ,self .canvas ,self .img_cnv ,RADIUS )
274+
254275 def save_drawing (self ):
255276 self .show_buttons ()
256277 self .img_cnv .drawing_polygon = False
@@ -265,21 +286,33 @@ def save_drawing(self):
265286 self .img_cnv .add_poly (new_poly_pts )
266287 #self.img_cnv.bbs.append(new_poly_pts)
267288 #self.img_cnv.draw_bbs([self.img_cnv.bbs[-1]])
268-
289+ #debug (1, str(type(self.drawing_obj)))
290+ if isinstance (self .drawing_obj ,DrawRect ):
291+ self .save_rect_button .grid_forget ()
292+ self .discard_rect_button .grid_forget ()
293+ self .draw_rect_button .grid (row = DRAW_RECT_ROW , columnspan = 2 , sticky = tk .W + tk .E )
294+ elif isinstance (self .drawing_obj ,DrawPoly ):
295+ self .save_poly_button .grid_forget ()
296+ self .discard_poly_button .grid_forget ()
297+ self .draw_poly_button .grid (row = DRAW_POLY_ROW , columnspan = 2 , sticky = tk .W + tk .E )
298+ self .drawing_obj .delete_self ()
269299 self .drawing_obj = None
270- self .save_poly_button .grid_forget ()
271- self .discard_poly_button .grid_forget ()
272- self .draw_poly_button .grid (row = 4 ,columnspan = 2 ,sticky = tk .W + tk .E )
273300
274301 def discard_drawing (self ):
275302 self .show_buttons ()
276303 self .img_cnv .drawing_polygon = False
277- for pt in self .drawing_obj .points :
278- self .canvas .delete (pt )
304+ #for pt in self.drawing_obj.points:
305+ # self.canvas.delete(pt)
306+ self .drawing_obj .delete_self ()
307+ if isinstance (self .drawing_obj ,DrawRect ):
308+ self .save_rect_button .grid_forget ()
309+ self .discard_rect_button .grid_forget ()
310+ self .draw_rect_button .grid (row = DRAW_RECT_ROW , columnspan = 2 , sticky = tk .W + tk .E )
311+ elif isinstance (self .drawing_obj ,DrawPoly ):
312+ self .save_poly_button .grid_forget ()
313+ self .discard_poly_button .grid_forget ()
314+ self .draw_poly_button .grid (row = DRAW_POLY_ROW , columnspan = 2 , sticky = tk .W + tk .E )
279315 self .drawing_obj = None
280- self .save_poly_button .grid_forget ()
281- self .discard_poly_button .grid_forget ()
282- self .draw_poly_button .grid (row = 4 ,columnspan = 2 , sticky = tk .W + tk .E )
283316
284317
285318gui = GUI ()
0 commit comments