SPB Git

Repository Public

captures.py 2.5 KB 53 lines python

1#!/usr/bin/env python32"""Prépare les captures des pages de détail : AVIF + WebP, deux tailles, et content/captures.json (dimensions).34Usage : python3 scripts/captures.py <dossier source> [<dossier source>…]5Chaque dossier source contient un sous-dossier par slug (lou-ka/, immo-ka/…) avec des JPEG ou PNG ; en cas de6doublon, le dernier dossier cité gagne. Seuls les fichiers cités dans content/<slug>.json sont traités.7Demande Pillow, avifenc et cwebp (macOS : brew install libavif webp).8"""9import json, os, subprocess, sys, tempfile10from PIL import Image1112ici = os.path.dirname(os.path.abspath(__file__))13racine = os.path.dirname(ici)14sources = sys.argv[1:]15dims = {}16for nom in sorted(os.listdir(os.path.join(racine, "content"))):17    if not nom.endswith(".json") or nom == "captures.json":18        continue19    contenu = json.load(open(os.path.join(racine, "content", nom)))20    slug = contenu["slug"]21    sortie = os.path.join(racine, "public", "captures", slug)22    os.makedirs(sortie, exist_ok=True)23    dims[slug] = {}24    for c in contenu["captures"]:25        src = None26        for s in sources:27            for sous in (os.path.join(s, slug, "shots"), os.path.join(s, slug)):28                p = os.path.join(sous, c["fichier"])29                if os.path.exists(p):30                    src = p31        if not src:32            sys.exit(f"introuvable : {slug}/{c['fichier']}")33        im = Image.open(src).convert("RGB")34        base = os.path.splitext(c["fichier"])[0]35        # site de bureau : 720 et 1440 px de large ; téléphone et iPhone : 300 et 60036        cibles = [720, 1440] if c["appareil"] == "web" else [300, 600]37        tailles = []38        for t in cibles:39            t = min(t, im.width)40            if t in tailles:41                continue42            tailles.append(t)43            r = im.resize((t, round(im.height * t / im.width)), Image.LANCZOS)44            with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:45                r.save(f.name)46            subprocess.run(["avifenc", "-q", "56", "-s", "3", "--yuv", "420", f.name, os.path.join(sortie, f"{base}-{t}.avif")], check=True, capture_output=True)47            subprocess.run(["cwebp", "-quiet", "-q", "78", f.name, "-o", os.path.join(sortie, f"{base}-{t}.webp")], check=True)48            os.unlink(f.name)49        g = max(tailles)50        dims[slug][base] = {"w": g, "h": round(im.height * g / im.width), "tailles": tailles}51    print(slug, len(dims[slug]), "captures")52json.dump(dims, open(os.path.join(racine, "content", "captures.json"), "w"), indent=1)53
Files main
Menu

Theme