-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
316 lines (267 loc) · 12.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import argparse
import os
import pickle
from collections import namedtuple
from io import BytesIO
import matplotlib
import numpy as np
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
matplotlib.use('agg')
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('--model', dest='model_path', type=str, default=os.path.join('pretrained', 'model-29'),
help='(optional) DL model to use')
parser.add_argument('--text', dest='text', type=str, help='Text to write')
parser.add_argument('--text-file', dest='file', type=str, default=None, help='Path to the input text file')
parser.add_argument('--style', dest='style', type=int, default=0, help='Style of handwriting (1 to 7)')
parser.add_argument('--bias', dest='bias', type=float, default=0.9,
help='Bias in handwriting. More bias is more unclear handwriting (0.00 to 1.00)')
parser.add_argument('--force', dest='force', action='store_true', default=False)
parser.add_argument('--color', dest='color_text', type=str, default='0,0,150',
help='Color of handwriting in RGB format')
parser.add_argument('--output', dest='output', type=str, default='./handwritten.pdf',
help='Output PDF file path and name')
args = parser.parse_args()
def sample(e, mu1, mu2, std1, std2, rho):
cov = np.array([[std1 * std1, std1 * std2 * rho],
[std1 * std2 * rho, std2 * std2]])
mean = np.array([mu1, mu2])
x, y = np.random.multivariate_normal(mean, cov)
end = np.random.binomial(1, e)
# print(np.array([x, y, end]))
return np.array([x, y, end])
def split_strokes(points):
points = np.array(points)
strokes = []
b = 0
for e in range(len(points)):
if points[e, 2] == 1.:
strokes += [points[b:
e + 1, :2].copy()]
b = e + 1
return strokes
def cumsum(points):
sums = np.cumsum(points[:, :2], axis=0)
return np.concatenate([sums, points[:, 2:]], axis=1)
def sample_text(sess, args_text, translation, bias, style=None):
fields = ['coordinates', 'sequence', 'bias', 'e', 'pi', 'mu1', 'mu2', 'std1', 'std2',
'rho', 'window', 'kappa', 'phi', 'finish', 'zero_states']
vs = namedtuple('Params', fields)(
*[tf.get_collection(name)[0] for name in fields]
)
text = np.array([translation.get(c, 0) for c in args_text])
coord = np.array([0., 0., 1.])
coords = [coord]
# Prime the model with the author style if requested
prime_len, style_len = 0, 0
if style is not None:
# Priming consist of joining to a real pen-position and character sequences the synthetic sequence to generate
# and set the synthetic pen-position to a null vector (the positions are sampled from the MDN)
style_coords, style_text = style
prime_len = len(style_coords)
style_len = len(style_text)
prime_coords = list(style_coords)
coord = prime_coords[0] # Set the first pen stroke as the first element to process
text = np.r_[style_text, text] # concatenate on 1 axis the prime text + synthesis character sequence
sequence_prime = np.eye(len(translation), dtype=np.float32)[style_text]
sequence_prime = np.expand_dims(np.concatenate([sequence_prime, np.zeros((1, len(translation)))]), axis=0)
sequence = np.eye(len(translation), dtype=np.float32)[text]
sequence = np.expand_dims(np.concatenate([sequence, np.zeros((1, len(translation)))]), axis=0)
phi_data, window_data, kappa_data, stroke_data = [], [], [], []
sess.run(vs.zero_states)
sequence_len = len(args_text) + style_len
for s in range(1, 60 * sequence_len + 1):
is_priming = s < prime_len
# print('\r[{:5d}] sampling... {}'.format(s, 'priming' if is_priming else 'synthesis'), end='')
e, pi, mu1, mu2, std1, std2, rho, \
finish, phi, window, kappa = sess.run([vs.e, vs.pi, vs.mu1, vs.mu2,
vs.std1, vs.std2, vs.rho, vs.finish,
vs.phi, vs.window, vs.kappa],
feed_dict={
vs.coordinates: coord[None, None, ...],
vs.sequence: sequence_prime if is_priming else sequence,
vs.bias: bias
})
if is_priming:
# Use the real coordinate if priming
coord = prime_coords[s]
else:
# Synthesis mode
phi_data += [phi[0, :]]
window_data += [window[0, :]]
kappa_data += [kappa[0, :]]
# ---
g = np.random.choice(np.arange(pi.shape[1]), p=pi[0])
coord = sample(e[0, 0], mu1[0, g], mu2[0, g],
std1[0, g], std2[0, g], rho[0, g])
coords += [coord]
stroke_data += [[mu1[0, g], mu2[0, g], std1[0, g], std2[0, g], rho[0, g], coord[2]]]
if not args.force and finish[0, 0] > 0.8:
# print('\nFinished sampling!\n')
break
coords = np.array(coords)
coords[-1, 2] = 1.
return phi_data, window_data, kappa_data, stroke_data, coords
from PIL import Image
def add_color(color, image_out):
print("Applying color : ", color)
img = Image.open(image_out)
width, height = img.size
for x in range(width):
for y in range(height):
old_color = list(img.getpixel((x, y)))
if old_color != [255, 255, 255, 255]:
new_color = [color[x]
for x in range(3)]
img.putpixel((x, y), tuple(new_color))
else:
new_color = [255, 255, 255, 0]
img.putpixel((x, y), tuple(new_color))
imgout = BytesIO()
img.save(imgout, 'PNG')
imgout.seek(0)
return imgout
##################################################################
# The Generator Function #
##################################################################
def generate(args_text, args, sess, translation, text_color=[0, 0, 0]):
style = None
if args.style is not None:
style = None
with open(os.path.join('data', 'styles.pkl'), 'rb') as file:
styles = pickle.load(file)
if args.style > len(styles[0]):
raise ValueError('Requested style is not in style list')
style = [styles[0][args.style], styles[1][args.style]]
currentX = 0
currentY = 0
currentLen = 0
line_length = 36
line_height = -4
num_lines = len(args_text) // 50
text_remaining = len(args_text)
lines_per_page = 28
curr_page = 1
cuur_line = 1
fig, ax = plt.subplots(1, 1)
plt.figure(num=None, figsize=(115, 5 * min(lines_per_page, text_remaining // line_length + args_text.count('\n'))),
dpi=35,
facecolor='w', edgecolor='k')
print('Writing...')
for multiline_text in args_text.split(' '):
for text_without_spaces in multiline_text.split('\n'):
text = " {} ".format(text_without_spaces)
phi_data, window_data, kappa_data, stroke_data, coords = sample_text(sess, text, translation, args.bias,
style)
if currentLen + len(text_without_spaces) > line_length or multiline_text.split('\n').index(
text_without_spaces) > 0:
# print(currentLen)
currentY += line_height
currentX = 0
currentLen = 0
print('')
cuur_line += 1
strokes = np.array(stroke_data)
epsilon = 1e-8
strokes[:, :2] = np.cumsum(strokes[:, :2], axis=0)
minx, maxx = np.min(strokes[:, 0]), np.max(strokes[:, 0])
miny, maxy = np.min(strokes[:, 1]), np.max(strokes[:, 1])
for stroke in split_strokes(cumsum(np.array(coords))):
if np.min(stroke[:, 0]) > maxx - 2 and np.max(stroke[:, 0]) < maxx + 2:
continue
plt.plot(stroke[:, 0] + currentX, -stroke[:, 1] + currentY)
currentX += maxx - 2
currentLen += len(text_without_spaces) + 1
text_remaining -= (len(text_without_spaces) + 1)
print(text, end=' ', flush=True)
if cuur_line >= lines_per_page:
ax.set_aspect('equal')
plt.axis('off')
figfile = BytesIO()
print("\n\nProcessing page No. {}...\nCreating image...".format(curr_page), flush=True)
plt.savefig(figfile, format='png', bbox_inches='tight')
figfile.seek(0) # rewind to beginning of file
print("Colouring text...", flush=True)
figfile1 = add_color(text_color, figfile)
print("Saving image...", flush=True)
image_out = 'pages/page{}.png'.format(curr_page)
with open(image_out, 'wb') as fl:
for x in figfile1:
fl.write(x)
from PIL import Image
img = Image.open(image_out)
img.load()
img = img.resize((int(img.size[0] * 0.8), int(img.size[1] * 0.804)), Image.ANTIALIAS)
#background = Image.new("RGB", img.size, (255, 255, 255))
background = Image.open('blank_page.jpg')
background.load()
background.paste(img, mask=img.split()[3], box=(20, 300)) # 3 is the alpha channel
background.save(image_out.replace('.png', '.jpg'), 'JPEG', quality=100)
print("\nPage No. {} done!\n\n".format(curr_page), flush=True)
fig, ax = plt.subplots(1, 1)
plt.figure(num=None, figsize=(115, 5 * min(lines_per_page, text_remaining // line_length + args_text[
args_text.index(
text_without_spaces):].count(
'\n'))), dpi=40, facecolor='w',
edgecolor='k')
curr_page += 1
currentX = 0
currentY = 0
currentLen = 0
cuur_line = 1
ax.set_aspect('equal')
plt.axis('off')
figfile = BytesIO()
print("\n\nProcessing page No. {}...\nCreating image...".format(curr_page), flush=True)
plt.savefig(figfile, format='png', bbox_inches='tight')
figfile.seek(0) # rewind to beginning of file
print("Colouring text...", flush=True)
figfile1 = add_color(text_color, figfile)
print("Saving image...", flush=True)
image_out = 'pages/page{}.png'.format(curr_page)
with open(image_out, 'wb') as fl:
for x in figfile1:
fl.write(x)
from PIL import Image
img = Image.open(image_out)
img.load()
img = img.resize((int(img.size[0] * 0.8), int(img.size[1] * 0.804)), Image.ANTIALIAS)
#background = Image.new("RGB", img.size, (255, 255, 255))
background = Image.open('blank_page.jpg')
background.load()
background.paste(img, mask=img.split()[3], box=(20, 290)) # 3 is the alpha channel
background.save(image_out.replace('.png', '.jpg'), 'JPEG', quality=100)
print("\nPage No. {} done!\n\n".format(curr_page), flush=True)
# Generate PDF
print('\nGenerating PDF...', end='')
'''
imagelist = [Image.open('pages/page{}.png'.format(i)) for i in range(2, curr_page + 1)]
#First create a pdf file if not created
pdf_creation(Image.open('pages/page1.png'))
#Now I am opening each images and converting them to pdf
#Appending them to pdfs
for PNG_FILE in imagelist:
pdf_creation(PNG_FILE,flag=True)
'''
from PIL import Image
img1 = Image.open('pages/page1.jpg')
#pdf_creation(Image.open('pages/page1.jpg'))
im_list = [Image.open('pages/page{}.jpg'.format(i)) for i in range(2, curr_page + 1)]
img1.save(args.output, "PDF", resolution=100.0, save_all=True, append_images=im_list)
'''
pdf_creation(Image.open('pages/page1.png'))
im_list = [Image.open('pages/page{}.png'.format(i)) for i in range(2, curr_page + 1)]
for imgnew in im_list:
pdf_creation(imgnew,flag=True)
'''
print("done\n\nSuccessfully generated handwritten pdf from text at :\n{}".format(args.output))
return args.output
def pdf_creation(PNG_FILE,flag=False):
width, height = int(8.27 * 300), int(11.7 * 300)
from PIL import Image
rgba = PNG_FILE
rgb = Image.new('RGB', (width, height),(255, 255, 255)) # white background
rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask
rgb.save('final_output.pdf', append=flag) #Now save multiple images in same pdf file