Update card production workflow and workspaces
This commit is contained in:
53
tools/card-production/gated_contrast.py
Normal file
53
tools/card-production/gated_contrast.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Shared gated-contrast finish extraction; compilation remains in finish_masks."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
from finish_masks import IllustrationCoverage, _smooth, extract_raw_ridges, prepare_illustration
|
||||
|
||||
RECIPE_PATH = Path(__file__).resolve().parent / 'recipes/gated-contrast-v1.json'
|
||||
RECIPE_SHA256 = '2cfaa28b96c66f440ed810f7a68a44f8584bb1bf2a9da1c71d74a49c6949d37a'
|
||||
|
||||
|
||||
def load_recipe() -> dict:
|
||||
if hashlib.sha256(RECIPE_PATH.read_bytes()).hexdigest() != RECIPE_SHA256:
|
||||
raise ValueError('gated-contrast-v1 recipe changed; create a new revision instead')
|
||||
return json.loads(RECIPE_PATH.read_text())
|
||||
|
||||
|
||||
def prepare_gated_illustration(art: Image.Image) -> IllustrationCoverage:
|
||||
"""Analyze fitted opaque art once using Ezekiel/Ruth's selected contrast math."""
|
||||
recipe = load_recipe()
|
||||
base = prepare_illustration(art)
|
||||
with tempfile.TemporaryDirectory(prefix='gated-contrast-') as temporary:
|
||||
folder = Path(temporary)
|
||||
source = folder / 'art.png'
|
||||
filtered_path = folder / 'filtered.png'
|
||||
canny_path = folder / 'canny.png'
|
||||
art.save(source)
|
||||
subprocess.run(['convert', str(source), '-kuwahara', recipe['preprocess']['imageMagickKuwahara'],
|
||||
str(filtered_path)], check=True)
|
||||
subprocess.run(['convert', str(filtered_path), '-colorspace', 'Gray', '-canny',
|
||||
recipe['preprocess']['canny'], str(canny_path)], check=True)
|
||||
filtered = Image.open(filtered_path).convert('RGB')
|
||||
gray_image = filtered.convert('L')
|
||||
gray = np.asarray(gray_image, dtype=np.float32) / 255.
|
||||
edge = np.asarray(Image.open(canny_path).convert('L').filter(
|
||||
ImageFilter.MaxFilter(recipe['edgeDilation'])), dtype=np.float32) / 255.
|
||||
local_max = np.asarray(gray_image.filter(ImageFilter.MaxFilter(recipe['localMaxSize'])),
|
||||
dtype=np.float32) / 255.
|
||||
smoothed_raw = extract_raw_ridges(filtered).astype(np.float32) / 255.
|
||||
darkness = 1. - _smooth(*recipe['darknessSmoothstep'], gray)
|
||||
contrast = _smooth(*recipe['contrastSmoothstep'], local_max - gray)
|
||||
confidence = np.maximum(recipe['smoothedRawRidgeWeight'] * smoothed_raw,
|
||||
recipe['gatedEdgeWeight'] * edge * darkness * contrast)
|
||||
ridges = np.rint(np.clip(confidence, 0., 1.) * 255).astype(np.uint8)
|
||||
return IllustrationCoverage(base.pane_weights, ridges, recipe['id'])
|
||||
Reference in New Issue
Block a user