-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycon.py
363 lines (333 loc) · 11.7 KB
/
pycon.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
#!/usr/bin/env python3
#%%
'''
██████ ██ ██ ██████ ██████ ███ ██
██ ██ ██ ██ ██ ██ ██ ████ ██
██████ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██████ ██ ████
A PNG to ICO image converter made with PySimpleGUI and Pillow.
-
Author:
Mister Riley
sorzkode@proton.me
https://github.com/sorzkode
MIT License
Copyright (c) 2024 Mister Riley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
# Dependencies
from tkinter.font import BOLD, ITALIC
import PySimpleGUI as sg
from PIL import Image
from os.path import exists
# Font styles
font_title = ('Lucida', 14, BOLD)
font_subtitle = ('Lucida', 12, ITALIC)
font_button = ('Lucida', 12, BOLD)
font_subtext = ('Lucida', 10, BOLD)
# PySimpleGUI version info
sgversion = sg.version
# GUI window theme
sg.theme("Default1")
# Application dropdown menu
app_menu = [["&Help", ["&Usage", "&About"]]]
# All GUI elements
layout = [
[
sg.Menu(
app_menu,
tearoff=False,
key='-MENU-'
)
],
[
sg.Image(
filename='assets\pyclogo.png',
key='-LOGO-'
)
],
[
sg.Text(
"Image Selection:",
font=font_title
),
sg.In(
"Select your PNG file...",
font=font_subtitle,
pad=(5, 15),
enable_events=True,
disabled=True,
key='-FILEPATH-'
),
sg.Button(
"Browse",
font=font_button,
pad=(5, 15),
disabled=False
),
],
[
sg.Text(
"ICO Properties:",
font=font_subtext
),
sg.Spin(
values=(
"16x16",
"24x24",
"32x32",
"64x64",
),
initial_value="32x32",
disabled=True,
enable_events=True,
readonly=True,
key='-PROPERTIES-'
),
],
[
sg.Button(
"Convert",
font=font_button,
pad=(5, 15),
disabled=True
),
sg.Button(
"Clear",
font=font_button,
pad=(5, 15),
disabled=True
),
sg.Button(
"Exit",
font=font_button,
pad=(5, 15)
),
],
]
class PyconWindow:
'''Initializing main application'''
def __init__(self, window):
self.window = sg.Window(
"Pycon",
layout,
resizable=True,
icon="assets\pycon.ico",
grab_anywhere=True,
keep_on_top=True,
)
self.start()
def image_selection(self):
'''Selecting PNG Image to convert to ICO'''
try:
png_image = sg.popup_get_file(
'Select a PNG Image for conversion',
file_types=(("Image Files", "*.png"),),
title='File Selection',
grab_anywhere=True,
keep_on_top=True
)
if not png_image:
sg.popup_cancel(
'No file selected, try again',
grab_anywhere=True,
keep_on_top=True
)
return None
self.window['-PROPERTIES-'].update(disabled=False)
self.window['Convert'].update(disabled=False)
self.window['-FILEPATH-'].update(png_image)
return png_image
except Exception as e:
sg.popup(
f'Error while selecting the PNG image: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
return None
def reset_defaults(self):
'''Resetting application interface defaults'''
self.window['-FILEPATH-'].update("Cleared....select a new PNG file")
self.window['-PROPERTIES-'].update(disabled=True)
self.window['Convert'].update(disabled=True)
def save_location(self):
'''Defining save path for new ICO file'''
try:
save_path = sg.popup_get_folder(
'Select Save Location',
title='Folder Selection',
grab_anywhere=True,
keep_on_top=True
)
if not save_path:
sg.popup_cancel(
'No folder selected, try again',
grab_anywhere=True,
keep_on_top=True
)
return None
return save_path
except Exception as e:
sg.popup(
f'Error while selecting the save location: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
return None
def new_name(self):
'''Defining ICO file name'''
try:
ico_name = sg.popup_get_text(
'Enter a name for your ICO file',
title='ICO Name',
grab_anywhere=True,
keep_on_top=True
)
if not ico_name:
sg.popup_cancel(
'No name entered, try again',
grab_anywhere=True,
keep_on_top=True
)
return None
return ico_name
except Exception as e:
sg.popup(
f'Error while entering the ICO file name: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
return None
def convert_image(self):
try:
png_image = self.values['-FILEPATH-']
save_path = self.save_location()
ico_name = self.new_name()
ico_properties = self.values['-PROPERTIES-']
size_mapping = {
'16x16': (16, 16),
'24x24': (24, 24),
'32x32': (32, 32),
'64x64': (64, 64)
}
ico_width, ico_height = size_mapping.get(ico_properties, (32, 32))
new_file = str(save_path + '/' + ico_name + '.ico')
file_exists = exists(new_file)
open_image = Image.open(png_image)
if file_exists:
warning_message = sg.popup_ok_cancel(
f"WARNING! {ico_name} already exists in this directory. Replace?",
grab_anywhere=True,
keep_on_top=True
)
if warning_message == "OK":
try:
open_image.save(
new_file,
format='ICO',
sizes=[(ico_width, ico_height)]
)
sg.popup(
'Success!',
grab_anywhere=True,
keep_on_top=True,
)
self.reset_defaults()
except Exception as e:
sg.popup(
f'Error while saving the ICO file: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
else: # Cancel button was clicked
sg.popup(
'Conversion cancelled. Try another name.',
grab_anywhere=True,
keep_on_top=True
)
else:
try:
open_image.save(
new_file,
format='ICO',
sizes=[(ico_width, ico_height)]
)
sg.popup(
'Success!',
grab_anywhere=True,
keep_on_top=True,
)
self.reset_defaults()
except Exception as e:
sg.popup(
f'Error while saving the ICO file: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
except Exception as e:
sg.popup(
f'Error while converting the image to ICO: {str(e)}',
grab_anywhere=True,
keep_on_top=True,
)
def about_gui(self):
'''About menu item'''
sg.popup(
"A PNG to ICO converter.",
"",
"Author: Mister Riley",
"Website: https://github.com/sorzkode",
"License: MIT",
"",
f"PySimpleGUI Version: {sgversion}",
"",
grab_anywhere=True,
keep_on_top=True,
title="About",
)
def usage_gui(self):
'''Usage menu option'''
sg.popup(
"Follow these basic steps:",
"",
'1. Click the "Browse" button to select your PNG file',
'2. Use the spinbox to set your ICO dimensions',
'3. Click the "Convert" button',
'4. Select your save path',
'5. Type a name for your ICO file',
"",
grab_anywhere=True,
keep_on_top=True,
title="Usage",
)
def start(self):
'''Initialization function'''
# Event loop when buttons are pressed / actions are taken in the app
while True:
self.event, self.values = self.window.read()
# Window closed event
if self.event == sg.WIN_CLOSED or self.event == "Exit":
break
# Matching events to functions
match self.event:
case "Browse":
self.image_selection()
case "Convert":
self.convert_image()
case "Clear":
self.reset_defaults()
case "About":
self.about_gui()
case "Usage":
self.usage_gui()
self.window.close()
if __name__ == "__main__":
PyconWindow(sg)