Cotton code
59 lines · 2.7 KB · PythonRaw
#!/usr/bin/env python3"""Import the fixed knob bodies and one moving knurl tooth into Loom.Run with Pillow installed:    python scripts/import-rolodex-knobs.py [~/lane-briefs/rolodex-knobs]smooth-frame-original.png is Codex's inpainting of rolodex-frame, removingonly the knurl. The original is retained outside the repository. Only thetwo knob regions are imported; the frame itself is never replaced. Theiralpha comes from the original painting, preserving its exact silhouette.The tooth is cut from the original knurl and neutralised for soft-lightcompositing, so its lighting comes from the fixed body beneath it."""import importlib.utilimport pathlibimport sysfrom PIL import Image, ImageDraw, ImageOpsHERE = pathlib.Path(__file__).resolve().parentspec = importlib.util.spec_from_file_location('people', HERE / 'import-people-materials.py')people = importlib.util.module_from_spec(spec)spec.loader.exec_module(people)def main(source):    source = pathlib.Path(source).expanduser()    original = Image.open(people.CATALOGUE / 'rolodex-frame.imageset/rolodex-frame.png').convert('RGBA')    smooth = Image.open(source / 'smooth-frame-original.png').convert('RGBA').resize(original.size, Image.Resampling.LANCZOS)    w, h = original.size    layers = source / 'layers'    layers.mkdir(exist_ok=True)    for name, x in [('left', 0), ('right', .87)]:        box = (round(x * w), round(.289 * h), round((x + .13) * w), round(.589 * h))        body = original.crop(box)        # Keep the original side plates, rim, adjoining axle and cradle.        # Only the tread is inpainted. Its outline is measured in this crop.        outline = [(.29, .02), (.84, .02), (.82, .98), (.28, .98),                   (.12, .91), (.035, .73), (.035, .30), (.13, .10)]        if name == 'right':            outline = [(1 - px, py) for px, py in outline]        mask = Image.new('L', body.size)        ImageDraw.Draw(mask).polygon([(round(px * body.width), round(py * body.height)) for px, py in outline], fill=255)        fixed = Image.composite(smooth.crop(box), body, mask)        fixed.putalpha(body.getchannel('A'))        body = fixed        filename = f'rolodex-knob-{name}'        body.save(layers / f'{filename}.png')        body.save(people.imageset(filename, 'png'))    # One horizontal tooth across the left tread, away from either side rim.    tooth = ImageOps.grayscale(original.crop((27, 446, 100, 455)))    tooth = ImageOps.autocontrast(tooth).convert('RGBA')    tooth.save(layers / 'rolodex-knurl.png')    tooth.save(people.imageset('rolodex-knurl', 'png'))if __name__ == '__main__':    main(sys.argv[1] if len(sys.argv) > 1 else '~/lane-briefs/rolodex-knobs')