-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIR.py
198 lines (158 loc) · 6.04 KB
/
BIR.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
import os
import re
from PIL import Image
import argparse
import customtkinter as ctk
import tkinter as tk
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def calc_new_height(width, height, new_width):
return round(new_width * height / width)
def resize(root, file, new_width, new_img_name, keep_aspect_ratio, new_height):
original_img_path = os.path.join(root, file)
new_img_path = os.path.join(root, new_img_name)
try:
new_width = int(new_width)
except:
raise TypeError(
f'-w, --new-width or NEW_WIDTH must be a number. Sent "{NEW_WIDTH}".')
pillow_img = Image.open(original_img_path)
width, height = pillow_img.size
if keep_aspect_ratio:
new_height = calc_new_height(width, height, new_width)
else:
new_height = new_height
new_img = pillow_img.resize((new_width, new_height), Image.LANCZOS)
try:
new_img.save(
new_img_path,
optimize=True,
quality=100,
subsampling=0,
exif=pillow_img.info.get('exif')
)
except:
try:
new_img.save(
new_img_path,
optimize=True,
subsampling=0,
quality=100,
)
except:
raise RuntimeError(f'Could not convert "{original_img_path}".')
print(f'Saved at {new_img_path}')
def is_image(extension):
extension_lowercase = extension.lower()
return bool(re.search(r"^\.(jpe?g|png)$", extension_lowercase))
def files_checks(root, file, height, width, keep_aspect_ratio):
filename, extension = os.path.splitext(file)
if not is_image(extension):
return
flag = '_CONVERTED'
if flag in file:
return
new_img_name = filename + flag + extension
resize(root=root, file=file, new_width=width, new_img_name=new_img_name, new_height=height,
keep_aspect_ratio=keep_aspect_ratio)
def files_loop(root, files, height, width, keep_aspect_ratio):
for file in files:
files_checks(root, file, height=height, width=width, keep_aspect_ratio=keep_aspect_ratio)
def main_function(root_folder, height, width, keep_aspect_ratio):
for root, dirs, files in os.walk(root_folder):
files_loop(root, files, height=height, width=width, keep_aspect_ratio=keep_aspect_ratio)
def main_gui():
base = ctk.CTk()
base.title("BIM")
base.geometry("400x550")
base.resizable(False, False)
mode_var = ctk.StringVar(value="Change aspect ratio")
height_entry_visibility = ctk.StringVar(value="normal")
def option_menu_callback(choice):
if choice == "keep aspect ratio":
width_entry.configure(state="disable")
if choice == "Change aspect ratio":
width_entry.configure(state="normal")
def start_bim():
mode = mode_var.get()
keep_aspect_ratio = True
height = 0
width = int(width_entry.get())
path = path_entry.get()
if mode == "Change aspect ratio":
keep_aspect_ratio = False
height = int(height_entry.get())
main_function(root_folder=path,
height=height,
width=width,
keep_aspect_ratio=keep_aspect_ratio)
return
label_font = ctk.CTkFont(family="sans", size=80)
entry_font = ctk.CTkFont(family="sans", size=20)
entry_font2 = ctk.CTkFont(family="sans", size=14)
frame = ctk.CTkFrame(master=base)
frame.pack(padx=45, pady=45)
"""Label"""
label = ctk.CTkLabel(master=frame,
text="BIR",
fg_color=("black", "gray75"),
text_color=("white", "black"),
corner_radius=8,
font=label_font)
label.pack(padx=20, pady=50)
"""Path entry"""
path_entry = ctk.CTkEntry(master=frame,
placeholder_text="Base directory",
width=250,
height=40,
border_width=2,
corner_radius=10,
font=entry_font)
path_entry.pack(padx=20, pady=20)
"""Option menu"""
mode_option_box = ctk.CTkOptionMenu(master=frame,
values=["Keep aspect ratio",
"Change aspect ratio"],
variable=mode_var,
command=option_menu_callback)
mode_option_box.pack(padx=10, pady=10)
"""Width entry"""
width_entry = ctk.CTkEntry(master=frame,
placeholder_text="Width",
width=150,
height=20,
border_width=2,
corner_radius=10,
font=entry_font2,
)
width_entry.pack(padx=10, pady=10)
"""Height entry"""
height_entry = ctk.CTkEntry(master=frame,
placeholder_text="Height",
width=150,
height=20,
border_width=2,
corner_radius=10,
font=entry_font2,
state=height_entry_visibility.get())
height_entry.pack(padx=10, pady=10)
"""Start Button"""
start_button = ctk.CTkButton(master=frame,
text="Start",
command=start_bim,
corner_radius=10,
height=40,
width=100,
font=entry_font)
start_button.pack(padx=10, pady=10)
base.mainloop()
if __name__ == '__main__':
main_gui()