@@ -37,6 +37,8 @@ def __init__(self):
37
37
self .frame_main_left = None
38
38
self .frame_main_center = None
39
39
self .frame_main_right = None
40
+ self .show_origin_btn = None
41
+ self .show_enhanced_btn = None
40
42
self .canvas : ResizingCanvas = None
41
43
self .enhance_pb = None
42
44
self .start_enhance_btn = None
@@ -83,9 +85,26 @@ class ThemedStyle:
83
85
self .model_dir = resource_path ("pretrained/" )
84
86
self .vignette_handler ()
85
87
86
- def enhance_listener (self , * args ):
87
- if not (self ._check_image () and self ._check_model ()):
88
+ def _show_origin_listener (self , * args ):
89
+ if not (self ._check_image ()): return
90
+ image = Image .fromarray (np .asarray (self ._main_image_origin ))
91
+ self .canvas .set_main_image (image )
92
+ self .show_origin_btn .config (state = "disabled" )
93
+ self .show_enhanced_btn .config (state = "normal" )
94
+
95
+ def _show_enhanced_listener (self , * args ):
96
+ if not (self ._check_image ()): return
97
+ if self ._main_image_enhanced is None :
98
+ pop_msg .showinfo ("Umm..." , "請先增強圖片" )
88
99
return
100
+ image = Image .fromarray (np .asarray (self ._main_image_enhanced ))
101
+ self .canvas .set_main_image (image )
102
+ self .canvas .request_update ()
103
+ self .show_enhanced_btn .config (state = "disabled" )
104
+ self .show_origin_btn .config (state = "normal" )
105
+
106
+ def enhance_listener (self , * args ):
107
+ if not (self ._check_image () and self ._check_model ()): return
89
108
thread = td .Thread (target = self ._enhance_task )
90
109
self .status_text .set ("增強圖片中.." )
91
110
self .enhance_pb .start ()
@@ -114,8 +133,7 @@ def _resize_height_listener(self, *args):
114
133
def _resize_width_listener (self , * args ):
115
134
if not self ._check_image (): return
116
135
with self .resize_lock :
117
- if self .resizing :
118
- return
136
+ if self .resizing : return
119
137
self .resizing = True
120
138
origin_height , origin_width , _ = np .shape (self ._main_image_origin )
121
139
resize_width = 0 if not self .resize_width .get () else int (self .resize_width .get ())
@@ -133,9 +151,11 @@ def _enhance_task(self):
133
151
134
152
resize_image = cv2 .resize (self ._main_image_origin , dsize = (new_width , new_height ))
135
153
resize_image = resize_image [new_height % 8 :, new_width % 8 :, :]
136
- self ._main_image_enhanced = self ._model .sample (resize_image , denoise = False )
137
- self ._main_image_current_clean = self ._main_image_enhanced
138
- print (self ._main_image_enhanced )
154
+ enhance_result = self ._model .sample (resize_image , denoise = False )
155
+ if enhance_result is not None :
156
+ self ._main_image_enhanced = enhance_result
157
+ self ._main_image_current_clean = self ._main_image_enhanced
158
+
139
159
140
160
def _enhance_handler (self , thread : td .Thread ):
141
161
if thread .is_alive ():
@@ -144,27 +164,42 @@ def _enhance_handler(self, thread: td.Thread):
144
164
self .enhance_pb .stop ()
145
165
self .start_enhance_btn .config (state = "normal" )
146
166
self .config (cursor = '' )
147
- image = Image .fromarray (np .asarray (self ._main_image_enhanced ))
148
- self .canvas .set_main_image (image )
149
- self .canvas .request_update ()
150
- self .status_text .set ("處理完成!" )
151
- try :
152
- subprocess .check_output (["notify-send" , "圖片處理完成!" , "<b>幻想濾鏡™</b>處理好圖片囉!" , "--icon=face-glasses" ])
153
- except FileNotFoundError :
154
- print ("can't send notification." )
155
- self .after (3000 , lambda : self .status_text .set ("就緒" ))
167
+ if self ._model .success :
168
+ self .show_enhanced_btn .config (state = "disabled" )
169
+ self .show_origin_btn .config (state = "normal" )
170
+ image = Image .fromarray (np .asarray (self ._main_image_enhanced ))
171
+ self .canvas .set_main_image (image )
172
+ self .canvas .request_update ()
173
+ self .status_text .set ("處理完成!" )
174
+ try :
175
+ subprocess .check_output (["notify-send" , "圖片處理完成!" , "<b>幻想濾鏡™</b>處理好圖片囉!" , "--icon=face-glasses" ])
176
+ except :
177
+ logging .warning ("can't send notification." )
178
+ self .after (3000 , lambda : self .status_text .set ("就緒" ))
179
+
180
+ else :
181
+ pop_msg .showerror ("Something went wrong.. " , "圖片處理失敗!\n 多數失敗是由於圖片太大張了,把圖片縮小點試試~" )
182
+ pop_msg .showerror ("Something went wrong.. " , self ._model .error_log )
183
+
184
+ self .status_text .set ("處理失敗!" )
185
+ try :
186
+ subprocess .check_output (["notify-send" , "圖片處理失敗!" , "<b>幻想濾鏡™</b>圖片處理失敗了QQ" , "--icon=face-sad" ])
187
+ except :
188
+ logging .warning ("can't send notification." )
189
+
190
+ self .after (3000 , lambda : self .status_text .set ("就緒" ))
156
191
157
192
def open_image_listener (self , * args ):
158
193
159
194
try :
195
+ # for Linux
160
196
filename = subprocess .check_output (['zenity' , '--file-selection' ]).decode ("utf-8" ).strip ()
161
197
except FileNotFoundError :
162
198
filename = filedialog .askopenfilename ()
163
199
except subprocess .CalledProcessError :
164
200
filename = False
165
201
166
202
if not filename :
167
- logging .info ("cancel opening image." )
168
203
return False
169
204
try :
170
205
logging .info ("open image:" , filename )
@@ -178,8 +213,6 @@ def open_image_listener(self, *args):
178
213
self .resize_height .set (image .height )
179
214
self .resize_width .set (image .width )
180
215
181
-
182
-
183
216
except IOError as e :
184
217
logging .error ("open image failed!" )
185
218
logging .error (str (e ))
@@ -266,9 +299,14 @@ def save(self, *args):
266
299
image .save (path )
267
300
268
301
def run (self ):
269
-
302
+ '''
303
+ try:
304
+ # self.call('wm', 'iconphoto', self._w, ImageTk.PhotoImage(Image.open(resource_path('appicon.png'))))
305
+ self.iconbitmap(resource_path('appicon.ico'))
306
+ except:
307
+ pass
308
+ '''
270
309
self .title ("Fantastic Filter" )
271
-
272
310
self .geometry ("%dx%d+50+40" % (800 , 500 ))
273
311
274
312
"""
@@ -307,6 +345,10 @@ def run(self):
307
345
model_cbb .current (0 ) # 選擇第一個
308
346
model_cbb .bind ("<<ComboboxSelected>>" , self .select_model_listener ) # 绑定事件,(下拉列表框被选中时)
309
347
348
+ self .show_origin_btn = ttk .Button (frame_toolbar , text = "檢視原圖" , command = self ._show_origin_listener )
349
+ self .show_enhanced_btn = ttk .Button (frame_toolbar , text = "檢視增強後" , command = self ._show_enhanced_listener )
350
+ self .show_origin_btn .pack (side = 'left' , padx = (10 , 0 ))
351
+ self .show_enhanced_btn .pack (side = 'left' , padx = (0 , 10 ))
310
352
''' main area '''
311
353
312
354
# split into 3 part, | load _model,image... | image preview | edit.. save.|
@@ -348,8 +390,8 @@ def run(self):
348
390
validatecommand = (self .register (isnumeric_or_blank ), "%P" ), width = 9 )
349
391
self .input_resize_width = ttk .Entry (frame_resize_inputs , textvariable = self .resize_width , validate = 'key' ,
350
392
validatecommand = (self .register (isnumeric_or_blank ), "%P" ), width = 9 )
351
- self .input_resize_width .pack (side = 'left' )
352
- self .input_resize_height .pack (side = 'right' )
393
+ self .input_resize_width .pack (side = 'left' , padx = ( 0 , 4 ) )
394
+ self .input_resize_height .pack (side = 'right' , padx = ( 4 , 0 ) )
353
395
354
396
ttk .Separator (self .frame_main_right , orient = 'horizontal' ).pack (fill = 'x' , pady = 10 )
355
397
@@ -509,7 +551,6 @@ def set_main_image(self, image: Image):
509
551
510
552
def resource_path (relative_path ):
511
553
if hasattr (sys , '_MEIPASS' ):
512
- print (sys ._MEIPASS )
513
554
return sys ._MEIPASS + '/' + relative_path
514
555
return os .path .join (os .path .dirname (os .path .abspath (__file__ )), relative_path )
515
556
0 commit comments