66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""Erzeugt aus den Original-Fotos in src/assets/img/originals/ die Web-Varianten.
|
|||
|
|
|
|||
|
|
Für jedes Bild entstehen AVIF + WebP + JPEG in mehreren Breiten, damit <picture>
|
|||
|
|
auf dem Handy kleine Dateien lädt. Kein Hochskalieren: Breiten größer als das
|
|||
|
|
Original werden übersprungen.
|
|||
|
|
|
|||
|
|
Aufruf: python3 scripts/make-photos.py
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
from PIL import Image
|
|||
|
|
|
|||
|
|
IMG = Path(__file__).resolve().parent.parent / "src" / "assets" / "img"
|
|||
|
|
SRC = IMG / "originals"
|
|||
|
|
|
|||
|
|
# Quelldatei, Zielname, gewünschte Breiten
|
|||
|
|
JOBS: list[tuple[str, str, tuple[int, ...]]] = [
|
|||
|
|
("portrait-sonja.png", "portrait-sonja", (308, 460)),
|
|||
|
|
("gruppe-bank.png", "gruppe-bank", (480, 932)),
|
|||
|
|
("bollerwagen.png", "bollerwagen", (235, 387)),
|
|||
|
|
("tagesablauf-uhr.png", "tagesablauf-uhr", (480, 900)),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# Zusätzlicher Beschnitt in Pixeln (links, oben, rechts, unten) – z. B. um am Bildrand
|
|||
|
|
# angeschnittene Fremdpersonen zu entfernen.
|
|||
|
|
CROPS: dict[str, tuple[int, int, int, int]] = {
|
|||
|
|
"portrait-sonja.png": (16, 0, 2, 0),
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def save_variants(image: Image.Image, name: str, width: int) -> None:
|
|||
|
|
height = round(image.height * width / image.width)
|
|||
|
|
resized = image.resize((width, height), Image.LANCZOS)
|
|||
|
|
base = IMG / f"{name}-{width}"
|
|||
|
|
|
|||
|
|
resized.save(base.with_suffix(".avif"), format="AVIF", quality=52, speed=4)
|
|||
|
|
resized.save(base.with_suffix(".webp"), format="WEBP", quality=78, method=6)
|
|||
|
|
# Fallback: JPEG deckt alle Browser ab (Porträt/Fotos – kein Transparenzbedarf)
|
|||
|
|
resized.save(base.with_suffix(".jpg"), format="JPEG", quality=82, optimize=True,
|
|||
|
|
progressive=True)
|
|||
|
|
print(f" ✓ {name}-{width} (avif/webp/jpg, {width}×{height})")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main() -> None:
|
|||
|
|
for filename, name, widths in JOBS:
|
|||
|
|
path = SRC / filename
|
|||
|
|
if not path.exists():
|
|||
|
|
print(f" ! fehlt: {path.name}")
|
|||
|
|
continue
|
|||
|
|
image = Image.open(path).convert("RGB")
|
|||
|
|
if filename in CROPS:
|
|||
|
|
left, top, right, bottom = CROPS[filename]
|
|||
|
|
image = image.crop((left, top, image.width - right, image.height - bottom))
|
|||
|
|
for width in widths:
|
|||
|
|
if width > image.width:
|
|||
|
|
continue
|
|||
|
|
save_variants(image, name, width)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|