annotate transformations/ttf2jpg.py @ 109:9c45e0071b52

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