Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
499 lines (433 loc) · 15.5 KB
/
Copy pathmain.py
File metadata and controls
499 lines (433 loc) · 15.5 KB
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
#!/usr/bin/env python3
"""
Kleines Icon-Tool mit GUI.
Features:
- Input: großes Bild (z.B. 1028x1028 PNG)
- Output: Unterordner mit:
- PNGs in den Größen wie im Beispiel-ZIP
- icon.ico (128x128)
- Ordnername frei wählbar
- Basis-Ausgabe-Ordner:
- Default: <Projektordner>/exports
- kann über Dialog geändert werden
- Optional: Ordner als ZIP packen
- Resizable Fenster + "schönere" UI mit ttk
Start:
python main.py
"""
import shutil
from pathlib import Path
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttk
from PIL import Image, ImageTk # pip install pillow
# PNG-Ziele (Dateiname -> (Breite, Höhe))
PNG_SIZES = {
"32x32.png": (32, 32),
"128x128.png": (128, 128),
"128x128@2x.png": (256, 256),
"icon.png": (512, 512),
"Square30x30Logo.png": (30, 30),
"Square44x44Logo.png": (44, 44),
"Square71x71Logo.png": (71, 71),
"Square89x89Logo.png": (89, 89),
"Square107x107Logo.png": (107, 107),
"Square142x142Logo.png": (142, 142),
"Square150x150Logo.png": (150, 150),
"Square284x284Logo.png": (284, 284),
"Square310x310Logo.png": (310, 310),
"StoreLogo.png": (50, 50),
}
# icon.ico soll nur 128x128 enthalten (wie in deinem Beispiel)
ICO_SIZE = (128, 128)
BASE_DIR = Path(__file__).resolve().parent
ASSETS_DIR = BASE_DIR / "assets"
ICONS_DIR = ASSETS_DIR / "icons"
APP_ICON_ICO = ICONS_DIR / "icon.ico"
APP_ICON_PNG = ICONS_DIR / "128x128.png"
APP_ICON_FALLBACK_PNG = ICONS_DIR / "icon.png"
BACKGROUND_IMAGE = ASSETS_DIR / "BG.jpeg"
DEFAULT_EXPORT_DIR = BASE_DIR / "exports"
PALETTE = {
"card_bg": "#081424",
"text": "#f5f7ff",
"muted_text": "#9eb4d3",
"accent": "#5cc3ff",
"accent_hover": "#71cffc",
"primary": "#1ed8c7",
"primary_hover": "#25e6d4",
"button_text": "#03121b",
"entry_bg": "#0f253c",
"status_text": "#d4e9ff",
"disabled_bg": "#3a4b63",
}
FONT_FAMILY = "Segoe UI"
def center_crop_square(img: Image.Image) -> Image.Image:
"""Bild mittig auf Quadrat zuschneiden, falls nötig."""
w, h = img.size
if w == h:
return img
side = min(w, h)
left = (w - side) // 2
top = (h - side) // 2
right = left + side
bottom = top + side
return img.crop((left, top, right, bottom))
def generate_icons(input_path: Path, output_dir: Path) -> None:
"""Erzeugt alle PNGs + icon.ico in output_dir."""
if not input_path.is_file():
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}")
output_dir.mkdir(parents=True, exist_ok=True)
img = Image.open(input_path).convert("RGBA")
img = center_crop_square(img)
# PNGs erzeugen
for filename, size in PNG_SIZES.items():
resized = img.resize(size, Image.LANCZOS)
out_path = output_dir / filename
resized.save(out_path, format="PNG")
# ICO erzeugen (nur 128x128)
ico_img = img.resize(ICO_SIZE, Image.LANCZOS)
ico_path = output_dir / "icon.ico"
ico_img.save(ico_path, format="ICO", sizes=[ICO_SIZE])
class IconApp(tk.Tk):
def __init__(self):
super().__init__()
# --- Fenster ---
self.title("IconCreator")
self.minsize(720, 640)
self.geometry("800x660")
# Resizebar aktiv
self.resizable(True, True)
# Farbpalette + Styles für eine moderne Optik
self.palette = PALETTE
self.style = ttk.Style(self)
self._setup_style()
self._icon_photo = None
self._init_window_icon()
# Hintergrundbild vorbereiten
self.bg_image_raw = None
self.bg_photo = None
self.background_label = None
self._init_background()
# --- State-Variablen ---
self.input_path = tk.StringVar()
# Standardpfad: <Projektordner>/exports
self.output_path = tk.StringVar(value=str(DEFAULT_EXPORT_DIR))
self.folder_name = tk.StringVar(value="icons")
self.zip_var = tk.BooleanVar(value=True)
# --- Haupt-Container (damit Resize funktioniert) ---
main_frame = ttk.Frame(self, style="Card.TFrame", padding=24)
main_frame.pack(fill="both", expand=True, padx=24, pady=24)
# Grid-Konfiguration für saubere Resize
main_frame.columnconfigure(0, weight=1)
# --- Ueberschrift ---
header_frame = ttk.Frame(main_frame, style="Card.TFrame")
header_frame.grid(row=0, column=0, sticky="we", pady=(0, 8))
header_frame.columnconfigure(0, weight=1)
lbl_title = ttk.Label(
header_frame,
text="Icon Creator",
style="Header.TLabel"
)
lbl_title.grid(row=0, column=0, sticky="w")
lbl_sub = ttk.Label(
header_frame,
text="Konvertiert eine Logo Datei in kleine Icons",
style="Subheader.TLabel"
)
lbl_sub.grid(row=1, column=0, sticky="w", pady=(2, 0))
ttk.Separator(main_frame, orient="horizontal", style="Card.TSeparator").grid(
row=1, column=0, sticky="we", pady=8
)
# --- Eingabebild ---
frame_input = ttk.Labelframe(
main_frame,
text=" Eingabebild ",
style="Card.TLabelframe",
padding=(12, 8)
)
frame_input.grid(row=2, column=0, sticky="we", pady=6)
frame_input.columnconfigure(0, weight=1)
entry_input = ttk.Entry(
frame_input,
textvariable=self.input_path,
style="Filled.TEntry"
)
entry_input.grid(row=0, column=0, padx=(4, 10), pady=4, sticky="we")
btn_input = ttk.Button(
frame_input,
text="Durchsuchen...",
command=self.browse_input,
style="Accent.TButton"
)
btn_input.grid(row=0, column=1, padx=(0, 4), pady=4)
# --- Export-Einstellungen ---
frame_export = ttk.Labelframe(
main_frame,
text=" Export ",
style="Card.TLabelframe",
padding=(12, 8)
)
frame_export.grid(row=3, column=0, sticky="we", pady=6)
frame_export.columnconfigure(0, weight=1)
# Basis-Ausgabe-Ordner
lbl_output = ttk.Label(
frame_export,
text="Basis-Ausgabe-Ordner (Default: ./exports):",
style="Card.TLabel"
)
lbl_output.grid(row=0, column=0, columnspan=2, sticky="w", padx=4, pady=(4, 0))
entry_output = ttk.Entry(
frame_export,
textvariable=self.output_path,
style="Filled.TEntry"
)
entry_output.grid(row=1, column=0, padx=(4, 10), pady=4, sticky="we")
btn_output = ttk.Button(
frame_export,
text="Durchsuchen...",
command=self.browse_output,
style="Accent.TButton"
)
btn_output.grid(row=1, column=1, padx=(0, 4), pady=4)
# Ordnername
lbl_folder = ttk.Label(
frame_export,
text="Ordnername (wird unterhalb angelegt):",
style="Card.TLabel"
)
lbl_folder.grid(row=2, column=0, columnspan=2, sticky="w", padx=4, pady=(6, 0))
entry_folder = ttk.Entry(
frame_export,
textvariable=self.folder_name,
width=24,
style="Filled.TEntry"
)
entry_folder.grid(row=3, column=0, padx=4, pady=4, sticky="w")
# ZIP-Option
chk_zip = ttk.Checkbutton(
frame_export,
text="Ordner nach dem Erzeugen als ZIP packen",
variable=self.zip_var,
style="Card.TCheckbutton"
)
chk_zip.grid(row=4, column=0, columnspan=2, padx=4, pady=(4, 4), sticky="w")
# --- Generate Button ---
btn_generate = ttk.Button(
main_frame,
text="Icons generieren",
command=self.on_generate,
style="Primary.TButton"
)
btn_generate.grid(row=4, column=0, pady=(12, 6), sticky="we")
# --- Status ---
self.status_label = ttk.Label(
main_frame,
text="",
anchor="w",
style="Status.TLabel"
)
self.status_label.grid(row=5, column=0, sticky="we", pady=(2, 0))
def _setup_style(self):
"""Konfiguriert Styles f�r die modernisierte UI."""
try:
self.style.theme_use("clam")
except tk.TclError:
pass
palette = self.palette
style = self.style
style.configure("Card.TFrame", background=palette["card_bg"])
style.configure(
"Card.TLabelframe",
background=palette["card_bg"],
foreground=palette["text"],
borderwidth=0,
relief="flat",
)
style.configure(
"Card.TLabelframe.Label",
background=palette["card_bg"],
foreground=palette["muted_text"],
)
style.configure(
"Header.TLabel",
font=(FONT_FAMILY, 20, "bold"),
foreground=palette["text"],
background=palette["card_bg"],
)
style.configure(
"Subheader.TLabel",
font=(FONT_FAMILY, 11),
foreground=palette["muted_text"],
background=palette["card_bg"],
)
style.configure(
"Card.TLabel",
font=(FONT_FAMILY, 10),
foreground=palette["text"],
background=palette["card_bg"],
)
style.configure(
"Status.TLabel",
font=(FONT_FAMILY, 10),
foreground=palette["status_text"],
background=palette["card_bg"],
)
style.configure(
"Filled.TEntry",
fieldbackground=palette["entry_bg"],
foreground=palette["text"],
insertcolor=palette["text"],
padding=6,
relief="flat",
)
style.map(
"Filled.TEntry",
foreground=[("disabled", palette["muted_text"])],
fieldbackground=[("disabled", palette["entry_bg"])],
)
style.configure(
"Card.TCheckbutton",
background=palette["card_bg"],
foreground=palette["text"],
focuscolor=palette["card_bg"],
)
style.map(
"Card.TCheckbutton",
foreground=[("disabled", palette["muted_text"])],
)
style.configure(
"Primary.TButton",
padding=(12, 8),
font=(FONT_FAMILY, 11, "bold"),
background=palette["primary"],
foreground=palette["button_text"],
borderwidth=0,
)
style.configure(
"Accent.TButton",
padding=(10, 6),
font=(FONT_FAMILY, 10),
background=palette["accent"],
foreground=palette["button_text"],
borderwidth=0,
)
style.map(
"Primary.TButton",
background=[
("active", palette["primary_hover"]),
("disabled", palette["disabled_bg"]),
],
foreground=[("disabled", palette["muted_text"])],
)
style.map(
"Accent.TButton",
background=[
("active", palette["accent_hover"]),
("disabled", palette["disabled_bg"]),
],
foreground=[("disabled", palette["muted_text"])],
)
style.configure("Card.TSeparator", background=palette["muted_text"])
def _init_window_icon(self):
"""Setzt die App-Icons fuer Titelleiste, Taskleiste und Vorschau."""
if APP_ICON_ICO.is_file():
try:
self.iconbitmap(default=str(APP_ICON_ICO))
except Exception:
pass
png_candidates = [APP_ICON_PNG, APP_ICON_FALLBACK_PNG]
for png_path in png_candidates:
if not png_path.is_file():
continue
try:
img = Image.open(png_path)
self._icon_photo = ImageTk.PhotoImage(img)
img.close()
self.iconphoto(False, self._icon_photo)
break
except Exception:
self._icon_photo = None
def _init_background(self):
"""L�dt das Hintergrundbild und sorgt f�r vollst�ndige Skalierung."""
if not BACKGROUND_IMAGE.is_file():
return
try:
self.bg_image_raw = Image.open(BACKGROUND_IMAGE)
except Exception:
self.bg_image_raw = None
return
self.background_label = tk.Label(self, borderwidth=0)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
self.background_label.lower()
self.bind("<Configure>", self._handle_background_resize, add="+")
self.after(50, self._refresh_background)
def _handle_background_resize(self, event):
if event.widget is self and self.bg_image_raw is not None:
self._update_background_image(event.width, event.height)
def _refresh_background(self):
if self.bg_image_raw is None:
return
width = max(1, self.winfo_width())
height = max(1, self.winfo_height())
self._update_background_image(width, height)
def _update_background_image(self, width, height):
width = max(1, int(width))
height = max(1, int(height))
resized = self.bg_image_raw.resize((width, height), Image.LANCZOS)
self.bg_photo = ImageTk.PhotoImage(resized)
self.background_label.configure(image=self.bg_photo)
# --- Event-Handler ---
def browse_input(self):
path = filedialog.askopenfilename(
title="Eingabebild wählen",
filetypes=[
("Bilddateien", "*.png;*.jpg;*.jpeg;*.bmp;*.gif"),
("Alle Dateien", "*.*"),
],
)
if path:
self.input_path.set(path)
def browse_output(self):
path = filedialog.askdirectory(title="Basis-Ausgabe-Ordner wählen")
if path:
self.output_path.set(path)
def on_generate(self):
in_path = self.input_path.get().strip()
out_path = self.output_path.get().strip()
folder_name = self.folder_name.get().strip()
if not in_path:
messagebox.showerror("Fehler", "Bitte ein Eingabebild auswählen.")
return
if not out_path:
messagebox.showerror("Fehler", "Bitte einen Basis-Ausgabe-Ordner auswählen.")
return
if not folder_name:
messagebox.showerror("Fehler", "Bitte einen Ordnernamen eingeben.")
return
base_out_dir = Path(out_path)
target_dir = base_out_dir / folder_name
try:
generate_icons(Path(in_path), target_dir)
zip_msg = ""
if self.zip_var.get():
# ZIP erstellen: <basis>/<folder_name>.zip
zip_base = base_out_dir / folder_name
zip_path = shutil.make_archive(
str(zip_base),
"zip",
root_dir=base_out_dir,
base_dir=folder_name
)
zip_msg = f"\nZIP erstellt: {zip_path}"
self.status_label.config(text=f"Fertig! Icons in: {target_dir}{zip_msg}")
messagebox.showinfo(
"Fertig",
f"Icons wurden erzeugt in:\n{target_dir}{zip_msg}"
)
except Exception as e:
messagebox.showerror("Fehler bei der Generierung", str(e))
self.status_label.config(text="Fehler aufgetreten.")
if __name__ == "__main__":
app = IconApp()
app.mainloop()