Speed up svg2png script (#686)
49
svg2png.py
@ -2,6 +2,9 @@ from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
import threading
|
||||
import queue
|
||||
import sys
|
||||
|
||||
|
||||
to_p = Path('svg2png')
|
||||
@ -11,14 +14,40 @@ for f in to_p.glob('*.png'):
|
||||
|
||||
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()
|
||||
|
||||
for from_ in sorted(images):
|
||||
to_ = to_p / from_.with_suffix('.png').name
|
||||
args = ['inkscape', '-z', '-e', str(to_.absolute()), '-w', '800',
|
||||
'-background', 'white', str(from_.absolute())]
|
||||
print("Running: {}".format(' '.join(shlex.quote(x) for x in args)))
|
||||
proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
if b'Bitmap saved as' not in proc.stdout:
|
||||
print("Error!")
|
||||
print(proc.stdout)
|
||||
raise ValueError
|
||||
|
||||
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)))
|
||||
proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
if b'Bitmap saved as' not in proc.stdout:
|
||||
print("Error!")
|
||||
print(proc.stdout)
|
||||
sys.exit(1)
|
||||
|
||||
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()
|
||||
|
BIN
svg2png/ac_dimmer.png
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
svg2png/connection.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
svg2png/dac.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
svg2png/diy.png
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
svg2png/function.png
Normal file
After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |