Source

AncientDocumentEditor / Import scripts / convert.py

import re
import os, sys, getopt
import Image
import argparse
from xml.dom.minidom import parse

###############################################################################
# Font importer
###############################################################################
#
# only for fonts from PaRADIIT Project
#     https://sites.google.com/site/paradiitproject/results 
#
# usage : (in the repertory of the unzipped archive from the previous url)
#
# python convert.py -name=fontName
# 
# => will use fontName as font name, export each letter as fontName_letter_version.bmp
# => will produce images into a fontName/ repertory inside the current rep.




# create thumb + rename it
def doJob( fontName = "font" ):
    
    # create out rep
    outRep = fontName + os.sep
    try:
        os.mkdir(outRep)
    except OSError:
        pass

    for bwFile in os.listdir('.'):
        if re.match(r'.*bw\.bmp', bwFile):
            #print bwFile

            coloFile = bwFile.rstrip('_bw.bmp')
            coloFile += '.bmp'

            print os.path.exists(coloFile);

            bwIm = Image.open(bwFile)
            coloIm = Image.open(coloFile)

            # convert to greyscale
            gIm = coloIm.convert("L")
            greyFile = bwFile.rstrip('_bw.bmp')
            greyFile += '_gray.bmp'

            # save grey image
            #gIm.save(greyFile)


            # go go write new img
            print bwIm.size == gIm.size

            outIm = Image.new("L", bwIm.size, "white")

            bwPix = bwIm.load()
            gPix = gIm.load()
            outPix = outIm.load()

            for y in xrange(bwIm.size[0]):
                for x in xrange(bwIm.size[1]):
                    if bwPix[y, x] == 0:            # if mask is black
                        outPix[y, x] = gPix[y, x];  # use grey level image

            # setup character name
            xmlFile = coloFile = bwFile.rstrip('_bw.bmp')
            xmlFile += '.xml'
            xmlDom = parse(xmlFile);
            charName = xmlDom.getElementsByTagName('Transcription')[0].getAttribute('Unicode')
            charName = charName.replace('M+', 'U+')
            
            print charName
            
            outFile = unicode(bwFile, 'utf8')
            split = outFile.split('_')
            mo = re.match(r'.*(\d+)$', split[0])
            
            if mo != None : 
                version = mo.group(1)
                outFileCustom = unicode(outRep + fontName +'_'+ charName +'_'+ version + ".bmp")

                #print outFileCustom

                outIm.save(outFileCustom, "BMP")

                print 'done'
    return "well done"


##############################################################################

# main
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'n:', ['name='])
    except getopt.GetoptError, err:
        print str(err) # will print something like "option -a not recognized"
        sys.exit(2)

    for o, a in opts:
        if o in ('-n', '--name'):
            print "you got -n arg"
            doJob(a)
        else:
            print "no args"
            doJob()

if __name__ == "__main__":
    main()