1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- from PIL import Image
- import argparse
- import tempfile
- def fix_font_file(filename, glyph_width, glyph_height):
- print("Processing %s %dx%d" % (filename, glyph_width, glyph_height))
- img = Image.open(filename)
- w, h = img.size
- print("Image size: %dx%d" % (w, h))
- assert w % glyph_width == 0, "image not multiple of glyph width"
- assert h % glyph_height == 0, "image not multiple of glyph height"
- w_g = w // glyph_width
- h_g = h // glyph_height
- print("Glyphs: %ax%a" % (w_g, h_g))
- out = Image.new("RGBA", (w_g * (glyph_width + 2), h_g * (glyph_height + 2)), color=5)
- for y in range(0, h_g):
- for x in range(0, w_g):
- ix = x * glyph_width
- iy = y * glyph_height
- ox = x * (glyph_width + 2) + 1
- oy = y * (glyph_height + 2) + 1
- glyph = img.crop((ix, iy, ix + glyph_width, iy + glyph_height))
- out.paste(glyph, (ox, oy))
- with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
- out.save(f)
- print("File saved as %s" % f.name)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='rFactor MAS packer')
- parser.add_argument('FILE', action='store', type=str,
- help='font image to change')
- parser.add_argument('GLYPH_WIDTH', action='store', type=int,
- help='glyph width')
- parser.add_argument('GLYPH_HEIGHT', action='store', type=int,
- help='glyph height')
- args = parser.parse_args()
- fix_font_file(args.FILE, args.GLYPH_WIDTH, args.GLYPH_HEIGHT)
|