slice-imagegen-assets.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from pathlib import Path
  2. from PIL import Image
  3. ROOT = Path(__file__).resolve().parents[1]
  4. SRC = ROOT / "assets" / "resources" / "sprites" / "imagegen"
  5. OUT = SRC / "frames"
  6. def alpha_crop(tile, padding=6):
  7. alpha = tile.getchannel("A")
  8. bbox = alpha.getbbox()
  9. if not bbox:
  10. return tile
  11. left, top, right, bottom = bbox
  12. left = max(0, left - padding)
  13. top = max(0, top - padding)
  14. right = min(tile.width, right + padding)
  15. bottom = min(tile.height, bottom + padding)
  16. return tile.crop((left, top, right, bottom))
  17. def slice_row(file_name, names):
  18. image = Image.open(SRC / file_name).convert("RGBA")
  19. tile_w = image.width / len(names)
  20. OUT.mkdir(parents=True, exist_ok=True)
  21. for index, name in enumerate(names):
  22. left = round(index * tile_w)
  23. right = round((index + 1) * tile_w)
  24. tile = image.crop((left, 0, right, image.height))
  25. alpha_crop(tile).save(OUT / f"{name}.png")
  26. def slice_grid(file_name, names, cols, rows):
  27. image = Image.open(SRC / file_name).convert("RGBA")
  28. tile_w = image.width / cols
  29. tile_h = image.height / rows
  30. OUT.mkdir(parents=True, exist_ok=True)
  31. for index, name in enumerate(names):
  32. col = index % cols
  33. row = index // cols
  34. left = round(col * tile_w)
  35. top = round(row * tile_h)
  36. right = round((col + 1) * tile_w)
  37. bottom = round((row + 1) * tile_h)
  38. tile = image.crop((left, top, right, bottom))
  39. alpha_crop(tile, 4).save(OUT / f"{name}.png")
  40. slice_row("hero_spritesheet.png", [
  41. "hero_idle",
  42. "hero_walk_1",
  43. "hero_walk_2",
  44. "hero_walk_3",
  45. "hero_walk_4",
  46. "hero_jump_rise",
  47. "hero_jump_fall",
  48. "hero_landing",
  49. ])
  50. slice_row("enemies_spritesheet.png", [
  51. "spore_idle",
  52. "spore_walk_1",
  53. "spore_walk_2",
  54. "spore_hit",
  55. "wing_idle",
  56. "wing_flap_1",
  57. "wing_flap_2",
  58. "wing_hit",
  59. "turret_idle",
  60. "turret_aim",
  61. "turret_fire",
  62. "turret_damaged",
  63. ])
  64. slice_row("boss_star_hive_spritesheet.png", [
  65. "boss_idle_closed",
  66. "boss_idle_pulse",
  67. "boss_attack_charge",
  68. "boss_tentacle_sweep",
  69. "boss_damaged",
  70. "boss_core_enraged",
  71. ])
  72. slice_grid("projectiles_spritesheet.png", [
  73. "rifle_1",
  74. "rifle_2",
  75. "rifle_3",
  76. "rifle_4",
  77. "flame_1",
  78. "flame_2",
  79. "flame_3",
  80. "flame_4",
  81. "rail_1",
  82. "rail_2",
  83. "acid_1",
  84. "acid_2",
  85. "acid_3",
  86. "acid_4",
  87. "impact_1",
  88. "impact_2",
  89. "impact_3",
  90. "impact_4",
  91. "muzzle_1",
  92. "muzzle_2",
  93. ], 10, 2)
  94. for path in sorted(OUT.glob("*.png")):
  95. with Image.open(path) as image:
  96. print(f"{path.name}: {image.width}x{image.height}")