"""Cut a painted icon tile out of its background: everything outside the rounded tile becomes transparent, then the icon and the studio's small mark are written. blender -b --factory-startup --python tools/brand_mask.py -- art/brand/masters/wireframe.png Run through Blender for its image I/O and numpy (the system Python has neither Pillow nor numpy). The tile is found from the master itself: its edge is where the pixels stop matching the flat background in the corners. Writes media/icon.png (1024 px, the source for `bun run icons`) and src/app/assets/mark.png (256 px). """ import os import sys import bpy import numpy as np ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) src = sys.argv[sys.argv.index('--') + 1] image = bpy.data.images.load(os.path.join(ROOT, src)) w, h = image.size px = np.array(image.pixels[:], dtype=np.float32).reshape(h, w, 4) # The flat background, sampled in the corners, and how far each pixel is from it. corner = np.concatenate([px[:24, :24, :3].reshape(-1, 3), px[-24:, -24:, :3].reshape(-1, 3)]).mean(axis=0) diff = np.abs(px[:, :, :3] - corner).max(axis=2) inside = diff > 0.06 rows, cols = np.where(inside) # Trim stray specks: the tile spans the central band of rows and columns. y0, y1 = np.percentile(rows, 0.2), np.percentile(rows, 99.8) x0, x1 = np.percentile(cols, 0.2), np.percentile(cols, 99.8) radius = 0.13 * min(x1 - x0, y1 - y0) print(f'tile x {x0:.0f}..{x1:.0f} y {y0:.0f}..{y1:.0f} radius {radius:.0f}') # A rounded-rectangle alpha with a one-pixel soft edge. yy, xx = np.mgrid[0:h, 0:w].astype(np.float32) + 0.5 cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 hx, hy = (x1 - x0) / 2, (y1 - y0) / 2 qx = np.maximum(np.abs(xx - cx) - (hx - radius), 0) qy = np.maximum(np.abs(yy - cy) - (hy - radius), 0) dist = np.sqrt(qx * qx + qy * qy) - radius px[:, :, 3] = np.clip(0.5 - dist, 0, 1) def write(path, size): out = bpy.data.images.new('out', w, h, alpha=True) out.pixels.foreach_set(px.ravel()) if size != w: out.scale(size, size) out.filepath_raw = os.path.join(ROOT, path) out.file_format = 'PNG' os.makedirs(os.path.dirname(out.filepath_raw), exist_ok=True) out.save() print('wrote', path) write('media/icon.png', 1024) write('src/app/assets/mark.png', 256)