sprite.py 740 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. import sys, getopt, os
  3. from PIL import Image
  4. def getvalue(i, im):
  5. r, g, b = im[i]
  6. color = (int(r * 31 / 255) << 11) + (int(g * 63 / 255) << 5) + (int(b * 31 / 255))
  7. return "0x" + format(color, "04X")
  8. file = sys.argv[1]
  9. im = Image.open(file)
  10. width, height = im.size
  11. im = list(im.convert("RGB").getdata())
  12. sprite = "unsigned short " + os.path.splitext(file)[0] + "[] =\n{\n"
  13. sprite += "\t" + str(width) + ", " + str(height) + ", "
  14. sprite += getvalue(0, im)
  15. sprite += ",\n\t"
  16. for i in range(width * height):
  17. sprite += getvalue(i, im)
  18. if i != width * height - 1:
  19. if (i + 1) % 10 == 0:
  20. sprite += ",\n\t"
  21. else:
  22. sprite += ", "
  23. sprite += "\n};"
  24. print(sprite)