diff 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
line wrap: on
line diff
--- a/transformations/ttf2jpg.py	Wed Feb 03 20:06:45 2010 -0500
+++ b/transformations/ttf2jpg.py	Wed Feb 03 21:24:05 2010 -0500
@@ -8,7 +8,7 @@
     Author: Guillaume Sicard
 '''
 
-import sys, os, fnmatch
+import sys, os, fnmatch, random
 import Image, ImageFont, ImageDraw, numpy
 
 class ttf2jpg():
@@ -19,6 +19,13 @@
         self.font_file = font_file
         self.image_dir = './images/'
         self.pattern = '*.ttf'
+        self.char_list = []
+        for i in range(0,26):
+            self.char_list.append(chr(ord('a') + i) )
+        for i in range(0,26):
+            self.char_list.append(chr(ord('A') + i) )
+        for i in range(0,10):
+            self.char_list.append(chr(ord('0') + i) )
 
     # get font name
     def get_settings_names(self):
@@ -33,6 +40,13 @@
         else:
             image.show()
 
+    # set a random font for character generation
+    def set_random_font(self):
+        files = os.listdir(self.font_dir)
+        font_files = fnmatch.filter(files, self.pattern)
+        i = random.randint(0, len(font_files) - 1)
+        self.font_file = self.font_dir + font_files[i]
+
     # return a picture array of "text" with font "font_file"
     def create_image(self, text):
          # create a w x h black picture, and a drawing space
@@ -53,31 +67,27 @@
 
     # write all the letters and numbers into pictures
     def process_font(self):
-        for i in range(0, 26):
-            image = self.create_image(chr(ord('a') + i))
-            self.save_image(image, self.image_dir + chr(ord('a') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
-            sys.stdout.write('.')
-            sys.stdout.flush()
-        for i in range(0, 26):
-            image = self.create_image(chr(ord('A') + i))
-            self.save_image(image, self.image_dir + chr(ord('A') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
+        for i in range(0, len(self.char_list) ):
+            image = self.create_image(self.char_list[i])
+            self.save_image(image, self.image_dir + self.char_list[i] + '-' + os.path.basename(self.font_file) + '.jpg')
             sys.stdout.write('.')
             sys.stdout.flush()
-        for i in range(0, 10):
-            image = self.create_image(chr(ord('0') + i))
-            self.save_image(image, self.image_dir + chr(ord('0') + i) + '-' + os.path.basename(self.font_file) + '.jpg')
-            sys.stdout.write('.')
-            sys.stdout.flush()
-        return (26 + 26 + 10)
+        return (len(self.char_list))
 
     # generate the character from the font_file and returns a numpy array
-    def generate_image(self, character, font_file = ''):
+    def generate_image_from_char(self, character, font_file = ''):
         if (font_file != ''):
             self.font_file = font_file
 
         return self.create_image(character)
 
-    # test method, create character images for all fonts in "font_dir"
+    # generate random character from random font file as a numpy array
+    def generate_image(self):
+        self.set_random_font()
+        i = random.randint(0, len(self.char_list) - 1)
+        return self.generate_image_from_char(self.char_list[i])
+
+    # test method, create character images for all fonts in "font_dir" in dir "image_dir"
     def test(self):
         import time
 
@@ -106,4 +116,6 @@
 if __name__ == '__main__':
 
     myttf2jpg = ttf2jpg()
-    myttf2jpg.test()
+    #myttf2jpg.test()
+    image = myttf2jpg.generate_image()
+    myttf2jpg.save_image(image, '')
\ No newline at end of file