← back to Japan Enrich

make_lily_fill.py

48 lines

#!/usr/bin/env python3
"""
Close the Lilycolor prod image gap. Most Lilycolor cards are served locally from
images/all_c/ (extracted by fetch_c_zips.sh). ~224 SKUs, however, only have an
image on the Mac2-mounted Henry volume (/Volumes/Henry/dw-lily-images/<sku>.jpg) —
prod has no Henry mount, so those cards render blank.

This downscales each Henry-only gap image to a 256px swatch named <sku>_H.jpg and
drops it in images/lily-fill/. Rsync that into prod images/all_c/ and the viewer's
allcIndex (keyed by normSku(filename.split('_')[0])) resolves it as /img/lily/<sku>,
taking Lilycolor to 100% local coverage. $0 local (Pillow). Requires Henry mounted.
"""
import os, json, re
from PIL import Image

ALLC = "images/all_c"
HENRY = "/Volumes/Henry/dw-lily-images"
OUT = "images/lily-fill"
STAGING = "staging/lilycolor-unified-staging.jsonl"
os.makedirs(OUT, exist_ok=True)

def norm(s): return re.sub(r"[^A-Za-z0-9]", "", (s or "")).upper()

def main():
    allc_keys = {norm(fn.split("_")[0]) for fn in os.listdir(ALLC)
                 if re.search(r"\.(jpe?g|png)$", fn, re.I)}
    rows = [json.loads(l) for l in open(STAGING) if l.strip()]
    made = miss = skip = 0
    for r in rows:
        sku = r.get("mfr_sku")
        if norm(sku) in allc_keys:
            skip += 1; continue                      # already served via all_c
        src = os.path.join(HENRY, f"{sku}.jpg")
        if not os.path.exists(src): miss += 1; continue
        dst = os.path.join(OUT, f"{sku}_H.jpg")       # split('_')[0]=sku -> allcIndex key
        if os.path.exists(dst) and os.path.getsize(dst) > 0: made += 1; continue
        try:
            with Image.open(src) as im:
                im = im.convert("RGB"); im.thumbnail((256, 256))
                im.save(dst, "JPEG", quality=82, optimize=True)
            made += 1
        except Exception:
            miss += 1
    print(f"lily-fill: {made} gap-swatches -> {OUT} ({skip} already via all_c, {miss} missing/failed)")

if __name__ == "__main__":
    main()