"""Post-process raw gpt-image UI art into Assets/Art/UI. - ensures real alpha (removes flat background from corners if needed) - trims transparent margins, pads to square for icons, resizes - writes a contact sheet to Tools/ui/contact.png for review """ import glob import os import sys from PIL import Image, ImageChops, ImageDraw, ImageFilter HERE = os.path.dirname(os.path.abspath(__file__)) RAW = os.path.join(HERE, "raw") DST = os.path.join(os.path.dirname(os.path.dirname(HERE)), "Assets", "Art", "UI") def remove_bg(im, tol=38): """Flood-fill background from the four corners into transparency.""" im = im.convert("RGBA") w, h = im.size mask = Image.new("L", (w, h), 0) rgb = im.convert("RGB") for corner in [(0, 0), (w - 1, 0), (0, h - 1), (w - 1, h - 1)]: tmp = rgb.copy() ImageDraw.floodfill(tmp, corner, (255, 0, 255), thresh=tol) diff = ImageChops.difference(tmp, rgb).convert("L").point(lambda v: 255 if v > 0 else 0) mask = ImageChops.lighter(mask, diff) alpha = ImageChops.invert(mask).filter(ImageFilter.MinFilter(3)).filter(ImageFilter.GaussianBlur(1.2)) im.putalpha(ImageChops.multiply(alpha, im.getchannel("A"))) return im def has_alpha(im): return im.mode == "RGBA" and im.getchannel("A").getextrema()[0] < 10 def trim(im, pad_frac=0.04, square=True): bbox = im.getchannel("A").point(lambda v: 255 if v > 8 else 0).getbbox() if bbox: im = im.crop(bbox) w, h = im.size side = max(w, h) if square else None pad = int(max(w, h) * pad_frac) if square: canvas = Image.new("RGBA", (side + 2 * pad, side + 2 * pad), (0, 0, 0, 0)) canvas.paste(im, ((side - w) // 2 + pad, (side - h) // 2 + pad)) else: canvas = Image.new("RGBA", (w + 2 * pad, h + 2 * pad), (0, 0, 0, 0)) canvas.paste(im, (pad, pad)) return canvas def process(name): src = os.path.join(RAW, name + ".png") im = Image.open(src) if name == "menu_bg": im = im.convert("RGB") im.save(os.path.join(DST, name + ".png"), optimize=True) return im.convert("RGBA") if not has_alpha(im): im = remove_bg(im) if name.startswith("logo"): im = trim(im, 0.02, square=False) if im.width > 1600: im = im.resize((1600, round(im.height * 1600 / im.width)), Image.LANCZOS) else: im = trim(im, 0.04).resize((256, 256), Image.LANCZOS) im.save(os.path.join(DST, name + ".png"), optimize=True) return im def contact(images, out): cell = 200 cols = 6 rows = (len(images) + cols - 1) // cols sheet = Image.new("RGBA", (cols * cell, rows * (cell + 20)), (255, 255, 255, 255)) checker = Image.new("RGBA", (cell, cell)) d = ImageDraw.Draw(checker) for y in range(0, cell, 16): for x in range(0, cell, 16): d.rectangle([x, y, x + 15, y + 15], fill=(200, 200, 200, 255) if (x + y) // 16 % 2 else (240, 240, 240, 255)) dd = ImageDraw.Draw(sheet) for i, (n, im) in enumerate(images): x, y = (i % cols) * cell, (i // cols) * (cell + 20) th = im.copy() th.thumbnail((cell, cell)) bg = checker.copy() bg.alpha_composite(th, ((cell - th.width) // 2, (cell - th.height) // 2)) sheet.paste(bg, (x, y)) dd.text((x + 4, y + cell + 3), n, fill=(0, 0, 0, 255)) sheet.save(out) if __name__ == "__main__": os.makedirs(DST, exist_ok=True) names = sys.argv[1:] or sorted(os.path.splitext(os.path.basename(p))[0] for p in glob.glob(os.path.join(RAW, "*.png"))) done = [(n, process(n)) for n in names] contact(done, os.path.join(HERE, "contact.png")) print("processed", len(done))