annotate transformations/ttf2jpg.py @ 41:fdb0e0870fb4

Beaucoup de modifications à pipeline.py pour généraliser et un début de visualisation, et créé un wrapper (run_pipeline.py) pour appeler avec GIMP. - Modifications à pipeline.py - Wrappé la boucle du pipeline dans une classe - Isolé le problème de itérer sur les batches et les complexités dans des itérateurs - Permet d'avoir des ordres compliqués de batch (plusieurs sources), de complexités - Maintenant regenerate_parameters() est appelé pour chaque image. - Command line arguments avec getopt(). On pourra rajouter des options ainsi. - run_pipeline.py - Le but est de permettre de passer des arguments. Pas facile (pas trouvé comment de façon simple) avec la command line pour appeler GIMP en mode batch. C'est un hack ici. - Le but ultime est de permettre de lancer les jobs sur les clusters avec dbidispatch en précisant les options (diff. pour chaque job) sur la ligne de commande.
author fsavard
date Wed, 03 Feb 2010 17:08:27 -0500
parents 6d432a5010a2
children 05145f4fb609
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
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
11 import sys, os, fnmatch
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
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
18 self.font_dir = '/usr/share/fonts/truetype/ttf-liberation/'
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'
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
22
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
23 # 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
24 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
25 return [self.font_file]
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
26
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
27 # 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
28 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
29 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
30 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
31 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
32 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
33 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
34 image.show()
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
35
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
36 # 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
37 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
38 # 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
39 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
40 draw = ImageDraw.Draw(image)
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
41
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
42 # 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
43 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
44 d_w,d_h = draw.textsize(text, font=font)
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
45
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
46 # 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
47 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
48
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
49 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
50 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
51
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 return image
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
53
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
54 # 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
55 def process_font(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
56 for i in range(0, 26):
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 image = self.create_image(chr(ord('a') + i))
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 self.save_image(image, self.image_dir + chr(ord('a') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
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
59 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
60 sys.stdout.flush()
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 for i in range(0, 26):
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 image = self.create_image(chr(ord('A') + i))
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 self.save_image(image, self.image_dir + chr(ord('A') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
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 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
65 sys.stdout.flush()
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 for i in range(0, 10):
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
67 image = self.create_image(chr(ord('0') + i))
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 self.save_image(image, self.image_dir + chr(ord('0') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
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 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
70 sys.stdout.flush()
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
71 return (26 + 26 + 10)
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
72
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 # generate the character from the font_file and returns a numpy array
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 def generate_image(self, character, 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
75 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
76 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
77
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
78 return self.create_image(character)
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
79
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
80 # test method, create character images for all fonts in "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
81 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
82 import time
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
83
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
84 # 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
85 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
86 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
87
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
88 # 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
89 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
90 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
91
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 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
93 sys.stdout.flush()
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
94
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
95 # 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
96 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
97 n = 0
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
98
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
99 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
100 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
101 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
102 t = time.time() - t
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
103
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
104 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
105
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
106 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
107
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
108 myttf2jpg = 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
109 myttf2jpg.test()