comparison data_generation/transformations/ttf2jpg.py @ 167:1f5937e9e530

More moves - transformations into data_generation, added "deep" folder
author Dumitru Erhan <dumitru.erhan@gmail.com>
date Fri, 26 Feb 2010 14:15:38 -0500
parents transformations/ttf2jpg.py@a4e5128ef2cb
children 7800be7bce66
comparison
equal deleted inserted replaced
166:17ae5a1a4dd1 167:1f5937e9e530
1 #!/usr/bin/python
2 # -*- coding: iso-8859-1 -*-
3
4 '''
5 Implementation of font image generator
6 download fonts from http://www.dafont.com for exemple
7
8 Author: Guillaume Sicard
9 '''
10
11 import sys, os, fnmatch, random
12 import Image, ImageFont, ImageDraw, numpy
13
14 class ttf2jpg():
15 def __init__(self, font_file = ''):
16 self.w = 32
17 self.h = 32
18 self.font_dir = '/Tmp/allfonts/'
19 self.font_file = font_file
20 self.image_dir = './images/'
21 self.pattern = '*.ttf'
22 self.char_list = []
23 for i in range(0,10):
24 self.char_list.append(chr(ord('0') + i) )
25 for i in range(0,26):
26 self.char_list.append(chr(ord('A') + i) )
27 for i in range(0,26):
28 self.char_list.append(chr(ord('a') + i) )
29 files = os.listdir(self.font_dir)
30 self.font_files = fnmatch.filter(files, '*.ttf') + fnmatch.filter(files, '*.TTF')
31
32 # get font name
33 def get_settings_names(self):
34 return [self.font_file]
35
36 # save an image
37 def save_image(self,array, filename = ''):
38 image = (array * 255.0).astype('int')
39 image = Image.fromarray(image).convert('L')
40 if (filename != ''):
41 image.save(filename)
42 else:
43 image.show()
44
45 # set a random font for character generation
46 def set_random_font(self):
47 i = random.randint(0, len(self.font_files) - 1)
48 self.font_file = self.font_dir + self.font_files[i]
49
50 # return a picture array of "text" with font "font_file"
51 def create_image(self, text):
52 # create a w x h black picture, and a drawing space
53 image = Image.new('L', (self.w, self.h), 'Black')
54 draw = ImageDraw.Draw(image)
55
56 # load the font with the right size
57 font = ImageFont.truetype(self.font_file, 28)
58 d_w,d_h = draw.textsize(text, font=font)
59
60 # write text and aligns it
61 draw.text(((32 - d_w) / 2, ((32 - d_h) / 2)), text, font=font, fill='White')
62
63 image = numpy.asarray(image)
64 image = (image / 255.0).astype(numpy.float32)
65
66 return image
67
68 # write all the letters and numbers into pictures
69 def process_font(self):
70 for i in range(0, len(self.char_list) ):
71 image = self.create_image(self.char_list[i])
72 self.save_image(image, self.image_dir + self.char_list[i] + '-' + os.path.basename(self.font_file) + '.jpg')
73 sys.stdout.write('.')
74 sys.stdout.flush()
75 return (len(self.char_list))
76
77 # generate the character from the font_file and returns a numpy array
78 def generate_image_from_char(self, character, font_file = ''):
79 if (font_file != ''):
80 self.font_file = font_file
81
82 return self.create_image(character)
83
84 # generate random character from random font file as a numpy array
85 def generate_image(self):
86 self.set_random_font()
87 i = random.randint(0, len(self.char_list) - 1)
88 return self.generate_image_from_char(self.char_list[i]), i
89
90 # test method, create character images for all fonts in "font_dir" in dir "image_dir"
91 def test(self):
92 import time
93
94 # look for ttf files
95 files = os.listdir(self.font_dir)
96 font_files = fnmatch.filter(files, self.pattern)
97
98 # create "image_dir" if it doesn't exist
99 if not os.path.isdir(self.image_dir):
100 os.mkdir(self.image_dir)
101
102 sys.stdout.write( str(len(font_files)) + ' fonts found, generating jpg images in folder ' + self.image_dir )
103 sys.stdout.flush()
104
105 # main loop
106 t = time.time()
107 n = 0
108
109 for font_file in font_files:
110 self.font_file = self.font_dir + font_file
111 n += self.process_font()
112 t = time.time() - t
113
114 sys.stdout.write('\nall done!\n' + str(n) + ' images generated in ' + str(t) + 's (average : ' + str(1000 * t / n) + ' ms/im)\n')
115
116 if __name__ == '__main__':
117
118 myttf2jpg = ttf2jpg()
119 #myttf2jpg.test()
120 image, i = myttf2jpg.generate_image()
121 myttf2jpg.save_image(image, '')