from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "assets" / "resources" / "sprites" / "imagegen" OUT = SRC / "frames" def alpha_crop(tile, padding=6): 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_row(file_name, names): image = Image.open(SRC / file_name).convert("RGBA") tile_w = image.width / len(names) OUT.mkdir(parents=True, exist_ok=True) for index, name in enumerate(names): left = round(index * tile_w) right = round((index + 1) * tile_w) tile = image.crop((left, 0, right, image.height)) alpha_crop(tile).save(OUT / f"{name}.png") def slice_grid(file_name, names, cols, rows): image = Image.open(SRC / file_name).convert("RGBA") tile_w = image.width / cols tile_h = image.height / rows OUT.mkdir(parents=True, exist_ok=True) 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, 4).save(OUT / f"{name}.png") slice_row("hero_spritesheet.png", [ "hero_idle", "hero_walk_1", "hero_walk_2", "hero_walk_3", "hero_walk_4", "hero_jump_rise", "hero_jump_fall", "hero_landing", ]) slice_row("enemies_spritesheet.png", [ "spore_idle", "spore_walk_1", "spore_walk_2", "spore_hit", "wing_idle", "wing_flap_1", "wing_flap_2", "wing_hit", "turret_idle", "turret_aim", "turret_fire", "turret_damaged", ]) slice_row("boss_star_hive_spritesheet.png", [ "boss_idle_closed", "boss_idle_pulse", "boss_attack_charge", "boss_tentacle_sweep", "boss_damaged", "boss_core_enraged", ]) slice_grid("projectiles_spritesheet.png", [ "rifle_1", "rifle_2", "rifle_3", "rifle_4", "flame_1", "flame_2", "flame_3", "flame_4", "rail_1", "rail_2", "acid_1", "acid_2", "acid_3", "acid_4", "impact_1", "impact_2", "impact_3", "impact_4", "muzzle_1", "muzzle_2", ], 10, 2) for path in sorted(OUT.glob("*.png")): with Image.open(path) as image: print(f"{path.name}: {image.width}x{image.height}")