Mercurial > ift6266
comparison transformations/ttf2jpg.py @ 43:05145f4fb609
Added : generation of random character with random font file from a specified folder
author | Guillaume Sicard <guitch21@gmail.com> |
---|---|
date | Wed, 03 Feb 2010 21:24:05 -0500 |
parents | 6d432a5010a2 |
children | 9c45e0071b52 |
comparison
equal
deleted
inserted
replaced
42:7f5133e1fd23 | 43:05145f4fb609 |
---|---|
6 download fonts from http://www.dafont.com for exemple | 6 download fonts from http://www.dafont.com for exemple |
7 | 7 |
8 Author: Guillaume Sicard | 8 Author: Guillaume Sicard |
9 ''' | 9 ''' |
10 | 10 |
11 import sys, os, fnmatch | 11 import sys, os, fnmatch, random |
12 import Image, ImageFont, ImageDraw, numpy | 12 import Image, ImageFont, ImageDraw, numpy |
13 | 13 |
14 class ttf2jpg(): | 14 class ttf2jpg(): |
15 def __init__(self, font_file = ''): | 15 def __init__(self, font_file = ''): |
16 self.w = 32 | 16 self.w = 32 |
17 self.h = 32 | 17 self.h = 32 |
18 self.font_dir = '/usr/share/fonts/truetype/ttf-liberation/' | 18 self.font_dir = '/usr/share/fonts/truetype/ttf-liberation/' |
19 self.font_file = font_file | 19 self.font_file = font_file |
20 self.image_dir = './images/' | 20 self.image_dir = './images/' |
21 self.pattern = '*.ttf' | 21 self.pattern = '*.ttf' |
22 self.char_list = [] | |
23 for i in range(0,26): | |
24 self.char_list.append(chr(ord('a') + i) ) | |
25 for i in range(0,26): | |
26 self.char_list.append(chr(ord('A') + i) ) | |
27 for i in range(0,10): | |
28 self.char_list.append(chr(ord('0') + i) ) | |
22 | 29 |
23 # get font name | 30 # get font name |
24 def get_settings_names(self): | 31 def get_settings_names(self): |
25 return [self.font_file] | 32 return [self.font_file] |
26 | 33 |
30 image = Image.fromarray(image).convert('L') | 37 image = Image.fromarray(image).convert('L') |
31 if (filename != ''): | 38 if (filename != ''): |
32 image.save(filename) | 39 image.save(filename) |
33 else: | 40 else: |
34 image.show() | 41 image.show() |
42 | |
43 # set a random font for character generation | |
44 def set_random_font(self): | |
45 files = os.listdir(self.font_dir) | |
46 font_files = fnmatch.filter(files, self.pattern) | |
47 i = random.randint(0, len(font_files) - 1) | |
48 self.font_file = self.font_dir + font_files[i] | |
35 | 49 |
36 # return a picture array of "text" with font "font_file" | 50 # return a picture array of "text" with font "font_file" |
37 def create_image(self, text): | 51 def create_image(self, text): |
38 # create a w x h black picture, and a drawing space | 52 # create a w x h black picture, and a drawing space |
39 image = Image.new('L', (self.w, self.h), 'Black') | 53 image = Image.new('L', (self.w, self.h), 'Black') |
51 | 65 |
52 return image | 66 return image |
53 | 67 |
54 # write all the letters and numbers into pictures | 68 # write all the letters and numbers into pictures |
55 def process_font(self): | 69 def process_font(self): |
56 for i in range(0, 26): | 70 for i in range(0, len(self.char_list) ): |
57 image = self.create_image(chr(ord('a') + i)) | 71 image = self.create_image(self.char_list[i]) |
58 self.save_image(image, self.image_dir + chr(ord('a') + i) + '-' + os.path.basename(self.font_file) + '.jpg') | 72 self.save_image(image, self.image_dir + self.char_list[i] + '-' + os.path.basename(self.font_file) + '.jpg') |
59 sys.stdout.write('.') | 73 sys.stdout.write('.') |
60 sys.stdout.flush() | 74 sys.stdout.flush() |
61 for i in range(0, 26): | 75 return (len(self.char_list)) |
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) | |
72 | 76 |
73 # generate the character from the font_file and returns a numpy array | 77 # generate the character from the font_file and returns a numpy array |
74 def generate_image(self, character, font_file = ''): | 78 def generate_image_from_char(self, character, font_file = ''): |
75 if (font_file != ''): | 79 if (font_file != ''): |
76 self.font_file = font_file | 80 self.font_file = font_file |
77 | 81 |
78 return self.create_image(character) | 82 return self.create_image(character) |
79 | 83 |
80 # test method, create character images for all fonts in "font_dir" | 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]) | |
89 | |
90 # test method, create character images for all fonts in "font_dir" in dir "image_dir" | |
81 def test(self): | 91 def test(self): |
82 import time | 92 import time |
83 | 93 |
84 # look for ttf files | 94 # look for ttf files |
85 files = os.listdir(self.font_dir) | 95 files = os.listdir(self.font_dir) |
104 sys.stdout.write('\nall done!\n' + str(n) + ' images generated in ' + str(t) + 's (average : ' + str(1000 * t / n) + ' ms/im)\n') | 114 sys.stdout.write('\nall done!\n' + str(n) + ' images generated in ' + str(t) + 's (average : ' + str(1000 * t / n) + ' ms/im)\n') |
105 | 115 |
106 if __name__ == '__main__': | 116 if __name__ == '__main__': |
107 | 117 |
108 myttf2jpg = ttf2jpg() | 118 myttf2jpg = ttf2jpg() |
109 myttf2jpg.test() | 119 #myttf2jpg.test() |
120 image = myttf2jpg.generate_image() | |
121 myttf2jpg.save_image(image, '') |