#!/usr/bin/env hython """Finishing comp for the Karma XPU EXR sequences, run with Houdini's hython (OpenImageIO + OpenColorIO that ship with Houdini 22, ACES 2.0 CG config). Scene-linear ACEScg in -> bloom from highlights, soft vignette, fine grain -> ACES 2.0 SDR display transform (sRGB) -> 8-bit PNG. Writes a 16:9 master and a 9:16 centre crop per frame. Usage: hython comp_aces.py [--bloom 0.10] [--vig 0.35] [--exp 0.0] """ import os import sys import numpy as np import OpenImageIO as oiio import PyOpenColorIO as ocio CFG = "/opt/hfs22.0/packages/ocio/cg-config-v3.0.0_aces-v2.0_ocio-v2.5.ocio" def arg(flag, default): return float(sys.argv[sys.argv.index(flag) + 1]) if flag in sys.argv else default def blur(img, radius): """Separable box blur x3 (close to gaussian), numpy only.""" out = img k = max(1, int(radius)) for _ in range(3): for axis in (0, 1): c = np.cumsum(np.pad(out, [(k + 1, k) if a == axis else (0, 0) for a in range(3)], mode="edge"), axis=axis, dtype=np.float64) n = out.shape[axis] hi = np.take(c, np.arange(2 * k + 1, 2 * k + 1 + n), axis=axis) lo = np.take(c, np.arange(0, n), axis=axis) out = ((hi - lo) / (2 * k + 1)).astype(np.float32) return out def main(): src, out_w, out_v = sys.argv[1], sys.argv[2], sys.argv[3] first, last = int(sys.argv[4]), int(sys.argv[5]) bloom_amt, vig_amt, exp = arg("--bloom", 0.10), arg("--vig", 0.35), arg("--exp", 0.0) cfg = ocio.Config.CreateFromFile(CFG) display = cfg.getDefaultDisplay() view = cfg.getDefaultView(display) dvt = ocio.DisplayViewTransform(src="ACEScg", display=display, view=view) cpu = cfg.getProcessor(dvt).getDefaultCPUProcessor() print(f"[COMP] OCIO {ocio.__version__} ยท ACEScg -> {display} / {view}") rng = np.random.default_rng(7) for f in range(first, last + 1): path = src % f buf = oiio.ImageBuf(path) spec = buf.spec() names = list(spec.channelnames) idx = [names.index(c) for c in ("R", "G", "B")] if "R" in names else [names.index(c) for c in ("C.r", "C.g", "C.b")] px = buf.get_pixels(oiio.FLOAT) rgb = np.ascontiguousarray(px[:, :, idx]).astype(np.float32) rgb = np.nan_to_num(rgb, nan=0.0, posinf=50.0, neginf=0.0) rgb *= 2.0 ** exp h, w, _ = rgb.shape # bloom: only energy above diffuse white feeds the glow hot = np.clip(rgb - 1.0, 0.0, 30.0) small = hot[::4, ::4] glow = blur(small, 6) * 0.6 + blur(small, 22) * 0.4 glow = np.repeat(np.repeat(glow, 4, axis=0), 4, axis=1)[:h, :w] rgb = rgb + glow * bloom_amt # vignette (multiplicative, in linear) yy, xx = np.mgrid[0:h, 0:w].astype(np.float32) r2 = ((xx - w / 2) / (w / 2)) ** 2 + ((yy - h / 2) / (h / 2)) ** 2 * 0.72 rgb *= (1.0 - vig_amt * np.clip(r2 - 0.25, 0, 1.6) / 1.6)[..., None] ** 1.6 out = np.ascontiguousarray(rgb) cpu.applyRGB(out) out = np.clip(out, 0, 1) # fine monochrome grain, stronger in mid-tones lum = out.mean(2, keepdims=True) out = np.clip(out + rng.normal(0, 0.006, (h, w, 1)).astype(np.float32) * (0.4 + 1.2 * lum * (1 - lum) * 4 * 0.25), 0, 1) img8 = (out * 255.0 + 0.5).astype(np.uint8) for dst, arr in ((out_w % f, img8), (out_v % f, None)): os.makedirs(os.path.dirname(dst), exist_ok=True) if arr is None: # 9:16 centre crop scaled to 1080x1920 cw = int(round(h * 9 / 16)) x0 = (w - cw) // 2 crop = oiio.ImageBuf(oiio.ImageSpec(cw, h, 3, oiio.UINT8)) crop.set_pixels(oiio.ROI(0, cw, 0, h), np.ascontiguousarray(img8[:, x0:x0 + cw])) res = oiio.ImageBufAlgo.resize(crop, roi=oiio.ROI(0, 1080, 0, 1920)) res.write(dst) else: o = oiio.ImageBuf(oiio.ImageSpec(w, h, 3, oiio.UINT8)) o.set_pixels(oiio.ROI(0, w, 0, h), arr) o.write(dst) if f == first or f % 24 == 0 or f == last: print(f"[COMP] frame {f}") if __name__ == "__main__": main()