slice-ui-scene-assets.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from pathlib import Path
  2. from PIL import Image
  3. ROOT = Path(__file__).resolve().parents[1]
  4. SRC = ROOT / "assets" / "resources" / "sprites" / "imagegen" / "ui_scene"
  5. def alpha_crop(tile, padding=8):
  6. alpha = tile.getchannel("A")
  7. bbox = alpha.getbbox()
  8. if not bbox:
  9. return tile
  10. left, top, right, bottom = bbox
  11. left = max(0, left - padding)
  12. top = max(0, top - padding)
  13. right = min(tile.width, right + padding)
  14. bottom = min(tile.height, bottom + padding)
  15. return tile.crop((left, top, right, bottom))
  16. def slice_grid(source, output_dir, names, cols, rows, padding=8):
  17. image = Image.open(SRC / source).convert("RGBA")
  18. out = SRC / output_dir
  19. out.mkdir(parents=True, exist_ok=True)
  20. tile_w = image.width / cols
  21. tile_h = image.height / rows
  22. for index, name in enumerate(names):
  23. col = index % cols
  24. row = index // cols
  25. left = round(col * tile_w)
  26. top = round(row * tile_h)
  27. right = round((col + 1) * tile_w)
  28. bottom = round((row + 1) * tile_h)
  29. tile = image.crop((left, top, right, bottom))
  30. alpha_crop(tile, padding).save(out / f"{name}.png")
  31. slice_grid("foreground_props.png", "props", [
  32. "ground_tile_a",
  33. "ground_tile_b",
  34. "steel_platform_a",
  35. "steel_platform_b",
  36. "alien_plant_a",
  37. "alien_plant_b",
  38. "rock_cluster_a",
  39. "rock_cluster_b",
  40. "warning_crate",
  41. "broken_machine",
  42. "grass_tuft_a",
  43. "grass_tuft_b",
  44. "glow_spore_a",
  45. "glow_spore_b",
  46. "pipe_debris",
  47. "small_console",
  48. ], 4, 4, 8)
  49. slice_grid("hud_ui.png", "ui", [
  50. "hp_capsule",
  51. "hp_empty",
  52. "weapon_rifle",
  53. "weapon_flame",
  54. "weapon_rail",
  55. "score_panel",
  56. "boss_bar",
  57. "pause_button",
  58. "retry_button",
  59. "mission_clear_badge",
  60. "warning_marker",
  61. "damage_corner_a",
  62. "damage_corner_b",
  63. "damage_corner_c",
  64. "damage_corner_d",
  65. "ui_panel_a",
  66. "ui_panel_b",
  67. "ui_panel_c",
  68. "ui_panel_d",
  69. "ui_panel_e",
  70. ], 5, 4, 6)
  71. for folder in ("props", "ui"):
  72. for path in sorted((SRC / folder).glob("*.png")):
  73. with Image.open(path) as image:
  74. print(f"{folder}/{path.name}: {image.width}x{image.height}")