from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "assets" / "resources" / "sprites" / "imagegen" / "level_redesign" OUT = SRC / "ui" def alpha_crop(tile, padding=8): bbox = tile.getchannel("A").getbbox() if not bbox: return tile left, top, right, bottom = bbox return tile.crop(( max(0, left - padding), max(0, top - padding), min(tile.width, right + padding), min(tile.height, bottom + padding), )) image = Image.open(SRC / "hp_hud.png").convert("RGBA") OUT.mkdir(parents=True, exist_ok=True) names = [ "hp_bar_frame", "hp_segments", "hp_empty_segment", "hp_numeric_plaque", "damage_badge", "shield_icon", "low_health_marker", "hp_panel_alt", ] cols = 4 rows = 2 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).save(OUT / f"{name}.png") for path in sorted(OUT.glob("*.png")): with Image.open(path) as item: print(f"{path.name}: {item.width}x{item.height}")