"""Render two registered optical studies and their default web presentation.""" from pathlib import Path import hashlib import json import numpy as np from PIL import Image from scipy.ndimage import gaussian_filter, sobel ROOT = Path(__file__).resolve().parents[1] INPUT = ROOT / 'source/human-profile.jpg' OUT = ROOT / 'assets/hero' CARBON = np.array([8, 9, 11], dtype=np.float32) PALETTE = np.array([[98,127,232], [164,154,207], [177,221,229], [98,127,232]], dtype=np.float32) VIEWS = {'wide':((-430,125,570,708),(1920,1120)), 'phone':((74,140,474,780),(840,1344))} SLANT = .30 HALF = .060 INITIAL = .38 def clamp(v): return np.clip(v,0,1) def smooth(v): v = clamp(v) return v*v*(3-2*v) def mask(x, y, reveal): threshold = (1+SLANT+HALF) * (1-reveal) - HALF*reveal return clamp((x + SLANT*y - threshold + HALF)/(2*HALF)) def spectral(luma): image = gaussian_filter(luma, 1.30, mode='reflect') gx, gy = (sobel(image,axis=axis,mode='reflect')/8 for axis in (1,0)) mag = np.hypot(gx,gy) u = np.mod(np.arctan2(gy,gx)/(2*np.pi),1)*3 k = np.floor(u).astype(np.int16) frac = (u-k)[...,None] hue = PALETTE[k]*(1-frac) + PALETTE[k+1]*frac reference = float(np.quantile(mag,.996)) strength = clamp(mag/reference)**.595 return hue*strength[...,None], reference def resample(a, box, size): left, top, right, bottom = box h,w=a.shape[:2] border=((max(-top,0),max(bottom-h,0)),(max(-left,0),max(right-w,0)),(0,0)) expanded=np.pad(a,border,mode='edge') image=Image.fromarray(np.uint8(np.round(np.clip(expanded,0,255)))) ox,oy=border[1][0],border[0][0] return np.asarray(image.crop((left+ox,top+oy,right+ox,bottom+oy)).resize(size,Image.Resampling.LANCZOS),dtype=np.float32) def exposure(view,x,y): if view == 'wide': feather=.015+.985*smooth((x-.22)/.45) overhead=.13+.87*smooth(y/.24) floor=.22+.78*smooth((1-y)/.32) right=smooth((.93-x)/.055) return feather*overhead*floor*right overhead=.12+.88*smooth(y/.23) floor=.025+.975*(1-smooth((y-.30)/.27)) return overhead*floor def encode(pixels,path): Image.fromarray(np.uint8(np.clip(np.round(pixels),0,255))).save(path,format='WEBP',quality=94,method=5) def render(): OUT.mkdir(exist_ok=True,parents=True) rgb = np.asarray(Image.open(INPUT).convert('RGB'),dtype=np.float32)/255 signal = rgb @ np.array([.2126,.7152,.0722],dtype=np.float32) local = signal-gaussian_filter(signal, 7) film = clamp((signal-.035)/.94 + local*.92)**.69 base = film[...,None]*np.array([220,224,229]) color = rgb-signal[...,None] photo = np.clip(base+color*45,0,255) edges,reference = spectral(signal) manifest = {'source_sha256':hashlib.sha256(INPUT.read_bytes()).hexdigest(), 'source_pixels':[int(rgb.shape[1]),int(rgb.shape[0])], 'smoothing_sigma_source_px':1.30,'magnitude_reference_quantile':.996, 'magnitude_reference_value':reference,'slant':SLANT,'half_width':HALF, 'initial_reveal':INITIAL,'views':{}} for view,(box,size) in VIEWS.items(): w,h=size yy,xx=np.mgrid[:h,:w].astype('float32') xx=(xx+.5)/w yy=(yy+.5)/h light=exposure(view,xx,yy)[...,None] source=CARBON + light * resample(photo,box,size) processed=CARBON + (light**.77) * resample(edges,box,size)*1.26 encode(source,OUT/f'photo-{view}.webp') encode(processed,OUT/f'signal-{view}.webp') source=np.asarray(Image.open(OUT/f'photo-{view}.webp'),dtype=np.float32) processed=np.asarray(Image.open(OUT/f'signal-{view}.webp'),dtype=np.float32) alpha=mask(xx,yy,INITIAL)[...,None] encode(source*(1-alpha)+processed*alpha,OUT/f'still-{view}.webp') assert np.max(mask(xx,yy,0))<1e-5 and np.min(mask(xx,yy,1))>.99999 manifest['views'][view]={'crop_source_coordinates':box,'output_pixels':size, 'out_of_bounds_fill':'edge padding; left margin falls to carbon', 'starting_fraction_fully_processed':round(float((alpha>=.999).mean()),4), 'starting_fraction_in_transition':round(float(((alpha<.999)&(alpha>.001)).mean()),4)} (OUT/'parameters.json').write_text(json.dumps(manifest,indent=2)+'\n') if __name__=='__main__': render()