forked from erikbern/deep-fonts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset.py
92 lines (72 loc) · 2.53 KB
/
create_dataset.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
import h5py
import PIL, PIL.ImageFont, PIL.Image, PIL.ImageDraw, PIL.ImageChops, PIL.ImageOps
import os
import random
import string
import numpy
import sys
w, h = 64, 64
w0, h0 = 256, 256
chars = string.uppercase + string.lowercase + string.digits
blank = PIL.Image.new('L', (w0*5, h0*3), 255)
def read_font(fn):
font = PIL.ImageFont.truetype(fn, min(w0, h0))
# We need to make sure we scale down the fonts but preserve the vertical alignment
min_ly = float('inf')
max_hy = float('-inf')
max_width = 0
imgs = []
for char in chars:
print '...', char
# Draw character
img = PIL.Image.new("L", (w0*5, h0*3), 255)
draw = PIL.ImageDraw.Draw(img)
draw.text((w0, h0), char, font=font)
# Get bounding box
diff = PIL.ImageChops.difference(img, blank)
lx, ly, hx, hy = diff.getbbox()
min_ly = min(min_ly, ly)
max_hy = max(max_hy, hy)
max_width = max(max_width, hx - lx)
imgs.append((lx, hx, img))
print 'crop dims:', max_hy - min_ly, max_width
scale_factor = min(1.0 * h / (max_hy - min_ly), 1.0 * w / max_width)
data = []
for lx, hx, img in imgs:
img = img.crop((lx, min_ly, hx, max_hy))
# Resize to smaller
new_width = (hx-lx) * scale_factor
new_height = (max_hy - min_ly) * scale_factor
img = img.resize((int(new_width), int(new_height)), PIL.Image.ANTIALIAS)
# Expand to square
img_sq = PIL.Image.new('L', (w, h), 255)
offset_x = (w - new_width)/2
offset_y = (h - new_height)/2
print offset_x, offset_y
img_sq.paste(img, (int(offset_x), int(offset_y)))
# Convert to numpy array
matrix = numpy.array(img_sq.getdata()).reshape((h, w))
matrix = 255 - matrix
data.append(matrix)
return numpy.array(data)
def get_ttfs(d='scraper/fonts'):
for dirpath, dirname, filenames in os.walk(d):
for filename in filenames:
if filename.endswith('.ttf') or filename.endswith('.otf'):
yield os.path.join(dirpath, filename)
f = h5py.File('fonts.hdf5', 'w')
dset = f.create_dataset('fonts', (1, len(chars), h, w), chunks=(1, len(chars), h, w), maxshape=(None, len(chars), h, w), dtype='u1')
i = 0
for fn in get_ttfs(d=sys.argv[1]):
print fn
try:
data = read_font(fn)
except: # IOError:
print 'was not able to read', fn
continue
print data.shape
dset.resize((i+1, len(chars), h, w))
dset[i] = data
i += 1
f.flush()
f.close()