-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (86 loc) · 3.27 KB
/
Copy pathmain.py
File metadata and controls
105 lines (86 loc) · 3.27 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
import concurrent.futures
import os
from pathlib import Path
from PIL import Image
import argparse
import time
def convert_image(input_path, output_dir, quality, speed):
try:
img = Image.open(input_path)
img = img.convert("RGBA" if img.mode == "P" else img.mode)
base_name = input_path.stem
output_paths = []
# AVIF Konvertierung
if img.mode in ["RGB", "RGBA"]:
avif_path = output_dir / f"{base_name}.avif"
img.save(
avif_path,
format="AVIF",
quality=quality,
speed=speed,
method="aom"
)
output_paths.append(avif_path)
# WebP Konvertierung
webp_path = output_dir / f"{base_name}.webp"
img.save(
webp_path,
format="WEBP",
quality=quality,
method=6 if quality > 80 else 4
)
output_paths.append(webp_path)
return (input_path, output_paths, True)
except Exception as e:
return (input_path, [], False, str(e))
def main():
parser = argparse.ArgumentParser(description="Konvertiere PNGs zu AVIF und WebP")
parser.add_argument("input", help="Eingabeverzeichnis oder Datei")
parser.add_argument("-o", "--output", default="converted", help="Ausgabeverzeichnis")
parser.add_argument("-q", "--quality", type=int, default=80,
help="Qualität (0-100), Standard: 80")
parser.add_argument("-s", "--speed", type=int, default=5,
help="AVIF Geschwindigkeit (0-9, 0=langsamer/besser), Standard: 5")
parser.add_argument("-t", "--threads", type=int, default=os.cpu_count(),
help="Anzahl Threads, Standard: CPU-Kerne")
args = parser.parse_args()
input_path = Path(args.input)
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
# Dateiliste erstellen
if input_path.is_file():
files = [input_path]
else:
files = list(input_path.glob("**/*.png"))
print(f"🍏 Konvertiere {len(files)} Dateien mit {args.threads} Threads...")
start_time = time.time()
success_count = 0
processed_count = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=args.threads) as executor:
futures = [
executor.submit(
convert_image,
file,
output_dir,
args.quality,
args.speed
)
for file in files
]
for future in concurrent.futures.as_completed(futures):
result = future.result()
processed_count += 1
if result[2]:
success_count += 1
print(f"✅ {result[0]} -> {', '.join(str(p) for p in result[1])}")
else:
print(f"❌ Fehler bei {result[0]}: {result[3]}")
if processed_count % 10 == 0:
print(f"Fortschritt: {processed_count}/{len(files)}")
total_time = time.time() - start_time
print(f"\n🎉 Konvertierung abgeschlossen!")
print(f"⏱ Dauer: {total_time:.2f}s")
print(f"📊 Erfolgsrate: {success_count}/{len(files)}")
print(f"💾 Ausgabeverzeichnis: {output_dir.resolve()}")
if __name__ == "__main__":
main()