font2array.py 731 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Python tool to convert font image to C bit array.
  2. #
  3. # by drummyfish
  4. # released under CC0 1.0.
  5. from PIL import Image
  6. imageArray = []
  7. paletteColors = []
  8. paletteArray = []
  9. image = Image.open("font.png").convert("RGB")
  10. pixels = image.load()
  11. column = 0
  12. value = 0
  13. index = 0
  14. result = ""
  15. chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,!?0123456789/-+()%"
  16. for x in range(image.size[0]):
  17. if column == 4:
  18. column = 0
  19. value = 0
  20. index += 1
  21. continue
  22. for y in range(image.size[1]):
  23. value = value * 2 + (1 if pixels[x,y][0] < 128 else 0)
  24. if column == 3:
  25. result += " 0x" + hex(value)[2:].zfill(4) + ", // " + str(index) + " \"" + chars[index] + "\"\n"
  26. column += 1
  27. result = "{\n" + result + "}";
  28. print(result)