#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image, ImageDraw, ImageFont
import bitarray
from bitarray import bitarray
import getopt
import sys
import re

def get_pix(image):
    pixel = image.load()
    width, height = image.size
    bitmap = bitarray()
    for h in range(height):
        for w in range(width):
            # if int(sum(pixel[w, h])) > (255 * 3 / 2):
            if pixel[w, h] > 0:
                bitmap.append(False)
            else:
                bitmap.append(True)
    return bitmap

def get_gb2312_pix(gb2312_code, w, h, usr_font):
    # image = Image.new("RGB", (w, h), (255, 255, 255))
    image = Image.new("1", (w, h), (1))
    d_usr = ImageDraw.Draw(image)
    try:
        unicode_code = gb2312_code.decode('gb2312')
        # d_usr.text((0, 0), unicode_code, (0,0,0), font=usr_font)
        d_usr.text((0, 0), unicode_code, (0), font=usr_font)
    except:
        # d_usr.text((0, 0), u" ", (0,0,0), font=usr_font)
        d_usr.text((0, 0), u" ", (0), font=usr_font)
    return get_pix(image)

def main():
    help = 'Usage: %s [option]' % sys.argv[0]
    help += '''\noption:
    -h | --help                                 display this information
    -i | --truetype-file TrueType-font-file     specify TrueType file
    -s | --size geometry                        width and height of font
    -o | --output output-dot-matrix-font        specify output file
    '''
    short_opts = 'hi:s:o:'
    opts = ['help', 'truetype-file=', 'size=', 'output=']
    try:
        opts, args = getopt.getopt(sys.argv[1:], short_opts, opts)
    except getopt.GetoptError as err:
        print(err)
        print(help)
        sys.exit(1)

    truetypefile = ''
    font_width = 16
    font_height = 16
    outfilename = 'dot_matrix.font'
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            print(help)
            sys.exit()
        elif opt in ('-i', '--truetype-file'):
            truetypefile = arg
        elif opt in ('-s', '--size'):
            fontsize = re.split(r'\D', arg)
            font_width = int(fontsize[0])
            font_height = int(fontsize[1])
        elif opt in ('-o', '--output'):
            outfilename = arg
        else:
            print(help)
            sys.exit(1)

    if not truetypefile:
        print(help)
        sys.exit(1)
    usr_font = ImageFont.truetype(truetypefile, font_height)
    with open(outfilename, 'wb') as outfile:
        for i in range(0xA1, 0xF8):
            for j in range(0xA1, 0xFF):
                data = get_gb2312_pix(chr(i) + chr(j), font_width, font_height, usr_font)
                data.tofile(outfile)

if __name__ == '__main__':
    main()

@@ -1084,16 +1084,32 @@
 %5Boption%5D
+ %3Ctruetype-file%3E
 ' %25 sys.
@@ -1218,78 +1218,8 @@
 ion%0A
-    -i %7C --truetype-file TrueType-font-file     specify TrueType file%0A
     
@@ -1355,16 +1355,122 @@
 ut file%0A
+example:%0A    gendotmatrix.py -o ubuntu-c.font %22/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf%22%0A
     '''%0A
@@ -1519,26 +1519,8 @@
 lp',
- 'truetype-file=',
  'si
@@ -1714,30 +1714,8 @@
 1)%0A%0A
-    truetypefile = ''%0A
     
@@ -1900,86 +1900,8 @@
 t()%0A
-        elif opt in ('-i', '--truetype-file'):%0A            truetypefile = arg%0A
     
@@ -2192,24 +2192,113 @@
 ys.exit(1)%0A%0A
+    if len(args) %3E 0:%0A        truetypefile = args%5B0%5D%0A    else:%0A        truetypefile = ''%0A
     if not t

Update usage