font-split.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import PIL
  5. from PIL import Image
  6. def glyphsplit(filename, chars):
  7. print("processing {}".format(filename))
  8. img = Image.open(filename)
  9. glyphs = []
  10. cw = 80
  11. ch = 88
  12. cols = img.size[0] // cw
  13. rows = img.size[1] // ch
  14. print(len(chars), cols * rows)
  15. assert len(chars) == cols * rows
  16. for y in range(0, rows):
  17. for x in range(0, cols):
  18. glyph_idx = x + cols * y
  19. if chars[glyph_idx] == " ":
  20. continue
  21. glyph = img.crop((x * cw,
  22. y * ch,
  23. x * cw + cw,
  24. y * ch + ch))
  25. out_filename = "{:04x}.png".format(ord(chars[glyph_idx]))
  26. out_filename = os.path.join("glyphs", out_filename)
  27. print("saving {}".format(out_filename))
  28. assert not os.path.exists(out_filename)
  29. glyph.save(out_filename)
  30. glyphsplit("font-game.png",
  31. "\0!\"#$%&'()*+,-./"
  32. "0123456789:;<=>?"
  33. "@ABCDEFGHIJKLMNO"
  34. "PQRSTUVWXYZ[\\]^_"
  35. "`abcdefghijklmno"
  36. "pqrstuvwxyz{|}~ "
  37. " ¡¢£¤¥¦§¨©ª«¬ ®¯"
  38. "°±²³´µ¶·¸¹º»¼½¾¿"
  39. "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ"
  40. "ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß"
  41. "àáâãäåæçèéêëìíîï"
  42. "ðñòóôõö÷øùúûüýþÿ"
  43. "ĀāĂ㥹ĆćĈĉĊċČčĎď"
  44. "ĐđĒēĔĕĖėĘęĚěĜĝĞğ"
  45. "ĠġĢģĤĥĦħĨĩĪīĬĭĮį"
  46. "İıIJijĴĵĶķĸĹĺĻļĽľĿ"
  47. "ŀŁłŃńŅņŇňʼnŊŋŌōŎŏ"
  48. "ŐőŒœŔŕŖŗŘřŚśŜŝŞş"
  49. "ŠšŢţŤťŦŧŨũŪūŬŭŮů"
  50. "ŰűŲųŴŵŶŷŸŹźŻżŽžſ"
  51. "…ắầặảạếệẻịỉốộồơớ"
  52. "ởợụủưựửứừữọấểềỏậ")
  53. glyphsplit("font-cyr.png",
  54. "ӘЁӨҢЄ ІЇ ЎҺҖҮ "
  55. "АБВГДЕЖЗИЙКЛМНОП"
  56. "РСТУФХЦЧШЩЪЫЬЭЮЯ"
  57. "абвгдежзийклмноп"
  58. "рстуфхцчшщъыьэюя"
  59. " ё є ії ўһүәҗөң")
  60. glyphsplit("font-greek.png",
  61. "ΑΒΓΔΕΖΗΙΚΛΜΝΞΟΠΡ"
  62. "ΣΤΥΦΧΨΩΉΈΆ "
  63. "αβγδεζηικλμνξοπρ"
  64. "ςστυφχψω "
  65. "Θθ "
  66. "άέίύώήό ")
  67. # EOF #