view transformations/ttf2jpg.py @ 39:17caecc92544

affine transformation using PIL
author Razvan Pascanu <r.pascanu@gmail.com>
date Tue, 02 Feb 2010 21:17:11 -0500
parents 6d432a5010a2
children 05145f4fb609
line wrap: on
line source

#!/usr/bin/python                                                                                 
# -*- coding: iso-8859-1 -*-                                                                      

'''
    Implementation of font image generator
    download fonts from http://www.dafont.com for exemple

    Author: Guillaume Sicard
'''

import sys, os, fnmatch
import Image, ImageFont, ImageDraw, numpy

class ttf2jpg():
    def __init__(self, font_file = ''):
        self.w = 32
        self.h = 32
        self.font_dir = '/usr/share/fonts/truetype/ttf-liberation/'
        self.font_file = font_file
        self.image_dir = './images/'
        self.pattern = '*.ttf'

    # get font name
    def get_settings_names(self):
        return [self.font_file]

    # save an image
    def save_image(self,array, filename = ''):
        image = (array * 255.0).astype('int')
        image = Image.fromarray(image).convert('L')
        if (filename != ''):
            image.save(filename)
        else:
            image.show()

    # return a picture array of "text" with font "font_file"
    def create_image(self, text):
         # create a w x h black picture, and a drawing space
        image = Image.new('L', (self.w, self.h), 'Black')
        draw = ImageDraw.Draw(image)

        # load the font with the right size
        font = ImageFont.truetype(self.font_file, 28)
        d_w,d_h =  draw.textsize(text, font=font)

        # write text and aligns it
        draw.text(((32 - d_w) / 2, ((32 - d_h) / 2)), text, font=font, fill='White')

        image = numpy.asarray(image)
        image = (image / 255.0).astype(numpy.float32)

        return image

    # write all the letters and numbers into pictures
    def process_font(self):
        for i in range(0, 26):
            image = self.create_image(chr(ord('a') + i))
            self.save_image(image, self.image_dir + chr(ord('a') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
            sys.stdout.write('.')
            sys.stdout.flush()
        for i in range(0, 26):
            image = self.create_image(chr(ord('A') + i))
            self.save_image(image, self.image_dir + chr(ord('A') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
            sys.stdout.write('.')
            sys.stdout.flush()
        for i in range(0, 10):
            image = self.create_image(chr(ord('0') + i))
            self.save_image(image, self.image_dir + chr(ord('0') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
            sys.stdout.write('.')
            sys.stdout.flush()
        return (26 + 26 + 10)

    # generate the character from the font_file and returns a numpy array
    def generate_image(self, character, font_file = ''):
        if (font_file != ''):
            self.font_file = font_file

        return self.create_image(character)

    # test method, create character images for all fonts in "font_dir"
    def test(self):
        import time

        # look for ttf files
        files = os.listdir(self.font_dir)
        font_files = fnmatch.filter(files, self.pattern)

        # create "image_dir" if it doesn't exist
        if not os.path.isdir(self.image_dir):
            os.mkdir(self.image_dir)

        sys.stdout.write( str(len(font_files)) + ' fonts found, generating jpg images in folder ' + self.image_dir )
        sys.stdout.flush()

        # main loop
        t =  time.time()
        n = 0

        for font_file in font_files:
            self.font_file = self.font_dir + font_file
            n += self.process_font()
        t = time.time() - t

        sys.stdout.write('\nall done!\n' + str(n) + ' images generated in ' + str(t) + 's (average : ' + str(1000 * t / n) + ' ms/im)\n')

if __name__ == '__main__':

    myttf2jpg = ttf2jpg()
    myttf2jpg.test()