comparison transformations/ttf2jpg.py @ 33:6d432a5010a2

Modified font generator script : module creation. Not fully compliant with testmod.py though (transformation != generation)
author Guillaume Sicard <guitch21@gmail.com>
date Mon, 01 Feb 2010 22:55:19 -0500
parents 424b5b0d9fcb
children 05145f4fb609
comparison
equal deleted inserted replaced
32:4d4248f7e2fb 33:6d432a5010a2
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- coding: iso-8859-1 -*- 2 # -*- coding: iso-8859-1 -*-
3 # ttf2jpg.py converts font files to jpg images
4 # download fonts from http://www.dafont.com
5 3
6 ################## 4 '''
7 # import libraries 5 Implementation of font image generator
8 ################## 6 download fonts from http://www.dafont.com for exemple
7
8 Author: Guillaume Sicard
9 '''
9 10
10 import sys, os, fnmatch 11 import sys, os, fnmatch
11 import Image 12 import Image, ImageFont, ImageDraw, numpy
12 import ImageFont, ImageDraw
13 13
14 ########### 14 class ttf2jpg():
15 # variables 15 def __init__(self, font_file = ''):
16 ########### 16 self.w = 32
17 # don't forget the "/" at the end of directories 17 self.h = 32
18 self.font_dir = '/usr/share/fonts/truetype/ttf-liberation/'
19 self.font_file = font_file
20 self.image_dir = './images/'
21 self.pattern = '*.ttf'
18 22
19 font_dir = "/usr/share/fonts/truetype/ttf-liberation/" 23 # get font name
20 image_dir = "./images/" 24 def get_settings_names(self):
21 pattern = "*.ttf" 25 return [self.font_file]
22 26
23 ########### 27 # save an image
24 # functions 28 def save_image(self,array, filename = ''):
25 ########### 29 image = (array * 255.0).astype('int')
30 image = Image.fromarray(image).convert('L')
31 if (filename != ''):
32 image.save(filename)
33 else:
34 image.show()
26 35
27 # save a picture of "text" with font "font_file" 36 # return a picture array of "text" with font "font_file"
28 def write_image(text, font_file): 37 def create_image(self, text):
29 # print "Writing \"" + text + "\" with " + font_file + "..." 38 # create a w x h black picture, and a drawing space
30 sys.stdout.write(".") 39 image = Image.new('L', (self.w, self.h), 'Black')
31 sys.stdout.flush() 40 draw = ImageDraw.Draw(image)
32 41
33 # create a 32x32 white picture, and a drawing space 42 # load the font with the right size
34 image = Image.new("L", (32, 32), "White") 43 font = ImageFont.truetype(self.font_file, 28)
35 draw = ImageDraw.Draw(image) 44 d_w,d_h = draw.textsize(text, font=font)
36 45
37 # load the font with the right size 46 # write text and aligns it
38 font = ImageFont.truetype(font_dir + font_file, 28) 47 draw.text(((32 - d_w) / 2, ((32 - d_h) / 2)), text, font=font, fill='White')
39 w,h = draw.textsize(text, font=font)
40 48
41 # write text and aligns it 49 image = numpy.asarray(image)
42 draw.text(((32 - w) / 2, ((32 - h) / 2)), text, font=font) 50 image = (image / 255.0).astype(numpy.float32)
43 51
44 # show image, xv must be installed 52 return image
45 #image.show()
46 53
47 # save the picture 54 # write all the letters and numbers into pictures
48 image.save(image_dir + text + "-" + font_file + ".jpg") 55 def process_font(self):
56 for i in range(0, 26):
57 image = self.create_image(chr(ord('a') + i))
58 self.save_image(image, self.image_dir + chr(ord('a') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
59 sys.stdout.write('.')
60 sys.stdout.flush()
61 for i in range(0, 26):
62 image = self.create_image(chr(ord('A') + i))
63 self.save_image(image, self.image_dir + chr(ord('A') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
64 sys.stdout.write('.')
65 sys.stdout.flush()
66 for i in range(0, 10):
67 image = self.create_image(chr(ord('0') + i))
68 self.save_image(image, self.image_dir + chr(ord('0') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
69 sys.stdout.write('.')
70 sys.stdout.flush()
71 return (26 + 26 + 10)
49 72
50 # write all the letters and numbers into pictures 73 # generate the character from the font_file and returns a numpy array
51 def process_font(font_file): 74 def generate_image(self, character, font_file = ''):
52 for i in range(0, 26): 75 if (font_file != ''):
53 write_image(chr(ord('a') + i), font_file) 76 self.font_file = font_file
54 for i in range(0, 26):
55 write_image(chr(ord('A') + i), font_file)
56 for i in range(0, 10):
57 write_image(chr(ord('0') + i), font_file)
58 77
59 ###### 78 return self.create_image(character)
60 # main
61 ######
62 79
63 # look for ttf files 80 # test method, create character images for all fonts in "font_dir"
64 files = os.listdir(font_dir) 81 def test(self):
65 font_files = fnmatch.filter(files, pattern) 82 import time
66 83
67 # create "image_dir" if it doesn't exist 84 # look for ttf files
68 if not os.path.isdir(image_dir): 85 files = os.listdir(self.font_dir)
69 os.mkdir(image_dir) 86 font_files = fnmatch.filter(files, self.pattern)
70 87
71 sys.stdout.write( str(len(font_files)) + " fonts found, generating jpg images in folder " + image_dir ) 88 # create "image_dir" if it doesn't exist
72 sys.stdout.flush() 89 if not os.path.isdir(self.image_dir):
90 os.mkdir(self.image_dir)
73 91
74 # main loop 92 sys.stdout.write( str(len(font_files)) + ' fonts found, generating jpg images in folder ' + self.image_dir )
75 for font_file in font_files: 93 sys.stdout.flush()
76 process_font(font_file)
77 94
78 sys.stdout.write("\nall done!\n") 95 # main loop
96 t = time.time()
97 n = 0
98
99 for font_file in font_files:
100 self.font_file = self.font_dir + font_file
101 n += self.process_font()
102 t = time.time() - t
103
104 sys.stdout.write('\nall done!\n' + str(n) + ' images generated in ' + str(t) + 's (average : ' + str(1000 * t / n) + ' ms/im)\n')
105
106 if __name__ == '__main__':
107
108 myttf2jpg = ttf2jpg()
109 myttf2jpg.test()