-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNiceScaler.py
1456 lines (1207 loc) · 56.5 KB
/
NiceScaler.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
import ctypes
import itertools
import multiprocessing
import os
import os.path
import platform
import shutil
import sys
import threading
import time
import tkinter as tk
import tkinter.font as tkFont
import webbrowser
from multiprocessing.pool import ThreadPool
from timeit import default_timer as timer
from tkinter import *
from tkinter import PhotoImage, ttk
import numpy as np
import cv2
import tkinterDnD
from cv2 import dnn_superres
from moviepy.editor import VideoFileClip
from moviepy.video.io import ImageSequenceClip
from PIL import Image
from win32mica import MICAMODE, ApplyMica
import sv_ttk
global app_name
app_name = "NiceScaler"
version = "1.13"
models_array = ['ESPCN_x2', 'ESPCN_x4',
'FSRCNN_x2', 'FSRCNN_x4',
'LapSRN_x2', 'LapSRN_x4',
'EDSR_x2', 'EDSR_x4']
AI_model = models_array[0]
image_path = "none"
device_list = ['Cpu', 'OpenCL16', 'OpenCL']
device = device_list[2]
input_video_path = ""
target_file_extension = ".png"
file_extension_list = [ '.png', '.jpg', '.jp2', '.bmp', '.tiff' ]
single_image = False
multiple_images = False
video_file = False
multi_img_list = []
video_frames_list = []
frames_upscaled_list = []
vram_multiplier = 1
default_vram_limiter = 8
multiplier_num_tiles = 2
cpu_number = 4
resize_algorithm = cv2.INTER_LINEAR
windows_subversion = int(platform.version().split('.')[2])
if sys.stdout is None: sys.stdout = open(os.devnull, "w")
if sys.stderr is None: sys.stderr = open(os.devnull, "w")
itchme = "https://jangystudio.itch.io/nicescaler"
githubme = "https://github.com/Djdefrag/NiceScaler"
default_font = 'Segoe UI'
background_color = "#181818"
text_color = "#DCDCDC"
selected_button_color = "#ffbf00"
window_width = 1300
window_height = 850
left_bar_width = 410
left_bar_height = window_height
drag_drop_width = window_width - left_bar_width
drag_drop_height = window_height
show_image_width = drag_drop_width * 0.8
show_image_height = drag_drop_width * 0.6
image_text_width = drag_drop_width * 0.8
support_button_height = 95
button1_y = 170
button2_y = button1_y + 90
button3_y = button2_y + 90
button4_y = button3_y + 90
button5_y = button4_y + 90
button6_y = button5_y + 90
supported_file_list = ['.jpg', '.jpeg', '.JPG', '.JPEG',
'.png', '.PNG',
'.webp', '.WEBP',
'.bmp', '.BMP',
'.tif', '.tiff', '.TIF', '.TIFF',
'.mp4', '.MP4',
'.webm', '.WEBM',
'.mkv', '.MKV',
'.flv', '.FLV',
'.gif', '.GIF',
'.m4v', ',M4V',
'.avi', '.AVI',
'.mov', '.MOV',
'.qt', '.3gp', '.mpg', '.mpeg']
supported_video_list = ['.mp4', '.MP4',
'.webm', '.WEBM',
'.mkv', '.MKV',
'.flv', '.FLV',
'.gif', '.GIF',
'.m4v', ',M4V',
'.avi', '.AVI',
'.mov', '.MOV',
'.qt',
'.3gp', '.mpg', '.mpeg']
not_supported_file_list = ['.txt', '.exe', '.xls', '.xlsx', '.pdf',
'.odt', '.html', '.htm', '.doc', '.docx',
'.ods', '.ppt', '.pptx', '.aiff', '.aif',
'.au', '.bat', '.java', '.class',
'.csv', '.cvs', '.dbf', '.dif', '.eps',
'.fm3', '.psd', '.psp', '.qxd',
'.ra', '.rtf', '.sit', '.tar', '.zip',
'.7zip', '.wav', '.mp3', '.rar', '.aac',
'.adt', '.adts', '.bin', '.dll', '.dot',
'.eml', '.iso', '.jar', '.py',
'.m4a', '.msi', '.ini', '.pps', '.potx',
'.ppam', '.ppsx', '.pptm', '.pst', '.pub',
'.sys', '.tmp', '.xlt', '.avif']
ctypes.windll.shcore.SetProcessDpiAwareness(True)
scaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100
font_scale = round(1/scaleFactor, 1)
# ---------------------- /Dimensions ----------------------
# ---------------------- Functions ----------------------
# ------------------------ Utils ------------------------
def remove_dir(name_dir):
if os.path.exists(name_dir): shutil.rmtree(name_dir)
def remove_file(name_file):
if os.path.exists(name_file): os.remove(name_file)
def create_temp_dir(name_dir):
if os.path.exists(name_dir): shutil.rmtree(name_dir)
if not os.path.exists(name_dir): os.makedirs(name_dir, mode=0o777)
def find_by_relative_path(relative_path):
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
def adapt_image_to_show(image_to_prepare):
old_image = image_read(image_to_prepare)
actual_width = old_image.shape[1]
actual_height = old_image.shape[0]
if actual_width >= actual_height:
max_val = actual_width
max_photo_resolution = show_image_width
else:
max_val = actual_height
max_photo_resolution = show_image_height
if max_val >= max_photo_resolution:
downscale_factor = max_val/max_photo_resolution
new_width = round(old_image.shape[1]/downscale_factor)
new_height = round(old_image.shape[0]/downscale_factor)
resized_image = cv2.resize(old_image,
(new_width, new_height),
interpolation = resize_algorithm)
image_write("temp.png", resized_image)
return "temp.png"
else:
new_width = round(old_image.shape[1])
new_height = round(old_image.shape[0])
resized_image = cv2.resize(old_image,
(new_width, new_height),
interpolation = resize_algorithm)
image_write("temp.png", resized_image)
return "temp.png"
def prepare_output_filename(img, AI_model, target_file_extension):
result_path = (img.replace("_resized" + target_file_extension, "").replace(target_file_extension, "")
+ "_" + AI_model + target_file_extension)
return result_path
def delete_list_of_files(list_to_delete):
if len(list_to_delete) > 0:
for to_delete in list_to_delete:
if os.path.exists(to_delete):
os.remove(to_delete)
def write_in_log_file(text_to_insert):
log_file_name = app_name + ".log"
with open(log_file_name,'w') as log_file:
os.chmod(log_file_name, 0o777)
log_file.write(text_to_insert)
log_file.close()
def read_log_file():
log_file_name = app_name + ".log"
with open(log_file_name,'r') as log_file:
os.chmod(log_file_name, 0o777)
step = log_file.readline()
log_file.close()
return step
# IMAGE
def image_write(path, image_data):
_, file_extension = os.path.splitext(path)
r, buff = cv2.imencode(file_extension, image_data)
buff.tofile(path)
def image_read(image_to_prepare, flags=cv2.IMREAD_COLOR):
return cv2.imdecode(np.fromfile(image_to_prepare, dtype=np.uint8), flags)
def resize_image(image_path, resize_factor, target_file_extension):
new_image_path = (os.path.splitext(image_path)[0] + "_resized" + target_file_extension).strip()
old_image = image_read(image_path.strip(), cv2.IMREAD_UNCHANGED)
new_width = int(old_image.shape[1] * resize_factor)
new_height = int(old_image.shape[0] * resize_factor)
resized_image = cv2.resize(old_image,
(new_width, new_height),
interpolation = resize_algorithm)
image_write(new_image_path, resized_image)
def resize_image_list(image_list, resize_factor, target_file_extension):
files_to_delete = []
downscaled_images = []
how_much_images = len(image_list)
index = 1
for image in image_list:
resized_image_path = (os.path.splitext(image)[0] + "_resized" + target_file_extension).strip()
resize_image(image.strip(), resize_factor, target_file_extension)
write_in_log_file("Resizing image " + str(index) + "/" + str(how_much_images))
downscaled_images.append(resized_image_path)
files_to_delete.append(resized_image_path)
index += 1
return downscaled_images, files_to_delete
#VIDEO
def extract_frames_from_video(video_path):
video_frames_list = []
cap = cv2.VideoCapture(video_path)
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
cap.release()
# extract frames
video = VideoFileClip(video_path)
img_sequence = app_name + "_temp" + os.sep + "frame_%01d" + '.jpg'
video_frames_list = video.write_images_sequence(img_sequence, logger = 'bar', fps = frame_rate)
# extract audio
try:
video.audio.write_audiofile(app_name + "_temp" + os.sep + "audio.mp3")
except Exception as e:
pass
return video_frames_list
def video_reconstruction_by_frames(input_video_path, frames_upscaled_list, AI_model, cpu_number):
cap = cv2.VideoCapture(input_video_path)
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
path_as_list = input_video_path.split("/")
video_name = str(path_as_list[-1])
only_path = input_video_path.replace(video_name, "")
for video_type in supported_video_list: video_name = video_name.replace(video_type, "")
upscaled_video_path = (only_path + video_name + "_" + AI_model + ".mp4")
cap.release()
audio_file = app_name + "_temp" + os.sep + "audio.mp3"
clip = ImageSequenceClip.ImageSequenceClip(frames_upscaled_list, fps = frame_rate)
if os.path.exists(audio_file):
clip.write_videofile(upscaled_video_path,
fps = frame_rate,
audio = audio_file,
threads = cpu_number)
else:
clip.write_videofile(upscaled_video_path,
fps = frame_rate,
threads = cpu_number)
def resize_frame(image_path, new_width, new_height, target_file_extension):
new_image_path = image_path.replace('.jpg', "" + target_file_extension)
old_image = cv2.imread(image_path.strip(), cv2.IMREAD_UNCHANGED)
resized_image = cv2.resize(old_image, (new_width, new_height),
interpolation = resize_algorithm)
image_write(new_image_path, resized_image)
def resize_frame_list(image_list, resize_factor, target_file_extension, cpu_number):
downscaled_images = []
old_image = Image.open(image_list[1])
new_width, new_height = old_image.size
new_width = int(new_width * resize_factor)
new_height = int(new_height * resize_factor)
with ThreadPool(cpu_number) as pool:
pool.starmap(resize_frame, zip(image_list,
itertools.repeat(new_width),
itertools.repeat(new_height),
itertools.repeat(target_file_extension)))
for image in image_list:
resized_image_path = image.replace('.jpg', "" + target_file_extension)
downscaled_images.append(resized_image_path)
return downscaled_images
image_to_prepare = image_to_prepare.replace("{", "").replace("}", "")
new_image_path = image_to_prepare
for file_type in supported_file_list:
new_image_path = new_image_path.replace(file_type, target_file_extension)
cv2.imwrite(new_image_path, cv2.imread(image_to_prepare))
return new_image_path
#-----------------------------------------------------------------------
def thread_check_steps_for_images( not_used_var, not_used_var2 ):
time.sleep(3)
try:
while True:
step = read_log_file()
if "Upscale completed" in step or "Error while upscaling" in step or "Stopped upscaling" in step:
info_string.set(step)
stop = 1 + "x"
info_string.set(step)
time.sleep(2)
except:
place_upscale_button()
def thread_check_steps_for_videos( not_used_var, not_used_var2 ):
time.sleep(3)
try:
while True:
step = read_log_file()
if "Upscale video completed" in step or "Error while upscaling" in step or "Stopped upscaling" in step:
info_string.set(step)
stop = 1 + "x"
info_string.set(step)
time.sleep(2)
except:
place_upscale_button()
def prepare_model(AI_model, device):
path_model = find_by_relative_path("AI" + os.sep + AI_model + ".pb")
super_res = dnn_superres.DnnSuperResImpl_create()
super_res.readModel(path_model)
if device == 'OpenCL16':
super_res.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL_FP16)
elif device == 'OpenCL':
super_res.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
elif device == 'Cpu':
super_res.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
if 'x2' in AI_model: upscale_factor = 2
if 'x4' in AI_model: upscale_factor = 4
model_name_for_superres = AI_model.lower().replace('_x2', '').replace('_x4','')
super_res.setModel(model_name_for_superres, upscale_factor)
return super_res
def process_upscale_video_frames(input_video_path,
AI_model,
resize_factor,
device,
target_file_extension,
cpu_number):
try:
start = timer()
create_temp_dir(app_name + "_temp")
write_in_log_file('Extracting video frames...')
frame_list = extract_frames_from_video(input_video_path)
if resize_factor != 1:
write_in_log_file('Resizing video frames...')
frame_list = resize_frame_list(frame_list,
resize_factor,
target_file_extension,
cpu_number)
model = prepare_model(AI_model, device)
write_in_log_file('Upscaling...')
frames_upscaled_list = []
how_many_images = len(frame_list)
done_images = 0
for frame in frame_list:
result_path = prepare_output_filename(frame, AI_model, target_file_extension)
result_image = model.upsample(cv2.imread(frame))
frames_upscaled_list.append(result_path)
cv2.imwrite(result_path, result_image)
done_images += 1
write_in_log_file("Upscaled frame " + str(done_images) + "/" + str(how_many_images))
write_in_log_file("Processing upscaled video...")
video_reconstruction_by_frames(input_video_path, frames_upscaled_list, AI_model, cpu_number)
write_in_log_file("Upscale video completed [" + str(round(timer() - start)) + " sec.]")
remove_dir(app_name + "_temp")
remove_file("temp.png")
except Exception as e:
write_in_log_file('Error while upscaling' + '\n\n' + str(e))
import tkinter as tk
tk.messagebox.showerror(title = 'Error',
message = 'Upscale failed caused by:\n\n' +
str(e) + '\n\n' +
'Please report the error on Github.com or Itch.io.' +
'\n\nThank you :)')
def process_upscale_multiple_images(image_list,
AI_model,
resize_factor,
device,
target_file_extension):
try:
start = timer()
write_in_log_file('Resizing images...')
image_list, files_to_delete = resize_image_list(image_list, resize_factor, target_file_extension)
how_many_images = len(image_list)
done_images = 0
write_in_log_file('Upscaling...')
model = prepare_model(AI_model, device)
for image in image_list:
result_path = prepare_output_filename(image, AI_model, target_file_extension)
result_image = model.upsample(cv2.imread(image))
cv2.imwrite(result_path, result_image)
done_images += 1
write_in_log_file("Upscaled images " + str(done_images) + "/" + str(how_many_images))
write_in_log_file("Upscale completed [" + str(round(timer() - start)) + " sec.]")
delete_list_of_files(files_to_delete)
remove_file("temp.png")
except Exception as e:
write_in_log_file('Error while upscaling' + '\n\n' + str(e))
import tkinter as tk
tk.messagebox.showerror(title = 'Error',
message = 'Upscale failed caused by:\n\n' +
str(e) + '\n\n' +
'Please report the error on Github.com or Itch.io.' +
'\n\nThank you :)')
def upscale_button_command():
global image_path
global multiple_images
global process_upscale
global thread_wait
global video_frames_list
global video_file
global frames_upscaled_list
global input_video_path
global device
global tiles_resolution
global target_file_extension
global cpu_number
remove_file(app_name + ".log")
info_string.set("Loading...")
write_in_log_file("Loading...")
is_ready = user_input_checks()
if is_ready:
if video_file:
place_stop_button()
process_upscale = multiprocessing.Process(target = process_upscale_video_frames,
args = (input_video_path,
AI_model,
resize_factor,
device,
target_file_extension,
cpu_number))
process_upscale.start()
thread_wait = threading.Thread(target = thread_check_steps_for_videos,
args = (1, 2),
daemon = True)
thread_wait.start()
elif multiple_images or single_image:
place_stop_button()
process_upscale = multiprocessing.Process(target = process_upscale_multiple_images,
args = (multi_img_list,
AI_model,
resize_factor,
device,
target_file_extension))
process_upscale.start()
thread_wait = threading.Thread(target = thread_check_steps_for_images,
args = (1, 2), daemon = True)
thread_wait.start()
elif "none" in image_path:
info_string.set("No file selected")
def stop_button_command():
global process_upscale
process_upscale.terminate()
process_upscale.join()
# this will stop thread that check upscaling steps
write_in_log_file("Stopped upscaling")
# ----------------------- /Core ------------------------
# ---------------------- GUI related ----------------------
def opengithub(): webbrowser.open(githubme, new=1)
def openitch(): webbrowser.open(itchme, new=1)
def open_info_ai_model():
info = """This widget allows you to choose between different AIs:\n
- ESPCN | fast | enlarge by 2 & by 4.
- FSRCNN | fast | enlarge by 2 & by 4.
- LapSRN | normal speed | enlarge by 2 & by 4.
- EDSR | really slow | enlarge by 2 & by 4.\n
Try them all and find the one that meets your needs :)"""
info_window = tk.Tk()
info_window.withdraw()
tk.messagebox.showinfo(title = 'AI model', message = info )
info_window.destroy()
def open_info_backend():
info = """This widget allows you to choose the backend on which to run your chosen AI. \n
Keep in mind that "OpenCL" will use your gpu and it tends to be faster than "cpu"""
info_window = tk.Tk()
info_window.withdraw()
tk.messagebox.showinfo(title = 'AI device', message = info)
info_window.destroy()
def open_info_file_extension():
info = """This widget allows you to choose the extension of the file generated by AI.\n
- png | very good quality | supports transparent images
- jpg | good quality | very fast
- jpg2 (jpg2000) | very good quality | not very popular
- bmp | highest quality | slow
- tiff | highest quality | very slow"""
info_window = tk.Tk()
info_window.withdraw()
tk.messagebox.showinfo(title = 'AI output extension', message = info)
info_window.destroy()
def open_info_resize():
info = """This widget allows you to choose the percentage of the resolution input to the AI.\n
For example for a 100x100px image:
- Input resolution 50% => input to AI 50x50px
- Input resolution 100% => input to AI 100x100px
- Input resolution 200% => input to AI 200x200px """
info_window = tk.Tk()
info_window.withdraw()
tk.messagebox.showinfo(title = 'Input resolution %', message = info)
info_window.destroy()
def open_info_cpu():
info = """This widget allows you to choose how much cpu to devote to the app.\n
Where possible the app will use the number of processors you select, for example:
- Extracting frames from videos
- Resizing frames from videos
- Recostructing final video"""
info_window = tk.Tk()
info_window.withdraw()
tk.messagebox.showinfo(title = 'Cpu number', message = info)
info_window.destroy()
def user_input_checks():
global tiles_resolution
global resize_factor
global cpu_number
is_ready = True
# resize factor
try: resize_factor = int(float(str(selected_resize_factor.get())))
except:
info_string.set("Resize % must be a numeric value")
is_ready = False
#if resize_factor > 0 and resize_factor <= 100: resize_factor = resize_factor/100
if resize_factor > 0: resize_factor = resize_factor/100
else:
info_string.set("Resize % must be a value > 0")
is_ready = False
# cpu number
try: cpu_number = int(float(str(selected_cpu_number.get())))
except:
info_string.set("Cpu number must be a numeric value")
is_ready = False
if cpu_number <= 0:
info_string.set("Cpu number value must be > 0")
is_ready = False
elif cpu_number == 1: cpu_number = 1
else: cpu_number = int(cpu_number)
return is_ready
def drop_event_to_image_list(event):
image_list = str(event.data).replace("{", "").replace("}", "")
for file_type in supported_file_list: image_list = image_list.replace(file_type, file_type+"\n")
image_list = image_list.split("\n")
image_list.pop()
return image_list
def file_drop_event(event):
global image_path
global multiple_images
global multi_img_list
global video_file
global single_image
global input_video_path
supported_file_dropped_number, not_supported_file_dropped_number, supported_video_dropped_number = count_files_dropped(event)
all_supported, single_image, multiple_images, video_file, more_than_one_video = check_compatibility(supported_file_dropped_number, not_supported_file_dropped_number, supported_video_dropped_number)
if video_file:
# video section
if not all_supported:
info_string.set("Some files are not supported")
return
elif all_supported:
if multiple_images:
info_string.set("Only one video supported")
return
elif not multiple_images:
if not more_than_one_video:
input_video_path = str(event.data).replace("{", "").replace("}", "")
show_video_in_GUI(input_video_path)
# reset variable
image_path = "none"
multi_img_list = []
elif more_than_one_video:
info_string.set("Only one video supported")
return
else:
# image section
if not all_supported:
if multiple_images:
info_string.set("Some files are not supported")
return
elif single_image:
info_string.set("This file is not supported")
return
elif all_supported:
if multiple_images:
image_list_dropped = drop_event_to_image_list(event)
show_list_images_in_GUI(image_list_dropped)
multi_img_list = image_list_dropped
# reset variable
image_path = "none"
video_frames_list = []
elif single_image:
image_list_dropped = drop_event_to_image_list(event)
show_image_in_GUI(image_list_dropped[0])
multi_img_list = image_list_dropped
# reset variable
image_path = "none"
video_frames_list = []
def check_compatibility(supported_file_dropped_number, not_supported_file_dropped_number,
supported_video_dropped_number):
all_supported = True
single_image = False
multiple_images = False
video_file = False
more_than_one_video = False
if not_supported_file_dropped_number > 0:
all_supported = False
if supported_file_dropped_number + not_supported_file_dropped_number == 1:
single_image = True
elif supported_file_dropped_number + not_supported_file_dropped_number > 1:
multiple_images = True
if supported_video_dropped_number == 1:
video_file = True
more_than_one_video = False
elif supported_video_dropped_number > 1:
video_file = True
more_than_one_video = True
return all_supported, single_image, multiple_images, video_file, more_than_one_video
def count_files_dropped(event):
supported_file_dropped_number = 0
not_supported_file_dropped_number = 0
supported_video_dropped_number = 0
# count compatible images files
for file_type in supported_file_list:
supported_file_dropped_number = supported_file_dropped_number + \
str(event.data).count(file_type)
# count compatible video files
for file_type in supported_video_list:
supported_video_dropped_number = supported_video_dropped_number + \
str(event.data).count(file_type)
# count not supported files
for file_type in not_supported_file_list:
not_supported_file_dropped_number = not_supported_file_dropped_number + \
str(event.data).count(file_type)
return supported_file_dropped_number, not_supported_file_dropped_number, supported_video_dropped_number
def clear_input_variables():
global image_path
global multi_img_list
global video_frames_list
global single_image
global multiple_images
global video_file
# reset variable
image_path = "none"
multi_img_list = []
video_frames_list = []
single_image = False
multiple_images = False
video_file = False
def clear_app_background():
drag_drop = ttk.Label(root,
ondrop = file_drop_event,
relief = "flat",
background = background_color,
foreground = text_color)
drag_drop.place(x = left_bar_width + 50, y=0,
width = drag_drop_width, height = drag_drop_height)
def show_video_in_GUI(video_path):
clear_app_background()
fist_frame = "temp.jpg"
cap = cv2.VideoCapture(video_path)
width = round(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_rate = cap.get(cv2.CAP_PROP_FPS)
duration = num_frames/frame_rate
minutes = int(duration/60)
seconds = duration % 60
path_as_list = video_path.split("/")
video_name = str(path_as_list[-1])
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False: break
image_write(fist_frame, frame)
break
cap.release()
resized_image_to_show = adapt_image_to_show(fist_frame)
global image
image = tk.PhotoImage(file = resized_image_to_show)
resized_image_to_show_width = round(image_read(resized_image_to_show).shape[1])
resized_image_to_show_height = round(image_read(resized_image_to_show).shape[0])
image_x_center = 30 + left_bar_width + drag_drop_width/2 - resized_image_to_show_width/2
image_y_center = drag_drop_height/2 - resized_image_to_show_height/2
image_container = ttk.Notebook(root)
image_container.place(x = image_x_center - 20,
y = image_y_center - 20,
width = resized_image_to_show_width + 40,
height = resized_image_to_show_height + 40)
image_ = ttk.Label(root,
text = "",
image = image,
ondrop = file_drop_event,
anchor = "center",
relief = "flat",
justify = "center",
background = background_color,
foreground = "#202020")
image_.place(x = image_x_center,
y = image_y_center,
width = resized_image_to_show_width,
height = resized_image_to_show_height)
image_info_label = ttk.Label(root,
font = bold11,
text = ( video_name + "\n" + "[" + str(width) + "x" + str(height) + "]" + " | " + str(minutes) + 'm:' + str(round(seconds)) + "s | " + str(num_frames) + "frames | " + str(round(frame_rate)) + "fps" ),
relief = "flat",
justify = "center",
background = background_color,
foreground = "#D3D3D3",
anchor = "center")
image_info_label.place(x = 30 + left_bar_width + drag_drop_width/2 - image_text_width/2,
y = drag_drop_height - 85,
width = image_text_width,
height = 40)
clean_button = ttk.Button(root,
text = ' CLEAN',
image = clear_icon,
compound = 'left',
style = 'Bold.TButton')
clean_button.place(x = image_x_center - 20,
y = image_y_center - 75,
width = 175,
height = 40)
clean_button["command"] = lambda: place_drag_drop_widget()
os.remove(fist_frame)
def show_list_images_in_GUI(image_list):
clear_app_background()
clean_button = ttk.Button(root,
text = ' CLEAN',
image = clear_icon,
compound = 'left',
style = 'Bold.TButton')
clean_button.place(x = left_bar_width + drag_drop_width/2 - 175/2,
y = 125,
width = 175,
height = 40)
clean_button["command"] = lambda: place_drag_drop_widget()
final_string = "\n"
counter_img = 0
for elem in image_list:
counter_img += 1
if counter_img <= 8:
img = image_read(elem.strip())
width = round(img.shape[1])
height = round(img.shape[0])
img_name = str(elem.split("/")[-1])
final_string += (str(counter_img) + ". " + img_name + " | [" + str(width) + "x" + str(height) + "]" + "\n\n")
else:
final_string += "and others... \n"
break
list_height = 420
list_width = 750
images_list_label = ttk.Label(root,
text = final_string,
ondrop = file_drop_event,
font = bold12,
anchor = "n",
relief = "flat",
justify = "left",
background = background_color,
foreground = "#D3D3D3",
wraplength = list_width)
images_list_label.place(x = left_bar_width + drag_drop_width/2 - list_width/2,
y = drag_drop_height/2 - list_height/2 -25,
width = list_width,
height = list_height)
images_counter = ttk.Entry(root,
foreground = text_color,
ondrop = file_drop_event,
font = bold12,
justify = 'center')
images_counter.insert(0, str(len(image_list)) + ' images')
images_counter.configure(state='disabled')
images_counter.place(x = left_bar_width + drag_drop_width/2 - 175/2,
y = drag_drop_height/2 + 225,
width = 200,
height = 42)
def show_image_in_GUI(original_image):
clear_app_background()
original_image = original_image.replace('{', '').replace('}', '')
resized_image_to_show = adapt_image_to_show(original_image)
global image
image = tk.PhotoImage(file = resized_image_to_show)
resized_image_to_show_width = round(image_read(resized_image_to_show).shape[1])
resized_image_to_show_height = round(image_read(resized_image_to_show).shape[0])
image_x_center = 30 + left_bar_width + drag_drop_width/2 - resized_image_to_show_width/2
image_y_center = drag_drop_height/2 - resized_image_to_show_height/2
image_container = ttk.Notebook(root)
image_container.place(x = image_x_center - 20,
y = image_y_center - 20,
width = resized_image_to_show_width + 40,
height = resized_image_to_show_height + 40)
image_ = ttk.Label(root,
text = "",
image = image,
ondrop = file_drop_event,
anchor = "center",
relief = "flat",
justify = "center",
background = background_color,
foreground = "#202020")
image_.place(x = image_x_center,
y = image_y_center,
width = resized_image_to_show_width,
height = resized_image_to_show_height)
img_name = str(original_image.split("/")[-1])
width = round(image_read(original_image).shape[1])
height = round(image_read(original_image).shape[0])
image_info_label = ttk.Label(root,
font = bold11,
text = (img_name + " | [" + str(width) + "x" + str(height) + "]"),
relief = "flat",
justify = "center",
background = background_color,
foreground = "#D3D3D3",
anchor = "center")
image_info_label.place(x = 30 + left_bar_width + drag_drop_width/2 - image_text_width/2,
y = drag_drop_height - 70,
width = image_text_width,
height = 40)
clean_button = ttk.Button(root,
text = ' CLEAN',
image = clear_icon,
compound = 'left',
style = 'Bold.TButton')
clean_button.place(x = image_x_center - 20,
y = image_y_center - 75,
width = 175,
height = 40)
clean_button["command"] = lambda: place_drag_drop_widget()
def place_drag_drop_widget():
clear_input_variables()
clear_app_background()
text_drop = (" DROP FILES HERE \n\n"
+ " ⥥ \n\n"
+ " IMAGE - jpg png tif bmp webp \n\n"
+ " IMAGE LIST - jpg png tif bmp webp \n\n"
+ " VIDEO - mp4 webm mkv flv gif avi mov mpg qt 3gp \n\n")
drag_drop = ttk.Notebook(root, ondrop = file_drop_event)