font-add-border.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # SuperTux
  3. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from PIL import Image
  18. import os
  19. import argparse
  20. import tempfile
  21. # Add a 1 pixel border around every glyph in a font
  22. def fix_font_file(filename, glyph_width, glyph_height):
  23. print("Processing %s %dx%d" % (filename, glyph_width, glyph_height))
  24. img = Image.open(filename)
  25. w, h = img.size
  26. print("Image size: %dx%d" % (w, h))
  27. assert w % glyph_width == 0, "image not multiple of glyph width"
  28. assert h % glyph_height == 0, "image not multiple of glyph height"
  29. w_g = w // glyph_width
  30. h_g = h // glyph_height
  31. print("Glyphs: %ax%a" % (w_g, h_g))
  32. out = Image.new("RGBA", (w_g * (glyph_width + 2), h_g * (glyph_height + 2)), color=5)
  33. for y in range(0, h_g):
  34. for x in range(0, w_g):
  35. ix = x * glyph_width
  36. iy = y * glyph_height
  37. ox = x * (glyph_width + 2) + 1
  38. oy = y * (glyph_height + 2) + 1
  39. glyph = img.crop((ix, iy, ix + glyph_width, iy + glyph_height))
  40. out.paste(glyph, (ox, oy))
  41. with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
  42. out.save(f)
  43. print("File saved as %s" % f.name)
  44. if __name__ == "__main__":
  45. parser = argparse.ArgumentParser(description='rFactor MAS packer')
  46. parser.add_argument('FILE', action='store', type=str,
  47. help='font image to change')
  48. parser.add_argument('GLYPH_WIDTH', action='store', type=int,
  49. help='glyph width')
  50. parser.add_argument('GLYPH_HEIGHT', action='store', type=int,
  51. help='glyph height')
  52. args = parser.parse_args()
  53. fix_font_file(args.FILE, args.GLYPH_WIDTH, args.GLYPH_HEIGHT)
  54. # EOF #