← back to Wallco Ai

scripts/build-aztec-tifs.py

52 lines

#!/usr/bin/env python3
"""Build 150-DPI print-master TIFs for the live aztec-kilim tiles from their
SVG vector masters. Square seamless tile → 5400x5400 (36in @ 150 DPI), the
wallco clean-edge standard. Flat-ink art + LZW = small files despite the size.

Run with the cairosvg venv:
  scripts/vectorize/.venv/bin/python scripts/build-aztec-tifs.py <id> <id> ...
Outputs data/tif/aztec-kilim/design_<id>.tif and prints one
'<id>|<abspath>|<w>|<h>|<bytes>' line per TIF for the DB updater to consume.
"""
import sys, os, io, glob, cairosvg
from PIL import Image

REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GEN  = os.path.join(REPO, 'data', 'generated')
OUT  = os.path.join(REPO, 'data', 'tif', 'aztec-kilim')
PX   = 5400  # 36 inches at 150 DPI

os.makedirs(OUT, exist_ok=True)
Image.MAX_IMAGE_PIXELS = None  # 5400^2 is fine; disable the decompression-bomb guard

def svg_for_id(did):
    # local_path basenames are aztec_kilim_<ts>_<idx>_<seed>.png; the SVG master
    # shares the stem. We can't map id->stem without the DB, so the caller passes
    # the stem via env or we glob by the png basename passed on the side.
    hits = glob.glob(os.path.join(GEN, f'*_{did}_*.svg'))  # not reliable; fallback below
    return hits[0] if hits else None

def main():
    # Expect lines on stdin: "<id>\t<svg_basename>"  (DB-resolved by the shell)
    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue
        parts = line.split('\t')
        did = parts[0].strip()
        svg_base = parts[1].strip() if len(parts) > 1 else None
        svg = os.path.join(GEN, svg_base) if svg_base else svg_for_id(did)
        if not svg or not os.path.exists(svg):
            print(f'{did}|MISSING_SVG|0|0|0')
            continue
        # SVG -> PNG bytes at target px, then PIL -> TIFF with 150 DPI + LZW.
        png_bytes = cairosvg.svg2png(url=svg, output_width=PX, output_height=PX)
        im = Image.open(io.BytesIO(png_bytes)).convert('RGB')
        out_path = os.path.join(OUT, f'design_{did}.tif')
        im.save(out_path, format='TIFF', compression='tiff_lzw', dpi=(150, 150))
        sz = os.path.getsize(out_path)
        print(f'{did}|{out_path}|{im.width}|{im.height}|{sz}')

if __name__ == '__main__':
    main()