„scripts/bilder_verkleinern.py“ hinzufügen

main
spertl 17 hours ago
parent a33d6d7e63
commit 2f20c3ab2f

@ -0,0 +1,84 @@
# pip install Pillow
import os
from PIL import Image
def resize_proportional(root_path):
extensions = ('.jpg', '.jpeg', '.png')
for subdir, dirs, files in os.walk(root_path):
for filename in files:
# Nur Bilder verarbeiten und bereits generierte Varianten ignorieren
if filename.lower().endswith(extensions) and "_100" not in filename:
file_path = os.path.join(subdir, filename)
try:
with Image.open(file_path) as img:
# Farbmodus für PNG sicherstellen
img = img.convert("RGBA") if img.mode in ("RGBA", "P") else img.convert("RGB")
original_width, original_height = img.size
base_name = os.path.splitext(filename)[0]
# --- Variante 1: Breite = 100 ---
w_target = 100
# Berechnung der proportionalen Höhe
h_proportional = int((w_target / original_width) * original_height)
img_w = img.resize((w_target, h_proportional), Image.Resampling.LANCZOS)
img_w.save(os.path.join(subdir, f"{base_name}_w100.png"), "PNG")
# --- Variante 2: Höhe = 100 ---
h_target = 100
# Berechnung der proportionalen Breite
w_proportional = int((h_target / original_height) * original_width)
img_h = img.resize((w_proportional, h_target), Image.Resampling.LANCZOS)
img_h.save(os.path.join(subdir, f"{base_name}_h100.png"), "PNG")
print(f"Erledigt: {filename} (w100 & h100)")
except Exception as e:
print(f"Fehler bei {file_path}: {e}")
# Start im aktuellen Verzeichnis
resize_proportional('.')
import os
from PIL import Image
def resize_images_recursive(root_path):
target_size = (100, 100)
extensions = ('.jpg', '.jpeg', '.png')
# os.walk durchsucht den Ordner und alle Unterordner
for subdir, dirs, files in os.walk(root_path):
for filename in files:
if filename.lower().endswith(extensions):
# Überspringe Dateien, die bereits das Suffix haben (verhindert Doppelte)
if "_100.png" in filename:
continue
file_path = os.path.join(subdir, filename)
try:
with Image.open(file_path) as img:
# Modus für PNG vorbereiten
if img.mode in ("RGBA", "P"):
img = img.convert("RGBA")
else:
img = img.convert("RGB")
# Verkleinern
img_resized = img.resize(target_size, Image.Resampling.LANCZOS)
# Neuen Dateinamen mit Suffix im gleichen Verzeichnis erstellen
base_name = os.path.splitext(filename)[0]
new_filename = f"{base_name}_100.png"
output_path = os.path.join(subdir, new_filename)
img_resized.save(output_path, "PNG")
print(f"Gespeichert: {output_path}")
except Exception as e:
print(f"Fehler bei {file_path}: {e}")
# Starte die Suche im aktuellen Verzeichnis
resize_images_recursive('.')
Loading…
Cancel
Save