-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_text.py
More file actions
74 lines (59 loc) · 1.76 KB
/
generate_text.py
File metadata and controls
74 lines (59 loc) · 1.76 KB
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
import argparse
import subprocess
import tempfile
from pathlib import Path
import numpy as np
preamble = r"""\documentclass[tikz,crop]{standalone}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,shadows,shadows.blur,matrix,backgrounds}
\tikzset{white background/.style={show background rectangle,tight background,
background rectangle/.style={fill=white}}}
"""
body = """
\\begin{{document}}
\\setlength{{\\fboxsep}}{{0pt}}
\\begin{{tikzpicture}}[white background]
{}
\\end{{tikzpicture}}
\\end{{document}}
"""
text_template = """
\\node[align=center] {{{}}};
"""
np.random.seed(0xCAFFE)
def render_single(text: str, name: str):
node = text_template.format(text)
tex_file = [preamble, body.format(node)]
with tempfile.NamedTemporaryFile(suffix=".tex", mode="w") as temp_file:
content = "\n".join(tex_file)
temp_file.write(content)
temp_file.flush()
path = Path("texts")
path.mkdir(exist_ok=True, parents=True)
compile_command = [
"pdflatex",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
f"-output-dir=/tmp",
"-recorder",
temp_file.name,
]
convert_command = [
"convert",
"-density",
"300",
Path(temp_file.name).with_suffix(".pdf"),
path / f"{name}.jpg",
]
subprocess.call(compile_command, stdout=subprocess.DEVNULL)
subprocess.call(convert_command, stdout=subprocess.DEVNULL)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("text")
parser.add_argument("name")
args = parser.parse_args()
render_single(args.text, args.name)
if __name__ == "__main__":
main()