-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
svg2png.py
60 lines (48 loc) · 1.11 KB
/
svg2png.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
from pathlib import Path
import re
import shlex
import subprocess
import threading
import queue
import sys
to_p = Path("svg2png")
to_p.mkdir(exist_ok=True)
images = [
f
for f in Path("_build/html/_images/").glob("*.svg")
if not re.match(r"^seg[0-9A-F]{2}$", f.stem)
]
q = queue.Queue()
def worker():
while True:
item = q.get()
if item is None:
break
to = to_p / item.with_suffix(".png").name
if to.is_file():
q.task_done()
continue
args = [
"inkscape",
f"--export-filename={to.absolute()}",
"-w",
"800",
"--export-background=white",
str(item.absolute()),
]
print("Running: {}".format(" ".join(shlex.quote(x) for x in args)))
subprocess.check_call(args)
q.task_done()
NUM_THREADS = 8
threads = []
for i in range(NUM_THREADS):
t = threading.Thread(target=worker)
t.start()
threads.append(t)
for img in sorted(images):
q.put(img)
q.join()
for i in range(NUM_THREADS):
q.put(None)
for t in threads:
t.join()