/Utilities/Production/fegba2lt_mapsprites.py

https://gitlab.com/Nick2001/lex-talionis
Python | 174 lines | 142 code | 27 blank | 5 comment | 15 complexity | 38dd880ebecc639a1a2d182ceafea986 MD5 | raw file
  1. # Converts FERepo Map Sprites to Lex Talionis Format
  2. import glob, difflib
  3. from PIL import Image
  4. gray_dict = {(88, 72, 120): (64, 64, 64),
  5. (144, 184, 232): (120, 120, 120),
  6. (216, 232, 240): (184, 184, 184),
  7. (112, 96, 96): (80, 80, 80),
  8. (176, 144, 88): (128, 128, 128),
  9. (248, 248, 208): (200, 200, 200),
  10. (56, 56, 144): (72, 72, 72),
  11. (56, 80, 224): (88, 88, 88),
  12. (40, 160, 248): (152, 152, 152),
  13. (24, 240, 248): (184, 184, 184),
  14. (232, 16, 24): (112, 112, 112),
  15. (248, 248, 64): (200, 200, 200),
  16. (248, 248, 248): (208, 208, 208)}
  17. enemy_dict = {(56, 56, 144): (96, 40, 32),
  18. (56, 80, 224): (168, 48, 40),
  19. (40, 160, 248): (224, 16, 16),
  20. (24, 240, 248): (248, 80, 72),
  21. (232, 16, 24): (56, 208, 48),
  22. (88, 72, 120): (104, 72, 96),
  23. (216, 232, 240): (224, 224, 224),
  24. (144, 184, 232): (192, 168, 184)}
  25. other_dict = {(56, 56, 144): (32, 80, 16),
  26. (56, 80, 224): (8, 144, 0),
  27. (40, 160, 248): (24, 208, 16),
  28. (24, 240, 248): (80, 248, 56),
  29. (232, 16, 24): (0, 120, 200),
  30. (88, 72, 120): (56, 80, 56),
  31. (144, 184, 232): (152, 200, 158),
  32. (216, 232, 240): (216, 248, 184),
  33. (112, 96, 96): (88, 88, 80),
  34. (176, 144, 88): (160, 136, 64),
  35. (248, 248, 208): (248, 248, 192),
  36. (248, 248, 64): (224, 248, 40)}
  37. enemy2_dict = {(56, 56, 144): (88, 32, 96),
  38. (56, 80, 224): (128, 48, 144),
  39. (40, 160, 248): (184, 72, 224),
  40. (24, 240, 248): (208, 96, 248),
  41. (232, 16, 24): (56, 208, 48),
  42. (88, 72, 120): (88, 64, 104),
  43. (144, 184, 232): (168, 168, 232),
  44. (64, 56, 56): (72, 40, 64)}
  45. def color_convert(newimg, d):
  46. width, height = newimg.size
  47. img = Image.new("RGB", (width, height), (128, 160, 128))
  48. # Change color
  49. for x in range(width):
  50. for y in range(height):
  51. current_color = newimg.getpixel((x, y))
  52. try:
  53. new_color = d[current_color]
  54. except KeyError:
  55. new_color = current_color
  56. img.putpixel((x, y), new_color)
  57. return img
  58. # Get images
  59. images = glob.glob('*.png')
  60. # Pair images
  61. images_dict = {}
  62. for image in images:
  63. if 'moving' in image.lower():
  64. standing_name = image.lower().replace('moving', 'standing')
  65. match = difflib.get_close_matches(standing_name, images, 1)
  66. if not match:
  67. print("Couldn't find a match for %s!" % image)
  68. continue
  69. else:
  70. match = match[0]
  71. shared_name = image.replace('moving', '').replace('Moving', '').replace('.png', '')
  72. images_dict[shared_name] = (match, image)
  73. for name, pair in images_dict.items():
  74. standing, moving = pair
  75. standing = Image.open(standing)
  76. mixed_width, mixed_height = standing.size
  77. moving = Image.open(moving)
  78. new_s = Image.new("RGB", (192, 144), (128, 160, 128))
  79. new_m = Image.new("RGB", (192, 160), (128, 160, 128))
  80. p1 = standing.crop((0, 0, mixed_width, mixed_height//3))
  81. p2 = standing.crop((0, mixed_height//3, mixed_width, 2*mixed_height//3))
  82. p3 = standing.crop((0, 2*mixed_height//3, mixed_width, 3*mixed_height//3))
  83. l1 = moving.crop((0, 0, 32, 32))
  84. l2 = moving.crop((0, 32, 32, 64))
  85. l3 = moving.crop((0, 64, 32, 96))
  86. l4 = moving.crop((0, 96, 32, 128))
  87. d1 = moving.crop((0, 32*4, 32, 32*5))
  88. d2 = moving.crop((0, 32*5, 32, 32*6))
  89. d3 = moving.crop((0, 32*6, 32, 32*7))
  90. d4 = moving.crop((0, 32*7, 32, 32*8))
  91. u1 = moving.crop((0, 32*8, 32, 32*9))
  92. u2 = moving.crop((0, 32*9, 32, 32*10))
  93. u3 = moving.crop((0, 32*10, 32, 32*11))
  94. u4 = moving.crop((0, 32*11, 32, 32*12))
  95. f1 = moving.crop((0, 32*12, 32, 32*13))
  96. f2 = moving.crop((0, 32*13, 32, 32*14))
  97. f3 = moving.crop((0, 32*14, 32, 32*15))
  98. if mixed_height//3 == 16:
  99. new_height = 24
  100. else:
  101. new_height = 8
  102. if mixed_width == 16:
  103. new_width = 24
  104. else:
  105. new_width = 16
  106. new_s.paste(p1, (new_width, new_height))
  107. new_s.paste(p2, (new_width+64, new_height))
  108. new_s.paste(p3, (new_width+64*2, new_height))
  109. new_s.paste(f1, (16, 8+96))
  110. new_s.paste(f2, (16+64, 8+96))
  111. new_s.paste(f3, (16+64*2, 8+96))
  112. new_m.paste(d1, (8, 8))
  113. new_m.paste(d2, (8+48, 8))
  114. new_m.paste(d3, (8+48*2, 8))
  115. new_m.paste(d4, (8+48*3, 8))
  116. new_m.paste(l1, (8, 48))
  117. new_m.paste(l2, (8+48, 48))
  118. new_m.paste(l3, (8+48*2, 48))
  119. new_m.paste(l4, (8+48*3, 48))
  120. new_m.paste(l1.transpose(Image.FLIP_LEFT_RIGHT), (8, 88))
  121. new_m.paste(l2.transpose(Image.FLIP_LEFT_RIGHT), (8+48, 88))
  122. new_m.paste(l3.transpose(Image.FLIP_LEFT_RIGHT), (8+48*2, 88))
  123. new_m.paste(l4.transpose(Image.FLIP_LEFT_RIGHT), (8+48*3, 88))
  124. new_m.paste(u1, (8, 128))
  125. new_m.paste(u2, (8+48, 128))
  126. new_m.paste(u3, (8+48*2, 128))
  127. new_m.paste(u4, (8+48*3, 128))
  128. # Make Passive Sprite
  129. width, height = new_s.size
  130. passive_sprites = new_s.crop((0, 0, width, height//3))
  131. gray_passive_sprites = color_convert(passive_sprites, gray_dict)
  132. new_s.paste(gray_passive_sprites, (0, height//3))
  133. new_name = name
  134. new_s.save("player" + new_name + ".png")
  135. new_m.save("player" + new_name + "_move.png")
  136. enemy_s = color_convert(new_s, enemy_dict)
  137. enemy_m = color_convert(new_m, enemy_dict)
  138. enemy_s.save("enemy" + new_name + ".png")
  139. enemy_m.save("enemy" + new_name + "_move.png")
  140. other_s = color_convert(new_s, other_dict)
  141. other_m = color_convert(new_m, other_dict)
  142. other_s.save("other" + new_name + '.png')
  143. other_m.save("other" + new_name + '_move.png')
  144. enemy2_s = color_convert(new_s, enemy2_dict)
  145. enemy2_m = color_convert(new_m, enemy2_dict)
  146. enemy2_s.save("enemy2" + new_name + '.png')
  147. enemy2_m.save("enemy2" + new_name + '_move.png')