esphome-docs/svg2png.py

67 lines
1.3 KiB
Python
Raw Normal View History

from pathlib import Path
import re
import shlex
import subprocess
2020-07-15 18:50:14 +02:00
import threading
import queue
import sys
to_p = Path("svg2png")
to_p.mkdir(exist_ok=True)
for f in to_p.glob("*.png"):
f.unlink()
images = [
f
for f in Path("_build/html/_images/").glob("*.svg")
if not re.match(r"^seg[0-9A-F]{2}$", f.stem)
]
2020-07-15 18:50:14 +02:00
q = queue.Queue()
2020-07-15 18:50:14 +02:00
def worker():
while True:
item = q.get()
if item is None:
break
to = to_p / item.with_suffix(".png").name
args = [
"inkscape",
"-z",
"-e",
str(to.absolute()),
"-w",
"800",
"-background",
"white",
str(item.absolute()),
]
print("Running: {}".format(" ".join(shlex.quote(x) for x in args)))
2020-07-15 18:50:14 +02:00
proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if b"Bitmap saved as" not in proc.stdout:
2020-07-15 18:50:14 +02:00
print("Error!")
print(proc.stdout)
sys.exit(1)
q.task_done()
2020-07-15 18:50:14 +02:00
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()