75 lines · 3.2 KB · PythonRaw
#!/usr/bin/env python3"""Import the personnel file's Codex-painted layers, keyed off green. python3 scripts/import-person-file-materials.py [~/lane-briefs/person-file]The source directory holds originals/{folder,clip,badge}.png and the imageprompts. No words or portraits are baked into the materials. Shadows comefrom the cut silhouettes, as with the rolodex. Requires Pillow and numpy."""import jsonfrom pathlib import Pathimport sysimport numpy as npfrom PIL import Image, ImageFilterCATALOGUE = Path(__file__).resolve().parent.parent / 'apps/loom/Sources/CottonPaper/Media.xcassets'def keyed(path, width): rgb = np.asarray(Image.open(path).convert('RGB'), dtype=np.float32) / 255 red, green, blue = rgb[..., 0], rgb[..., 1], rgb[..., 2] t = np.clip((green - np.maximum(red, blue) - 0.10) / 0.30, 0, 1) alpha = 1 - t * t * (3 - 2 * t) # Remove the green spill only at the keyed edge. rgb[..., 1] = np.where(alpha < 0.99, np.minimum(green, np.maximum(red, blue)), green) rgba = Image.fromarray(np.uint8(np.dstack((rgb, alpha)) * 255), 'RGBA') mask = Image.fromarray(np.uint8(alpha > 0.5) * 255) rgba = rgba.crop(mask.getbbox()) return rgba.resize((width, round(width * rgba.height / rgba.width)), Image.Resampling.LANCZOS)def shadow(image): pad = round(image.width * 0.08) size = (image.width + 2 * pad, image.height + 2 * pad) silhouette = Image.new('L', size) silhouette.paste(image.getchannel('A'), (pad + round(image.width * 0.012), pad + round(image.width * 0.018))) soft = silhouette.filter(ImageFilter.GaussianBlur(image.width * 0.025)).point(lambda a: round(a * 0.32)) contact = silhouette.filter(ImageFilter.GaussianBlur(image.width * 0.005)).point(lambda a: round(a * 0.28)) result = Image.new('RGBA', size, (15, 12, 8, 0)) result.putalpha(Image.fromarray(np.maximum(np.asarray(soft), np.asarray(contact)))) return resultdef write(name, image, layers): image.save(layers / f'{name}.png', optimize=True) folder = CATALOGUE / f'{name}.imageset' folder.mkdir(exist_ok=True) image.save(folder / f'{name}.png', optimize=True) (folder / 'Contents.json').write_text(json.dumps({ 'images': [ {'filename': f'{name}.png', 'idiom': 'universal', 'scale': '3x'}, {'filename': f'{name}.png', 'idiom': 'mac', 'scale': '2x'}, ], 'info': {'author': 'xcode', 'version': 1}, }, indent=2) + '\n') print(f'{name}: {image.width} × {image.height}')def main(source): layers = source / 'layers' layers.mkdir(exist_ok=True) for part, width in [('folder', 1050), ('clip', 84), ('badge', 270)]: image = keyed(source / 'originals' / f'{part}.png', width) write(f'person-file-{part}', image, layers) if part != 'clip': write(f'person-file-{part}-shadow', shadow(image), layers) total = sum(p.stat().st_size for p in CATALOGUE.glob('person-file-*.imageset/*')) if total >= 4_000_000: sys.exit(f'person file materials exceed 4 MB: {total}') print(f'{total:,} bytes in all')if __name__ == '__main__': main(Path(sys.argv[1]).expanduser() if len(sys.argv) > 1 else Path.home() / 'lane-briefs/person-file')