from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "assets" / "resources" / "sprites" / "imagegen" / "ui_scene" def alpha_crop(tile, padding=8): alpha = tile.getchannel("A") bbox = alpha.getbbox() if not bbox: return tile left, top, right, bottom = bbox left = max(0, left - padding) top = max(0, top - padding) right = min(tile.width, right + padding) bottom = min(tile.height, bottom + padding) return tile.crop((left, top, right, bottom)) def slice_grid(source, output_dir, names, cols, rows, padding=8): image = Image.open(SRC / source).convert("RGBA") out = SRC / output_dir out.mkdir(parents=True, exist_ok=True) tile_w = image.width / cols tile_h = image.height / rows for index, name in enumerate(names): col = index % cols row = index // cols left = round(col * tile_w) top = round(row * tile_h) right = round((col + 1) * tile_w) bottom = round((row + 1) * tile_h) tile = image.crop((left, top, right, bottom)) alpha_crop(tile, padding).save(out / f"{name}.png") slice_grid("foreground_props.png", "props", [ "ground_tile_a", "ground_tile_b", "steel_platform_a", "steel_platform_b", "alien_plant_a", "alien_plant_b", "rock_cluster_a", "rock_cluster_b", "warning_crate", "broken_machine", "grass_tuft_a", "grass_tuft_b", "glow_spore_a", "glow_spore_b", "pipe_debris", "small_console", ], 4, 4, 8) slice_grid("hud_ui.png", "ui", [ "hp_capsule", "hp_empty", "weapon_rifle", "weapon_flame", "weapon_rail", "score_panel", "boss_bar", "pause_button", "retry_button", "mission_clear_badge", "warning_marker", "damage_corner_a", "damage_corner_b", "damage_corner_c", "damage_corner_d", "ui_panel_a", "ui_panel_b", "ui_panel_c", "ui_panel_d", "ui_panel_e", ], 5, 4, 6) for folder in ("props", "ui"): for path in sorted((SRC / folder).glob("*.png")): with Image.open(path) as image: print(f"{folder}/{path.name}: {image.width}x{image.height}")