I wrote a Python script for drawing a character from a font, exporting, and sending it to a CGI interface for displaying. However, it isn't working. The code is as following:
#!/usr/bin/python
# coding: UTF-8
def getStringImage(text, font, size=40, color=[0,0,0]):
import Image, ImageDraw, ImageFont
img = Image.new("RGBA", (size, size), "white")
draw = ImageDraw.Draw( img )
draw.ink = color[0] + color[1]*256 + color[2]*256*256
font = ImageFont.truetype(font, size)
draw.text( (0,0), text, font=font )
data = img.getdata()
newData = list()
for item in data:
if (item[0], item[1], item[2]) == (255, 255, 255):
newData.append((255, 255, 255, 0))
else:
newData.append((item[0], item[1], item[2], 255))
img.putdata(newData)
return img
text = "" # U+E010
font = "hzcdp01b.ttf"
size = 100
color = [0, 0, 0]
img = getStringImage( text.decode('UTF-8'), font, size, color )
print 'Content-Type: image/png'
print 'Content-Disposition: inline; filename="image.png"'
print
import cStringIO
f = cStringIO.StringIO()
img.save(f, "png")
f.seek(0)
print f.read()
The font used above is extracted from this system: http://j.mp/14y6MAL, and here is the single backup file: http://j.mp/122PGLe
It works fine if another fonts is used (for example "mingliu.ttc" with char "人").
Make sure that the character you want to display is actually mapped to a glyph. When looking at the hzcdp01b.ttf
, it doesn't seem to have a character map table:
> fc-query hzcdp01b.ttf
Pattern has 15 elts (size 16)
family: "hzcdp01b"(s)
slant: 0(i)(s)
weight: 80(i)(s)
width: 100(i)(s)
spacing: 100(i)(s)
foundry: "unknown"(s)
file: "hzcdp01b.ttf"(s)
index: 0(i)(s)
outline: FcTrue(s)
scalable: FcTrue(s)
charset:
(s)
lang: (s)
fontversion: 69632(i)(s)
fontformat: "TrueType"(s)
decorative: FcFalse(s)