tile2colors.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from PIL import Image
  2. from math import sqrt
  3. def rgb_to_hsl(r, g, b):
  4. r = float(r)
  5. g = float(g)
  6. b = float(b)
  7. high = max(r, g, b)
  8. low = min(r, g, b)
  9. h, s, v = ((high + low) / 2,)*3
  10. return v
  11. def createPalette(color):
  12. if color[3] == 0:
  13. return "00000000"
  14. else:
  15. R3 = int(round(color[0] * 7 / 255))
  16. G3 = int(round(color[1] * 7 / 255))
  17. B2 = int(round( (color[2]-148)**3 * (1*9**(-6)) + 1.4 )) #color[2] * 3 / 255
  18. if B2 < 0:
  19. B2 = 0
  20. if B2 > 3:
  21. B2 = 3
  22. colorString = format(R3, '03b') + format(G3, '03b') + format(B2, '02b')
  23. return colorString
  24. #img: image to process
  25. #x: starting x position
  26. #y: starting y position
  27. #idx: index of pattern table, corresponding to index of tiles
  28. def createPattern(img, x, y, idx):
  29. tile = []
  30. for a in range(8):
  31. tileLine = []
  32. for b in range(8):
  33. pixel = px[x*8+b, y*8+a]
  34. tileLine.append(pixel)
  35. tile.append(tileLine)
  36. colorList = []
  37. for a in range(8):
  38. for b in range(8):
  39. if tile[a][b] not in colorList:
  40. colorList.append(tile[a][b])
  41. #order color list from dark to light
  42. colorList.sort(key=lambda x: rgb_to_hsl(x[0], x[1], x[2]) )
  43. if len(colorList) > 4:
  44. print("tile ", idx, " has more than 4 colors")
  45. paletteList = []
  46. for i in range(4):
  47. if i < len(colorList):
  48. paletteList.append(createPalette(colorList[i]))
  49. paletteString = "".join(paletteList)
  50. paletteString = paletteString.ljust(32, "0") + " ;" + str(idx)
  51. #swap second and third color intentionally
  52. swappedPaletteString = paletteString[0:8] + paletteString[16:24] + paletteString[8:16] + paletteString[24:]
  53. print(swappedPaletteString)
  54. im = Image.open('tiles.png')
  55. width, height = im.size
  56. px = im.load()
  57. #print(px[0,0])
  58. print("Width:", width, "px")
  59. print("Height:", height, "px")
  60. if (width%8 != 0):
  61. print("Width not dividable by 8")
  62. exit(1)
  63. if (height%8 != 0):
  64. print("Height not dividable by 8")
  65. exit(1)
  66. htiles = int(width / 8)
  67. vtiles = int(height / 8)
  68. tiles = htiles * vtiles
  69. print("Horizontal tiles:", htiles)
  70. print("Vertical tiles:", vtiles)
  71. print("Total number of tiles:", tiles)
  72. print("Index of tiles:")
  73. tile_i = 0
  74. for y in range(vtiles):
  75. for x in range(htiles):
  76. print (str(tile_i) + "\t", end = '')
  77. tile_i = tile_i + 1
  78. #function: create pattern of tile
  79. print()
  80. print()
  81. print()
  82. tile_i = 0
  83. for y in range(vtiles):
  84. for x in range(htiles):
  85. createPattern(px, x, y, tile_i)
  86. tile_i = tile_i + 1
  87. #createPattern(px, 0, 0, 0)