from PIL import Image import sys import os SEPARATOR_COLOR = (255,0,255, 255) NO_COL = (0, 0, 0, 0) def chk_vline_empty(img, x): for y in range(img.size[1]): _, _, _, a = img.getpixel((x, y)) if a != 0: return False else: return True def make_sfont(img_filename, output_name = None): img = Image.open(img_filename) ow, oh = img.size xlist = [] cx = -1 for x in range(ow): if chk_vline_empty(img, x): if cx >= 0: xlist.append([cx, x - cx]) cx = -1 else: if cx < 0: cx = x if cx >= 0: xlist.append([cx, ow - cx]) if len(xlist) != 94: print "Error: character length %d != 94" % len(xlist) return nw = 1 for _, w in xlist: nw += (w + 1) nimg = Image.new("RGBA", (nw, oh), NO_COL) nimg.putpixel((0, 0), SEPARATOR_COLOR) cx = 1 for x, w in xlist: box = (x, 0, x + w, oh) nbox = (cx, 0, cx + w, oh) nimg.paste(img.crop(box), nbox) nimg.putpixel((cx + w, 0), SEPARATOR_COLOR) cx += (w + 1) if output_name is None: base, name = os.path.split(img_filename) name, ext = os.path.splitext(name) name = name + '_sfont' + ext name = os.path.join(base, name) else: name = output_name nimg.save(name) print 'SFont written to %s' % name if __name__ == '__main__': args = len(sys.argv) if args == 1 or args > 3 : print 'Usage: make_sfont_wt.py IN.png OUT.png' elif args == 3: make_sfont(sys.argv[1], sys.argv[2]) else: make_sfont(sys.argv[1])