From c68ea0c2f8d0d0250100243239548164f8dcd660 Mon Sep 17 00:00:00 2001 From: heyzec Date: Sun, 4 Apr 2021 20:09:48 +0800 Subject: [PATCH] More fixes (mateosss#61) - Changed verbosity of inkscape command - Script now works with older versions of inkscape (Tested on 0.92.5) --- svg2png.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/svg2png.py b/svg2png.py index 3801305..25b8d85 100755 --- a/svg2png.py +++ b/svg2png.py @@ -2,6 +2,7 @@ import os import re +import subprocess import xml.etree.ElementTree as ET import xml.dom.minidom @@ -65,8 +66,26 @@ def prettify(xml_string): with open(TEMPFILE, 'w') as f: f.write(xml_string) - cmd = f"inkscape --export-filename='{dst_path}' {TEMPFILE} -w 72 2>/dev/null" - exit_code = os.system(cmd) + + # Check inkscape version + version_string = subprocess.run('inkscape --version 2>/dev/null', + shell=True, capture_output=True).stdout.decode() + version = re.findall(r'(?<=Inkscape )[.\d]+', version_string)[0] + if version[0] == '1': + arguments = [f'--export-filename={dst_path}'] + elif version[0] == '0': + arguments = ['--without-gui', f'--export-png={dst_path}'] + arguments.extend([TEMPFILE, '-w', '72']) + + inkscape_proc = subprocess.Popen(['inkscape', *arguments], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + subprocess.run(['sed', 's/^/inkscape: /'], stdin=inkscape_proc.stdout) + inkscape_proc.wait() + exit_code = inkscape_proc.returncode + # Alternative - Using bash to pipe + # cmd = f"""/bin/bash -c 'inkscape {' '.join(arguments)} 2>&1 | sed "s/^/inkscape: /"; exit ${{PIPESTATUS[0]}}'""" + # exit_code = os.system(cmd) + os.remove(TEMPFILE) return exit_code