-
Notifications
You must be signed in to change notification settings - Fork 0
/
scribe.py
executable file
·60 lines (47 loc) · 1.86 KB
/
scribe.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Uses scribe_interface to render given text to image.
'''
import random
from scribe_interface import scribe_text
from trimmers import horztrim
def scribe_wrapper(text, fontname, styleid, size,
height, hbuffer, vbuffer,
twist):
styles = '', ' Italic', ' Bold', ' Bold Italic'
font_style = "{} {} {}".format(fontname, styles[styleid], size)
if type(text) is list:
text = ''.join(text)
lines = text.split('\n')
n_lines = len(lines)
n_letters = max(len(line) for line in lines)
line_ht = height / (n_lines+1)
letter_wd = .7 * line_ht
width = round((n_letters+2) * letter_wd)
return scribe_text(text, font_style, width, height, hbuffer, vbuffer, twist)
class Scribe():
def __init__(self, language, **kwargs):
self.language = language
self.height = kwargs["height"]
self.vbuffer = kwargs["vbuffer"]
self.hbuffer = kwargs["hbuffer"]
self.maxangle = kwargs["maxangle"]
self.nchars = kwargs["nchars_per_sample"]
self.size = kwargs["size"]
self.noise = kwargs["noise"]
def get_text_image(self):
twist = self.maxangle * random.uniform(-1, 1)
fontname, rel_size, styleid = self.language.random_font()
text = self.language.get_word(self.nchars)
img = scribe_wrapper(text, fontname, styleid, self.size,
self.height, self.hbuffer, self.vbuffer, twist)
img = horztrim(img, self.hbuffer)
return img, text, self.language.get_labels(text)
def __str__(self):
return "Height:{} Buffers:v{} h{} Size:{}" \
"\nAngle:{:.2f} Noise:{:.2f}" \
"\nNumChars:{}".format(
self.height, self.vbuffer, self.hbuffer, self.size,
self.maxangle, self.noise, self.nchars
)