-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmain.py
executable file
·1267 lines (1123 loc) · 53.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from tkinter.messagebox import NO
import wx
from main_gui import MulimgViewerGui
import numpy as np
from about import About
from utils_img import ImgManager
from index_table import IndexTable
from pathlib import Path
import copy
from utils import get_resource_path, MyTestEvent
import platform
import requests
import threading
class MulimgViewer (MulimgViewerGui):
def __init__(self, parent, UpdateUI, get_type):
super().__init__(parent)
self.create_ImgManager()
self.UpdateUI = UpdateUI
self.get_type = get_type
acceltbl = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_UP,
self.menu_up.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_DOWN,
self.menu_down.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_RIGHT,
self.menu_right.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_LEFT,
self.menu_left.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_DELETE,
self.menu_delete_box.GetId())
])
self.SetAcceleratorTable(acceltbl)
# self.img_Sizer = self.scrolledWindow_img.GetSizer()
self.Bind(wx.EVT_CLOSE, self.close)
# self.Bind(wx.EVT_PAINT, self.OnPaint)
# parameter
self.out_path_str = ""
self.img_name = []
self.position = [0, 0]
self.Uint = self.scrolledWindow_img.GetScrollPixelsPerUnit()
self.Status_number = self.m_statusBar1.GetFieldsCount()
self.img_size = [-1, -1]
self.width = 1000
self.height = 600
self.start_flag = 0
self.x = -1
self.x_0 = -1
self.y = -1
self.y_0 = -1
self.color_list = []
self.box_id = -1
self.xy_magnifier = []
self.show_scale_proportion = 0
self.key_status = {"shift": 0, "ctrl": 0, "alt": 0}
self.indextablegui = None
self.aboutgui = None
self.icon = wx.Icon(get_resource_path(
'mulimgviewer.ico'), wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.m_statusBar1.SetStatusWidths([-2, -1, -4, -4])
self.set_title_font()
self.hidden_flag = 0
# Different platforms may need to adjust the width of the scrolledWindow_set
sys_platform = platform.system()
if sys_platform.find("Windows") >= 0:
self.width_setting = 300
elif sys_platform.find("Linux") >= 0:
self.width_setting = 280
elif sys_platform.find("Darwin") >= 0:
self.width_setting = 350
else:
self.width_setting = 300
self.SashPosition = self.width-self.width_setting
self.m_splitter1.SetSashPosition(self.SashPosition)
self.split_changing = False
self.width_setting_ = self.width_setting
# Draw color to box
self.colourPicker_draw.Bind(
wx.EVT_COLOURPICKER_CHANGED, self.draw_color_change)
# Check the software version
self.myEVT_MY_TEST = wx.NewEventType()
EVT_MY_TEST = wx.PyEventBinder(self.myEVT_MY_TEST, 1)
self.Bind(EVT_MY_TEST, self.myEVT_MY_TEST_OnHandle)
self.version = "v3.9.7"
self.check_version()
def myEVT_MY_TEST_OnHandle(self, event):
self.about_gui(None, update=True, new_version=event.GetEventArgs())
def check_version(self):
t1 = threading.Thread(target=self.run, args=())
t1.setDaemon(True)
t1.start()
def run(self):
url = "https://api.github.com/repos/nachifur/MulimgViewer/releases/latest"
try:
resp = requests.get(url)
resp.encoding = 'UTF-8'
if resp.status_code == 200:
output = resp.json()
if output["tag_name"] == self.version:
# print("No need to update!")
pass
else:
# print(output["tag_name"])
# print("Need to update!")
evt = MyTestEvent(self.myEVT_MY_TEST)
evt.SetEventArgs(output["tag_name"])
wx.PostEvent(self, evt)
except:
pass
def set_title_font(self):
font_path = Path("font")/"using"
font_path = Path(get_resource_path(str(font_path)))
files_name = [f.stem for f in font_path.iterdir()]
files_name = np.sort(np.array(files_name)).tolist()
for file_name in files_name:
file_name = file_name.split("_", 1)[1]
file_name = file_name.replace("-", " ")
self.title_font.Append(file_name)
self.title_font.SetSelection(0)
font_paths = [str(f) for f in font_path.iterdir()]
self.font_paths = np.sort(np.array(font_paths)).tolist()
def frame_resize(self, event):
self.auto_layout(frame_resize=True)
def open_all_img(self, event):
input_mode = self.choice_input_mode.GetSelection()
if input_mode == 0:
self.one_dir_mul_img(event)
elif input_mode == 1:
self.one_dir_mul_dir_auto(event)
elif input_mode == 2:
self.one_dir_mul_dir_manual(event)
elif input_mode == 3:
self.onefilelist(event)
def close(self, event):
if self.get_type() == -1:
self.Destroy()
else:
self.UpdateUI(-1)
def next_img(self, event):
if self.ImgManager.img_num != 0:
self.show_img_init()
self.ImgManager.add()
self.show_img()
else:
self.SetStatusText_(
["-1", "", "***Error: First, need to select the input dir***", "-1"])
self.SetStatusText_(["Next", "-1", "-1", "-1"])
def last_img(self, event):
if self.ImgManager.img_num != 0:
self.show_img_init()
self.ImgManager.subtract()
self.show_img()
else:
self.SetStatusText_(
["-1", "", "***Error: First, need to select the input dir***", "-1"])
self.SetStatusText_(["Last", "-1", "-1", "-1"])
def skip_to_n_img(self, event):
if self.ImgManager.img_num != 0:
self.show_img_init()
self.ImgManager.set_action_count(self.slider_img.GetValue())
self.show_img()
else:
self.SetStatusText_(
["-1", "", "***Error: First, need to select the input dir***", "-1"])
self.SetStatusText_(["Skip", "-1", "-1", "-1"])
def slider_value_change(self, event):
try:
value = int(self.slider_value.GetValue())
except:
self.slider_value.SetValue(str(self.ImgManager.action_count))
else:
if self.ImgManager.img_num != 0:
self.show_img_init()
self.ImgManager.set_action_count(value)
self.show_img()
self
else:
self.SetStatusText_(
["-1", "", "***Error: First, need to select the input dir***", "-1"])
self.SetStatusText_(["Skip", "-1", "-1", "-1"])
def save_img(self, event):
layout_params = self.set_img_layout()
if layout_params != False:
self.ImgManager.layout_params = layout_params
type_ = self.choice_output.GetSelection()
if self.auto_save_all.Value:
last_count_img = self.ImgManager.action_count
self.ImgManager.set_action_count(0)
if Path(self.out_path_str).is_dir():
continue_ = True
else:
continue_ = False
if continue_:
for i in range(self.ImgManager.max_action_num):
self.SetStatusText_(
["-1", "-1", "***"+str(self.ImgManager.name_list[self.ImgManager.action_count])+", saving img***", "-1"])
self.ImgManager.get_flist()
self.ImgManager.save_img(self.out_path_str, type_)
self.ImgManager.add()
self.ImgManager.set_action_count(last_count_img)
self.SetStatusText_(
["-1", "-1", "***Finish***", "-1"])
else:
self.SetStatusText_(
["-1", "-1", "***Error: First, need to select the output dir***", "-1"])
else:
try:
self.SetStatusText_(
["-1", "-1", "***"+str(self.ImgManager.name_list[self.ImgManager.action_count])+", saving img...***", "-1"])
except:
pass
flag = self.ImgManager.save_img(self.out_path_str, type_)
self.refresh(event)
if flag == 0:
self.SetStatusText_(
["Save", str(self.ImgManager.action_count), "Save success!", "-1"])
elif flag == 1:
self.SetStatusText_(
["-1", "-1", "***First, you need to select the output dir***", "-1"])
self.out_path(event)
self.SetStatusText_(
["-1", "-1", "", "-1"])
elif flag == 2:
self.SetStatusText_(
["-1", str(self.ImgManager.action_count), "***Error: "+str(self.ImgManager.name_list[self.ImgManager.action_count]) + ", during stitching images***", "-1"])
elif flag == 3:
self.SetStatusText_(
["-1", str(self.ImgManager.action_count), "***Error: "+str(self.ImgManager.name_list[self.ImgManager.action_count]) + ", the number of img in sub folders is different***", "-1"])
elif flag == 4:
self.SetStatusText_(
["-1", str(self.ImgManager.action_count), "***Error: No magnification box, the magnified image can not be saved***", "-1"])
self.SetStatusText_(["Save", "-1", "-1", "-1"])
def refresh(self, event):
if self.ImgManager.img_num != 0:
self.show_img_init()
self.show_img()
else:
self.SetStatusText_(
["-1", "", "***Error: First, need to select the input dir***", "-1"])
self.SetStatusText_(["Refresh", "-1", "-1", "-1"])
def one_dir_mul_dir_auto(self, event):
self.SetStatusText_(["Input", "", "", "-1"])
dlg = wx.DirDialog(None, "Parallel auto choose input dir", "",
wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.ImgManager.init(
dlg.GetPath(), 0, self.parallel_to_sequential.Value)
self.show_img_init()
self.ImgManager.set_action_count(0)
self.show_img()
self.choice_input_mode.SetSelection(1)
self.SetStatusText_(["Input", "-1", "-1", "-1"])
def one_dir_mul_dir_manual(self, event):
self.SetStatusText_(["Input", "", "", "-1"])
try:
if self.ImgManager.type == 1:
input_path = self.ImgManager.input_path
else:
input_path = None
except:
input_path = None
self.UpdateUI(1, input_path, self.parallel_to_sequential.Value)
self.choice_input_mode.SetSelection(2)
self.SetStatusText_(["Input", "-1", "-1", "-1"])
def one_dir_mul_img(self, event):
self.SetStatusText_(
["Sequential choose input dir", "", "", "-1"])
dlg = wx.DirDialog(None, "Choose input dir", "",
wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.ImgManager.init(dlg.GetPath(), 2)
self.show_img_init()
self.ImgManager.set_action_count(0)
self.show_img()
self.choice_input_mode.SetSelection(0)
self.SetStatusText_(
["Sequential choose input dir", "-1", "-1", "-1"])
def onefilelist(self, event):
self.SetStatusText_(["Choose the File List", "", "", "-1"])
wildcard = "List file (*.txt; *.csv)|*.txt;*.csv|" \
"All files (*.*)|*.*"
dlg = wx.FileDialog(None, "choose the Images List", "", "",
wildcard, wx.FD_DEFAULT_STYLE | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.ImgManager.init(dlg.GetPath(), 3)
self.show_img_init()
self.ImgManager.set_action_count(0)
self.show_img()
self.choice_input_mode.SetSelection(3)
self.SetStatusText_(["Choose the File List", "-1", "-1", "-1"])
def input_flist_parallel_manual(self, event):
wildcard = "List file (*.txt;)|*.txt;|" \
"All files (*.*)|*.*"
dlg = wx.FileDialog(None, "choose the Images List", "", "",
wildcard, wx.FD_DEFAULT_STYLE | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
with open(dlg.GetPath(), "r") as f:
input_path = f.read().split('\n')
self.ImgManager.init(
input_path[0:-1], 1, self.parallel_to_sequential.Value)
self.show_img_init()
self.ImgManager.set_action_count(0)
self.show_img()
self.choice_input_mode.SetSelection(2)
def save_flist_parallel_manual(self, event):
if self.out_path_str == "":
self.SetStatusText_(
["-1", "-1", "***Error: First, need to select the output dir***", "-1"])
else:
try:
np.savetxt(Path(self.out_path_str)/"input_flist_parallel_manual.txt",
self.ImgManager.input_path, fmt='%s')
except:
self.SetStatusText_(
["-1", "-1", "***Error: First, need to select parallel manual***", "-1"])
else:
self.SetStatusText_(
["-1", "-1", "Save" + str(Path(self.out_path_str)/"input_flist_parallel_manual.txt")+" success!", "-1"])
def out_path(self, event):
if len(self.img_name) != 0:
self.SetStatusText_(
["Choose out dir", str(self.ImgManager.action_count), self.img_name[self.ImgManager.action_count], "-1"])
else:
self.SetStatusText_(["Choose out dir", "-1", "-1", "-1"])
dlg = wx.DirDialog(None, "Choose out dir", "",
wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.out_path_str = dlg.GetPath()
self.m_statusBar1.SetStatusText(self.out_path_str, 3)
def colour_change(self, event):
c = self.colourPicker_gap.GetColour()
self.ImgManager.gap_color = (
c.red, c.green, c.blue, self.ImgManager.gap_alpha)
def background_alpha(self, event):
c = self.colourPicker_gap.GetColour()
self.ImgManager.gap_alpha = self.background_slider.GetValue()
self.ImgManager.gap_color = (
c.red, c.green, c.blue, self.ImgManager.gap_alpha)
def foreground_alpha(self, event):
self.ImgManager.img_alpha = self.foreground_slider.GetValue()
def delete_box(self, event):
if self.select_img_box.Value:
if self.box_id != -1:
self.xy_magnifier.pop(self.box_id)
self.refresh(event)
self.SetStatusText_(
["delete "+str(self.box_id)+"-th box", "-1", "-1", "-1"])
else:
self.xy_magnifier = []
self.refresh(event)
self.SetStatusText_(["delete all box", "-1", "-1", "-1"])
def up_img(self, event):
speed = self.get_speed(name="pixel")
if self.select_img_box.Value:
if self.box_id != -1:
box_point = self.xy_magnifier[self.box_id][0:4]
show_scale = self.xy_magnifier[self.box_id][4:6]
x, y = self.get_center_box(box_point)
x = x+0
y = y-speed
self.xy_magnifier[self.box_id][0:4] = self.move_box_point(
x, y, show_scale)
self.refresh(event)
else:
size = self.scrolledWindow_img.GetSize()
self.position[0] = int(
self.scrolledWindow_img.GetScrollPos(wx.HORIZONTAL)/self.Uint[0])
self.position[1] = int(
self.scrolledWindow_img.GetScrollPos(wx.VERTICAL)/self.Uint[1])
if self.position[1] > 0:
self.position[1] -= speed
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], self.position[1]*self.Uint[1])
self.SetStatusText_(["Up", "-1", "-1", "-1"])
def down_img(self, event):
speed = self.get_speed(name="pixel")
if self.select_img_box.Value:
if self.box_id != -1:
box_point = self.xy_magnifier[self.box_id][0:4]
show_scale = self.xy_magnifier[self.box_id][4:6]
x, y = self.get_center_box(box_point)
x = x+0
y = y+speed
self.xy_magnifier[self.box_id][0:4] = self.move_box_point(
x, y, show_scale)
self.refresh(event)
else:
size = self.scrolledWindow_img.GetSize()
self.position[0] = int(
self.scrolledWindow_img.GetScrollPos(wx.HORIZONTAL)/self.Uint[0])
self.position[1] = int(
self.scrolledWindow_img.GetScrollPos(wx.VERTICAL)/self.Uint[1])
if (self.position[1]-1)*self.Uint[1] < size[1]:
self.position[1] += speed
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], self.position[1]*self.Uint[1])
else:
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], size[1])
self.SetStatusText_(["Down", "-1", "-1", "-1"])
def right_img(self, event):
speed = self.get_speed(name="pixel")
if self.select_img_box.Value:
if self.box_id != -1:
box_point = self.xy_magnifier[self.box_id][0:4]
show_scale = self.xy_magnifier[self.box_id][4:6]
x, y = self.get_center_box(box_point)
x = x+speed
y = y+0
self.xy_magnifier[self.box_id][0:4] = self.move_box_point(
x, y, show_scale)
self.refresh(event)
else:
size = self.scrolledWindow_img.GetSize()
self.position[0] = int(
self.scrolledWindow_img.GetScrollPos(wx.HORIZONTAL)/self.Uint[0])
self.position[1] = int(
self.scrolledWindow_img.GetScrollPos(wx.VERTICAL)/self.Uint[1])
if (self.position[0]-1)*self.Uint[0] < size[0]:
self.position[0] += speed
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], self.position[1]*self.Uint[1])
else:
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], size[0])
self.SetStatusText_(["Right", "-1", "-1", "-1"])
def left_img(self, event):
speed = self.get_speed(name="pixel")
if self.select_img_box.Value:
if self.box_id != -1:
box_point = self.xy_magnifier[self.box_id][0:4]
show_scale = self.xy_magnifier[self.box_id][4:6]
x, y = self.get_center_box(box_point)
x = x-speed
y = y+0
self.xy_magnifier[self.box_id][0:4] = self.move_box_point(
x, y, show_scale)
self.refresh(event)
else:
size = self.scrolledWindow_img.GetSize()
self.position[0] = int(
self.scrolledWindow_img.GetScrollPos(wx.HORIZONTAL)/self.Uint[0])
self.position[1] = int(
self.scrolledWindow_img.GetScrollPos(wx.VERTICAL)/self.Uint[1])
if self.position[0] > 0:
self.position[0] -= speed
self.scrolledWindow_img.Scroll(
self.position[0]*self.Uint[0], self.position[1]*self.Uint[1])
self.SetStatusText_(["Left", "-1", "-1", "-1"])
def SetStatusText_(self, texts):
for i in range(self.Status_number):
if texts[i] != '-1':
self.m_statusBar1.SetStatusText(texts[i], i)
def img_left_click(self, event):
if self.magnifier.Value:
x_0, y_0 = event.GetPosition()
self.x_0 = x_0
self.y_0 = y_0
self.x = x_0
self.y = y_0
if self.select_img_box.Value:
# select box
x, y = event.GetPosition()
id = self.get_img_id_from_point([x, y])
xy_grid = self.ImgManager.xy_grid[id]
x = x-xy_grid[0]
y = y-xy_grid[1]
x_y_array = []
for i in range(len(self.ImgManager.crop_points)):
x_y_array.append(self.get_center_box(
self.ImgManager.crop_points[i][0:4]))
x_y_array = np.array(x_y_array)
dist = (x_y_array[:, 0]-x)**2+(x_y_array[:, 1]-y)**2
self.box_id = np.array(dist).argmin()
str_ = str(self.box_id)
self.SetStatusText_(["Select "+str_+"-th box", "-1", "-1", "-1"])
self.start_flag = 0
else:
# magnifier
if self.magnifier.Value:
self.start_flag = 1
else:
self.start_flag = 0
if self.magnifier.Value:
self.SetStatusText_(["Magnifier", "-1", "-1", "-1"])
# rotation
if self.rotation.Value:
x, y = event.GetPosition()
self.ImgManager.rotate(
self.get_img_id_from_point([x, y], img=True))
self.refresh(event)
self.SetStatusText_(["Rotate", "-1", "-1", "-1"])
# flip
if self.flip.Value:
x, y = event.GetPosition()
self.ImgManager.flip(self.get_img_id_from_point(
[x, y], img=True), FLIP_TOP_BOTTOM=self.checkBox_orientation.Value)
self.refresh(event)
self.SetStatusText_(["Flip", "-1", "-1", "-1"])
# focus img
if self.indextablegui or self.aboutgui:
pass
else:
self.img_panel.Children[0].SetFocus()
# show dir_id
x, y = event.GetPosition()
id = self.get_img_id_from_point([x, y])
second_txt = self.m_statusBar1.GetStatusText(1)
second_txt = second_txt.split("/")[0]
self.m_statusBar1.SetStatusText(second_txt+"/"+str(id)+"-th dir", 1)
def img_left_dclick(self, event):
if self.select_img_box.Value:
pass
else:
self.start_flag = 0
self.xy_magnifier = []
self.color_list = []
def img_left_move(self, event):
# https://stackoverflow.com/questions/57342753/how-to-select-a-rectangle-of-the-screen-to-capture-by-dragging-mouse-on-transpar
if self.magnifier.Value != False and self.start_flag == 1:
x, y = event.GetPosition()
id = self.get_img_id_from_point([self.x_0, self.y_0])
xy_grid = self.ImgManager.xy_grid[id]
xy_limit = np.array(xy_grid) + \
np.array(self.ImgManager.img_resolution_show)
if self.x_0 < xy_limit[0] and self.y_0 < xy_limit[1]:
if x < xy_limit[0] and y < xy_limit[1]:
self.x = x
self.y = y
elif x > xy_limit[0] and y > xy_limit[1]:
self.x = xy_limit[0]
self.y = xy_limit[1]
elif x > xy_limit[0]:
self.x = xy_limit[0]
self.y = y
elif y > xy_limit[1]:
self.x = x
self.y = xy_limit[1]
# show mouse position
x, y = event.GetPosition()
id = self.get_img_id_from_point([x, y])
xy_grid = self.ImgManager.xy_grid[id]
RGBA = self.ImgManager.img.getpixel((int(x), int(y)))
x = x-xy_grid[0]
y = y-xy_grid[1]
self.m_statusBar1.SetStatusText(str(x)+","+str(y)+"/"+str(RGBA), 0)
def img_left_release(self, event):
if self.magnifier.Value != False:
self.start_flag = 0
id = self.get_img_id_from_point([self.x_0, self.y_0])
xy_grid = self.ImgManager.xy_grid[id]
x = self.x-xy_grid[0]
y = self.y-xy_grid[1]
x_0 = self.x_0 - xy_grid[0]
y_0 = self.y_0 - xy_grid[1]
width = np.abs(x-x_0)
height = np.abs(y-y_0)
if width > 5 and height > 5:
self.xy_magnifier = []
self.color_list.append(self.colourPicker_draw.GetColour())
show_scale = self.show_scale.GetLineText(0).split(',')
show_scale = [float(x) for x in show_scale]
points = self.ImgManager.ImgF.sort_box_point(
[x_0, y_0, x, y], show_scale, self.ImgManager.img_resolution_origin, first_point=True)
self.xy_magnifier.append(
points+show_scale+[self.ImgManager.title_setting[2] and self.ImgManager.title_setting[1]])
self.refresh(event)
def img_right_click(self, event):
x, y = event.GetPosition()
id = self.get_img_id_from_point([x, y])
xy_grid = self.ImgManager.xy_grid[id]
x = x-xy_grid[0]
y = y-xy_grid[1]
if self.select_img_box.Value:
# move box
if self.box_id != -1:
show_scale = self.show_scale.GetLineText(0).split(',')
show_scale = [float(x) for x in show_scale]
points = self.move_box_point(x, y, show_scale)
self.xy_magnifier[self.box_id] = points+show_scale+[
self.ImgManager.title_setting[2] and self.ImgManager.title_setting[1]]
self.refresh(event)
else:
# new box
if self.magnifier.Value:
self.color_list.append(self.colourPicker_draw.GetColour())
try:
show_scale = self.show_scale.GetLineText(0).split(',')
show_scale = [float(x) for x in show_scale]
points = self.move_box_point(x, y, show_scale)
self.xy_magnifier.append(
points+show_scale+[self.ImgManager.title_setting[2] and self.ImgManager.title_setting[1]])
except:
self.SetStatusText_(
["-1", "Drawing a box need click left mouse button!", "-1", "-1"])
self.refresh(event)
self.SetStatusText_(["Magnifier", "-1", "-1", "-1"])
else:
self.refresh(event)
def move_box_point(self, x, y, show_scale):
x_0, y_0, x_1, y_1 = self.xy_magnifier[0][0:4]
show_scale_old = self.xy_magnifier[0][4:6]
scale = [show_scale[0]/show_scale_old[0],
show_scale[1]/show_scale_old[1]]
x_0 = int(x_0*scale[0])
x_1 = int(x_1*scale[0])
y_0 = int(y_0*scale[1])
y_1 = int(y_1*scale[1])
x_center_old, y_center_old = self.get_center_box(
[x_0, y_0, x_1, y_1])
delta_x = x-x_center_old
delta_y = y-y_center_old
return self.ImgManager.ImgF.sort_box_point([x_0+delta_x, y_0+delta_y, x_1+delta_x, y_1+delta_y], self.ImgManager.img_resolution_origin, show_scale)
def get_center_box(self, box, more=False):
x_0, y_0, x_1, y_1 = box
width = abs(x_0-x_1)
height = abs(y_0-y_1)
x_center_old = x_0+int((width)/2)
y_center_old = y_0+int((height)/2)
if more:
return [x_center_old, y_center_old, width, height]
else:
return [x_center_old, y_center_old]
def img_wheel(self, event):
# https://wxpython.org/Phoenix/docs/html/wx.MouseEvent.html
# zoom
i_cur = 0
status_toggle = [self.magnifier, self.rotation, self.flip]
if status_toggle[i_cur].Value and self.key_status["ctrl"] == 1:
if event.GetWheelDelta() >= 120:
speed = self.get_speed(name="scale")
self.adjust_show_scale_proportion() # adjust show_scale_proportion
# set show_scale
if event.GetWheelRotation() > 0:
self.show_scale_proportion = self.show_scale_proportion+speed
else:
self.show_scale_proportion = self.show_scale_proportion-speed
if self.show_scale_proportion > 0:
show_scale = [1*(1+self.show_scale_proportion),
1*(1+self.show_scale_proportion)]
elif self.show_scale_proportion < 0:
show_scale = [1/(1-self.show_scale_proportion),
1/(1-self.show_scale_proportion)]
else:
show_scale = [1, 1]
self.show_scale.Value = str(
round(show_scale[0], 2))+","+str(round(show_scale[1], 2))
self.refresh(event)
else:
pass
else:
pass
# move
if self.key_status["ctrl"] == 0 and event.GetWheelDelta() >= 120:
if event.WheelAxis == 0:
if event.GetWheelRotation() > 0:
self.up_img(event)
else:
self.down_img(event)
else:
if event.GetWheelRotation() > 0:
self.right_img(event)
else:
self.left_img(event)
def adjust_show_scale_proportion(self):
# check "cur_scale", and adjust "self.show_scale_proportion"
cur_scale = self.show_scale.GetLineText(0).split(',')
cur_scale = [float(x) for x in cur_scale]
if self.show_scale_proportion > 0:
if cur_scale[0] == round(1*(1+self.show_scale_proportion), 2):
pass
else:
if cur_scale[0] > 1:
self.show_scale_proportion = cur_scale[0]-1
elif cur_scale[0] < 1 and cur_scale[0] > 0:
self.show_scale_proportion = 1-1/cur_scale[0]
elif cur_scale[0] == 1:
self.show_scale_proportion = 0
else:
pass
elif self.show_scale_proportion < 0:
if cur_scale[0] == round(1/(1-self.show_scale_proportion), 2):
pass
else:
if cur_scale[0] > 1:
self.show_scale_proportion = cur_scale[0]-1
elif cur_scale[0] < 1 and cur_scale[0] > 0:
self.show_scale_proportion = 1-1/cur_scale[0]
elif cur_scale[0] == 1:
self.show_scale_proportion = 0
else:
pass
else:
self.show_scale_proportion = 0
def key_down_detect(self, event):
if event.GetKeyCode() == wx.WXK_CONTROL:
if self.key_status["ctrl"] == 0:
self.key_status["ctrl"] = 1
elif event.GetKeyCode() == wx.WXK_SHIFT:
if self.key_status["shift"] == 0:
self.key_status["shift"] = 1
elif self.key_status["shift"] == 1:
self.key_status["shift"] = 0
def key_up_detect(self, event):
if event.GetKeyCode() == wx.WXK_CONTROL:
if self.key_status["ctrl"] == 1:
self.key_status["ctrl"] = 0
elif event.GetKeyCode() == wx.WXK_SHIFT:
pass
def get_speed(self, name="pixel"):
if name == "pixel":
if self.key_status["shift"] == 1:
speed = 5
else:
speed = 1
elif name == "scale":
if self.key_status["shift"] == 1:
speed = 0.5
else:
speed = 0.1
else:
speed = None
return speed
def magnifier_fc(self, event):
self.start_flag = 0
i_cur = 0
status_toggle = [self.magnifier, self.rotation, self.flip]
if status_toggle[i_cur].Value:
self.SetCursor(wx.Cursor(wx.CURSOR_CROSS))
for i in range(len(status_toggle)):
if i != i_cur and status_toggle[i].Value:
status_toggle[i].Value = False
self.SetStatusText_(["Magnifier", "-1", "-1", "-1"])
else:
self.SetCursor(wx.Cursor(wx.CURSOR_ARROW))
self.Refresh()
def rotation_fc(self, event):
i_cur = 1
status_toggle = [self.magnifier, self.rotation, self.flip]
if status_toggle[i_cur].Value:
self.SetCursor(wx.Cursor(wx.CURSOR_POINT_RIGHT))
for i in range(len(status_toggle)):
if i != i_cur and status_toggle[i].Value:
status_toggle[i].Value = False
self.SetStatusText_(["Rotate", "-1", "-1", "-1"])
else:
self.SetCursor(wx.Cursor(wx.CURSOR_ARROW))
self.Refresh()
def flip_fc(self, event):
i_cur = 2
status_toggle = [self.magnifier, self.rotation, self.flip]
if status_toggle[i_cur].Value:
flip_cursor_path = Path(get_resource_path(str(Path("img"))))
flip_cursor_path = str(flip_cursor_path/"flip_cursor.png")
self.SetCursor(
wx.Cursor((wx.Image(flip_cursor_path, wx.BITMAP_TYPE_PNG))))
for i in range(len(status_toggle)):
if i != i_cur and status_toggle[i].Value:
status_toggle[i].Value = False
self.SetStatusText_(["Flip", "-1", "-1", "-1"])
else:
self.SetCursor(wx.Cursor(wx.CURSOR_ARROW))
self.Refresh()
def show_img_init(self):
layout_params = self.set_img_layout()
if layout_params != False:
# setting
self.ImgManager.layout_params = layout_params
if self.ImgManager.type == 0 or self.ImgManager.type == 1:
if self.parallel_to_sequential.Value:
self.ImgManager.set_count_per_action(
layout_params[0]*layout_params[1]*layout_params[2])
else:
if self.parallel_sequential.Value:
self.ImgManager.set_count_per_action(layout_params[1])
else:
self.ImgManager.set_count_per_action(1)
elif self.ImgManager.type == 2 or self.ImgManager.type == 3:
self.ImgManager.set_count_per_action(
layout_params[0]*layout_params[1]*layout_params[2])
def set_img_layout(self):
try:
num_per_img = int(self.num_per_img.GetLineText(0))
if num_per_img == -1:
row_col = self.ImgManager.layout_advice()
self.img_num_per_row.SetValue(str(row_col[1]))
self.img_num_per_column.SetValue(str(row_col[0]))
num_per_img = 1
img_num_per_row = int(self.img_num_per_row.GetLineText(0))
img_num_per_column = int(self.img_num_per_column.GetLineText(0))
gap = self.gap.GetLineText(0).split(',')
gap = [int(x) for x in gap]
show_scale = self.show_scale.GetLineText(0).split(',')
show_scale = [float(x) for x in show_scale]
output_scale = self.output_scale.GetLineText(0).split(',')
output_scale = [float(x) for x in output_scale]
img_resolution = self.img_resolution.GetLineText(0).split(',')
img_resolution = [int(x) for x in img_resolution]
magnifier_scale = self.magnifier_scale.GetLineText(0).split(',')
magnifier_scale = [float(x) for x in magnifier_scale]
if self.checkBox_auto_draw_color.Value:
# 10 colors built into the software
color_list = [
wx.Colour(217, 26, 42, 85/100*255),
wx.Colour(147, 81, 166, 65/100*255),
wx.Colour(85, 166, 73, 65/100*255),
wx.Colour(242, 229, 48, 95/100*255),
wx.Colour(242, 116, 5, 95/100*255),
wx.Colour(242, 201, 224, 95/100*255),
wx.Colour(36, 132, 191, 75/100*255),
wx.Colour(65, 166, 90, 65/100*255),
wx.Colour(214, 242, 206, 95/100*255),
wx.Colour(242, 163, 94, 95/100*255)]
num_box = len(self.xy_magnifier)
if num_box <= len(color_list):
self.color_list = color_list[0:num_box]
else:
self.color_list = color_list + \
color_list[0:num_box-len(color_list)]
color = self.color_list
line_width = self.line_width.GetLineText(0).split(',')
line_width = [int(x) for x in line_width]
title_setting = [self.title_auto.Value, # 0
self.title_show.Value, # 1
self.title_down_up.Value, # 2
self.title_show_parent.Value, # 3
self.title_show_prefix.Value, # 4
self.title_show_name.Value, # 5
self.title_show_suffix.Value, # 6
self.title_font.GetSelection(), # 7
self.title_font_size.Value, # 8
self.font_paths] # 9
if title_setting[0]:
if self.ImgManager.type == 0 or self.ImgManager.type == 1:
# one_dir_mul_dir_auto / one_dir_mul_dir_manual
if self.parallel_sequential.Value or self.parallel_to_sequential.Value:
title_setting[2:7] = [False, True, True, True, False]
else:
title_setting[2:7] = [False, True, True, False, False]
elif self.ImgManager.type == 2:
# one_dir_mul_img
title_setting[2:7] = [False, False, True, True, False]
elif self.ImgManager.type == 3:
# read file list from a list file
title_setting[2:7] = [False, True, True, True, False]
except:
self.SetStatusText_(
["-1", "-1", "***Error: setting***", "-1"])
return False
else:
return [img_num_per_row, # 0
num_per_img, # 1
img_num_per_column, # 2
gap, # 3
show_scale, # 4
output_scale, # 5
img_resolution, # 6
1 if self.magnifier.Value else 0, # 7
magnifier_scale, # 8
color, # 9
line_width, # 10
self.move_file.Value, # 11
self.keep_magnifer_size.Value, # 12
self.image_interp.GetSelection(), # 13
self.show_box.Value, # 14
self.show_box_in_crop.Value, # 15
self.show_original.Value, # 16
title_setting, # 17
self.show_crop.Value, # 18
self.parallel_to_sequential.Value, # 19
self.one_img.Value, # 20
self.box_position.GetSelection(), # 21
self.parallel_sequential.Value, # 22
self.auto_save_all.Value, # 23
# add new in this line
self.checkBox_orientation.Value]
def show_img(self):
# check layout_params change
try:
if self.layout_params_old[0:3] != self.ImgManager.layout_params[0:3] or (self.layout_params_old[19] != self.ImgManager.layout_params[19]):
action_count = self.ImgManager.action_count
if self.ImgManager.type == 0 or self.ImgManager.type == 1:
parallel_to_sequential = self.parallel_to_sequential.Value
else:
parallel_to_sequential = False
self.ImgManager.init(
self.ImgManager.input_path, self.ImgManager.type, parallel_to_sequential)
self.show_img_init()
self.ImgManager.set_action_count(action_count)
if self.index_table_gui:
self.index_table_gui.show_id_table(
self.ImgManager.name_list, self.ImgManager.layout_params)
except:
pass
self.layout_params_old = self.ImgManager.layout_params
self.slider_img.SetValue(self.ImgManager.action_count)
self.slider_value.SetValue(str(self.ImgManager.action_count))
self.slider_value_max.SetLabel(
str(self.ImgManager.max_action_num-1))