Download tools/make_bench_refs.py from SharpMeow/unroll: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/spaces/SharpMeow/unroll/resolve/main/tools/make_bench_refs.py
- Command line
-
hf download hf://spaces/SharpMeow/unroll/tools/make_bench_refs.py
-
curl -L -o make_bench_refs.py https://huggingface.co/spaces/SharpMeow/unroll/resolve/main/tools/make_bench_refs.py
2 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Build the Ink Bench reference layers (data/bench/) from the page's own data. | |
| Each winding gets <id>_ink.png (published ink preview) and <id>_ct.png (X-ray layer | |
| cropped from the atlas), resampled to the same grid, long side LONG px. Windings with our | |
| ink_9um run also get <id>_gpu.png on that grid. Lossless 8-bit gray PNG, so the browser | |
| and tools/inkbench.py read bit-identical pixels. | |
| """ | |
| import hashlib, json, os | |
| from PIL import Image | |
| LONG = 640 | |
| root = os.path.join(os.path.dirname(__file__), '..') | |
| d = json.load(open(os.path.join(root, 'data/scroll.json'))) | |
| gpu = json.load(open(os.path.join(root, 'data/gpu.json')))['windings'] | |
| ct = Image.open(os.path.join(root, 'data/atlas_ct.jpg')).convert('L') | |
| out = {'schema': 'unroll-bench-refs/1', 'scroll': d['scroll'], 'long': LONG, 'segments': {}} | |
| def save(im, name): | |
| p = os.path.join(root, 'data/bench', name); im.save(p, optimize=True) | |
| return {'file': 'data/bench/' + name, 'sha256': hashlib.sha256(open(p, 'rb').read()).hexdigest()} | |
| for w in d['windings']: | |
| hi = Image.open(os.path.join(root, 'data', w['hi'])).convert('L') | |
| s = LONG / max(hi.size); W, H = round(hi.size[0] * s), round(hi.size[1] * s) | |
| e = {'width': W, 'height': H, 'widthMM': w['widthMM'], 'heightMM': w['heightMM'], | |
| 'pxPerMM': round(W / w['widthMM'], 4), 'layers': {}} | |
| e['layers']['ink'] = save(hi.resize((W, H), Image.LANCZOS), f"{w['id']}_ink.png") | |
| u0, v0, u1, v1 = w['atlas'] | |
| c = ct.crop((round(u0 * 4096), round(v0 * 4096), round(u1 * 4096), round(v1 * 4096))) | |
| e['layers']['ct'] = save(c.resize((W, H), Image.LANCZOS), f"{w['id']}_ct.png") | |
| if w['id'] in gpu: | |
| g = Image.open(os.path.join(root, 'data', gpu[w['id']]['hi'])).convert('L') | |
| e['layers']['gpu'] = save(g.resize((W, H), Image.LANCZOS), f"{w['id']}_gpu.png") | |
| out['segments'][w['id']] = e | |
| json.dump(out, open(os.path.join(root, 'data/bench/index.json'), 'w'), indent=1) | |
| print(len(out['segments']), 'segments') | |