entries
by year
by tag
This is an entry posted to my blog on November 5, 2008.
Tibetan Unicode to JPEG with Python
from reportlab.pdfbase import pdfmetrics, ttfonts, _fontdata
from reportlab.graphics import shapes, renderPM
from reportlab.lib import colors
font = ttfonts.TTFont('TibMachUni-1.901b', 'TibMachUni-1.901b.ttf')
pdfmetrics.registerFont(font)
font.encoding.vector = _fontdata.encodings['StandardEncoding']
shapes.STATE_DEFAULTS['fontName'] = 'TibMachUni-1.901b'
import xml.dom.minidom
doc = xml.dom.minidom.parse("x3.xml")
path = 'x/card_'
number = 1
one = '_side1.jpg'
two = '_side2.jpg'
n = 36
fontsize = 36
colour = colors.lawngreen
bg_colour = colors.black
for row in doc.getElementsByTagName("Row"):
try:
cell1 = row.childNodes[0].childNodes[0].childNodes[0].nodeValue
cell2 = row.childNodes[1].childNodes[0].childNodes[0].nodeValue
cell1_list = cell1.split(u'\u0F0B')
cell1_a = u'\u0F0B'.join(cell1_list[0:5])
cell1_b = u'\u0F0B'.join(cell1_list[5:10])
cell1_c = u'\u0F0B'.join(cell1_list[10:15])
d1 = shapes.Drawing(800, 600)
d1.add(shapes.Rect(0, 0, 800, 600, fillColor=bg_colour))
d1.add(shapes.String(4, 450, cell1_a, fontSize=130, fillColor=colour))
d1.add(shapes.String(4, 265, cell1_b, fontSize=130, fillColor=colour))
d1.add(shapes.String(4, 80, cell1_c, fontSize=130, fillColor=colour))
renderPM.drawToFile(d1, (path+str(number)+one), 'JPG')
d2 = shapes.Drawing(800, 600)
d2.add(shapes.Rect(0, 0, 800, 600, fillColor=bg_colour))
d2.add(shapes.String(4, 500, cell2[0:n], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 450, cell2[n:n*2], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 400, cell2[n*2:n*3], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 350, cell2[n*3:n*4], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 300, cell2[n*4:n*5], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 250, cell2[n*5:n*6], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 200, cell2[n*6:n*7], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 150, cell2[n*7:n*8], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 100, cell2[n*8:n*9], fontSize=fontsize, fillColor=colour))
d2.add(shapes.String(4, 50, cell2[n*9:n*10], fontSize=fontsize, fillColor=colour))
renderPM.drawToFile(d2, (path+str(number)+two), 'JPG')
number +=1
except:
pass
Comments, questions to - writepython at gmail dot com
Tags for this entry: flashcards ipod jpeg jpg python tibetan tibetan-flashcards