-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·179 lines (147 loc) · 5.94 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
#-*- coding:utf8 -*-
""" This code generates the training files for tesseract-ocr for bootstrapping
a new character set
"""
import tatfile
import os
import sys
import pango
import cairo
import pangocairo
from PIL import Image, ImageDraw, ImageChops
import train
def expand(temp_bbox):
"""expand a bounding box a little bit"""
tol = 2
bbox = (temp_bbox[0] - tol, temp_bbox[1] - tol, temp_bbox[2] + tol, \
temp_bbox[3] + tol)
return bbox
def draw(font_name, font_size, lang, alphabets):
""" Generates tif images and box files"""
font_string = font_name + ' ' + str(font_size)
image_dir = lang + "." + "images"
if(os.path.exists(image_dir)):
pass
else:
os.mkdir(image_dir)
boxfile = image_dir + "/" + lang + "." + font_name + \
".exp%2d.box" % (font_size)
file_box = open(boxfile, "w")
# TODO: A4 = 2480x3508 with 300x300 DPI
# TODO: A5 format 1754x2480 with 300x300 DPI
bigimage = Image.new("L", (2000, 2000), 255)
x_val = y_val = 10
count = 0
for akshar in alphabets:
akshar.strip() # remove nasty characters
# I shall now create an image with black bgc and white font color. One
# getbbox() determines the bounding box values I shall invert the
# image. This has to be done since getbbox() only finds bounding box
# values for non-zero pixels (read as white), but tesseract-ocr runs on
# the exact opposite bgc fgc combination. Contact [email protected].
# The lines below are pango/cairo code
surface = cairo.ImageSurface(cairo.FORMAT_A8, font_size * 4, \
font_size * 3)
context = cairo.Context(surface)
p_context = pangocairo.CairoContext(context)
layout = p_context.create_layout()
layout.set_font_description(pango.FontDescription(font_string))
layout.set_text(akshar)
#print akshar
# Lines take care of centering the text.
position = (10, 10) # (width/2.0 - w/2.0, height/2.0 - h/2.0)
context.move_to(position[0], position[1])
p_context.show_layout(layout)
surface.write_to_png("pango.png")
# Here we open the generated image using PIL functions
# Black background, white text
temp_image = Image.open("pango.png")
bbox = temp_image.getbbox()
deltax = bbox[2] - bbox[0]
deltay = bbox[3] - bbox[1]
#print bbox
new_image = temp_image.crop(bbox)
temp_image = temp_image.load()
# White background, black text
inverted_image = ImageChops.invert(new_image)
inverted_image.save(image_dir + "/" + str(count) + ".png")
count = count + 1
bigimage.paste(inverted_image, (x_val, y_val))
bigbox = (x_val, y_val, x_val + deltax, y_val + deltay)
x_val = bigbox[2] + 5
if x_val > 1950:
x_val = 10
y_val = y_val + 40
# Delete the pango generated png
os.unlink("pango.png")
# This is the line to be added to the box file
line = akshar + " " + str(bigbox[0] - 1) + " " + \
str(2000 - (bigbox[1] + deltay) - 1) + " " + \
str(bigbox[2] + 1) + " " + str(2000 - (bigbox[3] - deltay) + 1)
#print "line:", line
file_box.write(line + '\n')
# Degrade code starts
strip = [deltax * .2, deltax * .4, deltax * .7]
for values in range(0, 3):
distort2 = inverted_image
for wai in range(0, deltay):
for ex in range(int(strip[values]), int(strip[values]) + 1):
distort2.putpixel((ex, wai), 255)
bigbox = (x_val, y_val, x_val + deltax, y_val + deltay)
# This is the line to be added to the box file
line = akshar + " " + str(bigbox[0] - 1) + " " + \
str(2000 - (bigbox[1] + deltay) - 1) + " " + \
str(bigbox[2] + 1) + " " + \
str(2000 - (bigbox[3] - deltay) + 1)
file_box.write(line + '\n')
bigimage.paste(distort2, (x_val, y_val))
x_val = bigbox[2] + 5
if x_val > 1950:
x_val = 10
y_val = y_val + 40
# Degrade code ends
# TODO: multipage tiff maybe with:
# - http://code.google.com/p/pylibtiff/
# - http://freeimagepy.sourceforge.net/
# - http://code.google.com/p/pylepthonica/
bigimage.save(image_dir + "/" + lang + "." + font_name + \
".exp%2d.tif" % font_size, "TIFF", dpi=(600, 600))
file_box.close()
def main():
"""Main"""
if(len(sys.argv) != 9):
print "Usage: python generate.py -font <font name> -l <language> " + \
"-s <size> -a <input alphabet directory>"
exit()
if(sys.argv[1] == "-font"):
font_name = sys.argv[2]
else:
print "Usage: python generate.py -font <font name> -l <language> " + \
"-s <size> -a <input alphabet directory>"
exit()
if(sys.argv[3] == "-l"):
lang = sys.argv[4]
else:
print "Usage: python generate.py -font <font name> -l <language> " + \
"-s <size> -a <input alphabet directory>"
exit()
if(sys.argv[5] == "-s"):
font_size = sys.argv[6]
else:
print "Usage: python generate.py -font <font name> -l <language> " + \
"-s <size> -a <input alphabet directory>"
exit()
if(sys.argv[7] == "-a"):
alphabet_dir = sys.argv[8]
else:
print "Usage: python generate.py -font <font name> -l <language> " + \
"-s <size> -a <input alphabet directory>"
exit()
draw(font_name, int(font_size), lang, tatfile.read_file(alphabet_dir))
# Begin training
# Reads all fonts in the directory font_dir and trains
train.train(lang, lang + "." + font_name + ".exp%2d" % (int(font_size)))
#training ends
if __name__ == '__main__':
main()