-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
243 lines (208 loc) · 8.15 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
import PySimpleGUI as sg
import os.path
from wand import image
from enum import Enum
import PIL.Image
import io
import base64
import gettext
import webbrowser
import logging
import sys
SUPPORT_IMAGE_TYPE = (".png", ".tga")
def make_full_file_path_with_dds(image_file_name):
filename_with_dds = os.path.splitext(image_file_name)[0] + '.dds'
if output_folder_path:
return os.path.join(output_folder_path, filename_with_dds)
else:
return os.path.join(image_folder_path, filename_with_dds)
def convert_to_bytes(file_or_bytes, resize=None):
"""
Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.
Turns into PNG format in the process so that can be displayed by tkinter
:param file_or_bytes: either a string filename or a bytes base64 image object
:type file_or_bytes: (Union[str, bytes])
:param resize: optional new size
:type resize: (Tuple[int, int] or None)
:return: (bytes) a byte-string object
:rtype: (bytes)
"""
if isinstance(file_or_bytes, str):
img = PIL.Image.open(file_or_bytes)
else:
try:
img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
except Exception as e:
data_bytes_io = io.BytesIO(file_or_bytes)
img = PIL.Image.open(data_bytes_io)
cur_width, cur_height = img.size
if resize:
new_width, new_height = resize
scale = min(new_height/cur_height, new_width/cur_width)
img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS)
with io.BytesIO() as bio:
img.save(bio, format="PNG")
del img
return bio.getvalue()
def convert_image_to_dds():
i = 0
for image_file_name in file_names:
logging.debug('Converting ' + image_file_name)
file_full_path = os.path.join(image_folder_path, image_file_name)
output_full_path = make_full_file_path_with_dds(image_file_name)
logging.debug('Converting ' + image_file_name + " :: file full path: " + file_full_path)
logging.debug('Converting ' + image_file_name + " :: output full path: " + output_full_path)
try:
with image.Image(filename=fr'{file_full_path}') as img:
img.compression = 'dxt5'
img.save(filename=output_full_path)
i = i + 1
sg.one_line_progress_meter('Converting Image Files',
i, len(file_names),
orientation='h',
bar_color=('#F47264', '#FFFFFF'))
except Exception as e:
logging.error(e.with_traceback(sys.exc_info()[2]))
sg.popup_error_with_traceback(f'An error happened', e.with_traceback(sys.exc_info()[2]))
def get_list_file_names_recursive(target_folder, arr, relative_path=''):
full_path = os.path.join(target_folder, relative_path)
files_list = os.listdir(full_path)
for f in files_list:
if os.path.isfile(os.path.join(full_path, f)):
if f.lower().endswith(SUPPORT_IMAGE_TYPE):
arr.append(os.path.join(relative_path, f))
else:
get_list_file_names_recursive(target_folder, arr, os.path.join(relative_path, f))
return arr
def setup_log_dir():
if not os.path.isdir(logging_path):
os.makedirs(logging_path)
class EventKey(Enum):
IMAGE_FOLDER_CHOSEN = '-IMAGE FOLDER-'
OUTPUT_FOLDER_CHOSEN = '-OUTPUT FOLDER-'
FILE_LIST = '-FILE LIST-'
IMAGE_SHOW = '-IMAGE-'
IMAGE_NAME_SHOW = '-TOUT-'
Convert = 'convert'
output_folder_path = ''
image_folder_path = ''
logging_path = os.path.join(os.path.expanduser('~'), 'Documents', 'DDS Converter')
file_names = []
# set up logging
setup_log_dir()
logging.basicConfig(filename=os.path.join(logging_path, 'log.log'),
level=logging.DEBUG,
format='%(asctime)s %(levelname)s:: %(message)s')
def main_window(my_window=None):
menu_def = [_('&About'), ['Github']], [_('&Language'), ['&ko', 'en']],
file_list_column = [
[
sg.Menu(menu_def, pad=(10, 10))
],
[
sg.Text(_("Image Folder")),
sg.In(size=(25, 1), enable_events=True, key=EventKey.IMAGE_FOLDER_CHOSEN),
sg.FolderBrowse(),
],
[
sg.Text(_("Output Folder")),
sg.In(size=(25, 1), enable_events=True, key=EventKey.OUTPUT_FOLDER_CHOSEN),
sg.FolderBrowse(),
],
[
sg.Listbox(
values=[], enable_events=True, size=(40, 20), key=EventKey.FILE_LIST
)
],
[
sg.Button(button_text=EventKey.Convert.value)
]
]
# For now will only show the name of the file that was chosen
image_viewer_column = [
[sg.Text(_("Choose an image from list on left:"))],
[sg.Text(size=(40, 1), key="-TOUT-")],
[sg.Image(key=EventKey.IMAGE_SHOW)],
]
# ----- Full layout -----
layout = [
[
sg.Column(file_list_column),
sg.VSeperator(),
sg.Column(image_viewer_column),
]
]
new_window = sg.Window(_("Image Converter"), layout, icon='images/logo.ico')
if my_window is not None:
my_window.close()
return new_window
try:
localedir = 'locale'
translate = gettext.translation('messages', localedir=localedir, languages=['ko'])
_ = translate.gettext
except Exception as e:
print(e)
logging.error(e)
sys.exit(-1)
window = main_window()
logging.info('Window initialized')
# Run the Event Loop
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
# Folder name was filled in, make a list of files in the folder
if event == EventKey.IMAGE_FOLDER_CHOSEN:
folder = values[EventKey.IMAGE_FOLDER_CHOSEN]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = get_list_file_names_recursive(folder, [])
window[EventKey.FILE_LIST].update(fnames)
image_folder_path = folder
file_names = fnames
print('image folder path: ' + image_folder_path)
print('file names: ' + file_names.__str__())
logging.debug('file names: ' + file_names.__str__())
logging.debug('image folder path: ' + image_folder_path)
# Output folder was chosen, store path
elif event == EventKey.OUTPUT_FOLDER_CHOSEN:
output_folder_path = values[EventKey.OUTPUT_FOLDER_CHOSEN]
print('output folder path: ' + output_folder_path)
logging.debug('output folder path: ' + output_folder_path)
elif event == EventKey.Convert.value:
if not image_folder_path:
sg.popup(_('No Image Folder Selected!'))
elif not len(file_names):
sg.popup(_('No Image Files Found on Folder!'))
else:
logging.info('Converting started')
convert_image_to_dds()
sg.popup(_('Done!'))
logging.info('Converting finished')
elif event == 'ko':
translate = gettext.translation('messages', localedir=localedir, languages=['ko'])
_ = translate.gettext
print(_("Language changed to KO"))
window = main_window(window)
elif event == 'en':
translate = gettext.translation('messages', localedir=localedir, languages=['en'])
_ = translate.gettext
print(_("Language changed to EN"))
window = main_window(window)
elif event == 'Github':
webbrowser.open('https://github.com/probaku1234/DDS_Converter')
elif event == EventKey.FILE_LIST: # A file was chosen from the listbox
if len(file_names):
try:
filename = os.path.join(
values[EventKey.IMAGE_FOLDER_CHOSEN], values[EventKey.FILE_LIST][0]
)
window["-TOUT-"].update(filename)
window[EventKey.IMAGE_SHOW].update(data=convert_to_bytes(filename))
except Exception as e:
logging.error(e)
sg.popup_error_with_traceback(f'An error happened', e)
window.close()