annotate transformations/ttf2jpg.py @ 26:47e7202d4f19

Array format handling bug fix for thick.py
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Fri, 29 Jan 2010 14:09:14 -0500
parents 424b5b0d9fcb
children 6d432a5010a2
rev   line source
6
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
1 #!/usr/bin/python
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
2 # -*- coding: iso-8859-1 -*-
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
3 # ttf2jpg.py converts font files to jpg images
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
4 # download fonts from http://www.dafont.com
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
5
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
6 ##################
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
7 # import libraries
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
8 ##################
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
9
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
10 import sys, os, fnmatch
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
11 import Image
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
12 import ImageFont, ImageDraw
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
13
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
14 ###########
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
15 # variables
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
16 ###########
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
17 # don't forget the "/" at the end of directories
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
18
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
19 font_dir = "/usr/share/fonts/truetype/ttf-liberation/"
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
20 image_dir = "./images/"
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
21 pattern = "*.ttf"
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
22
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
23 ###########
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
24 # functions
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
25 ###########
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
26
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
27 # save a picture of "text" with font "font_file"
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
28 def write_image(text, font_file):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
29 # print "Writing \"" + text + "\" with " + font_file + "..."
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
30 sys.stdout.write(".")
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
31 sys.stdout.flush()
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
32
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
33 # create a 32x32 white picture, and a drawing space
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
34 image = Image.new("L", (32, 32), "White")
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
35 draw = ImageDraw.Draw(image)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
36
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
37 # load the font with the right size
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
38 font = ImageFont.truetype(font_dir + font_file, 28)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
39 w,h = draw.textsize(text, font=font)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
40
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
41 # write text and aligns it
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
42 draw.text(((32 - w) / 2, ((32 - h) / 2)), text, font=font)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
43
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
44 # show image, xv must be installed
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
45 #image.show()
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
46
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
47 # save the picture
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
48 image.save(image_dir + text + "-" + font_file + ".jpg")
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
49
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
50 # write all the letters and numbers into pictures
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
51 def process_font(font_file):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
52 for i in range(0, 26):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
53 write_image(chr(ord('a') + i), font_file)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
54 for i in range(0, 26):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
55 write_image(chr(ord('A') + i), font_file)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
56 for i in range(0, 10):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
57 write_image(chr(ord('0') + i), font_file)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
58
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
59 ######
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
60 # main
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
61 ######
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
62
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
63 # look for ttf files
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
64 files = os.listdir(font_dir)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
65 font_files = fnmatch.filter(files, pattern)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
66
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
67 # create "image_dir" if it doesn't exist
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
68 if not os.path.isdir(image_dir):
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
69 os.mkdir(image_dir)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
70
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
71 sys.stdout.write( str(len(font_files)) + " fonts found, generating jpg images in folder " + image_dir )
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
72 sys.stdout.flush()
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
73
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
74 # main loop
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
75 for font_file in font_files:
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
76 process_font(font_file)
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
77
424b5b0d9fcb Image generation from fonts added
guitch
parents:
diff changeset
78 sys.stdout.write("\nall done!\n")