From c8948cdfe1bdc2f08b7df2c4f51130ed7f4f1d18 Mon Sep 17 00:00:00 2001 From: heyzec Date: Wed, 31 Mar 2021 22:45:06 +0800 Subject: [PATCH] Partial solution to remove ImageMagick dependency The convert.py script will do the necessary scaling and changing of colors instead of ImageMagick. Inkscape is still required to do the final conversion of svg to png. Requires lxml python package but in theory, can modify it to just do the svg transformations using plain regex. --- convert.py | 43 +++++++++++++++++++++++++++++++++++++++++++ matter.py | 17 ++++++++++------- 2 files changed, 53 insertions(+), 7 deletions(-) create mode 100755 convert.py diff --git a/convert.py b/convert.py new file mode 100755 index 0000000..f1bcc04 --- /dev/null +++ b/convert.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +import os +import lxml.etree as ET + +def inkscape_export_svg2png(color, src_path, dst_path): + NS = {"svg" : 'http://www.w3.org/2000/svg'} + FRAC = 0.7 + TEMPFILE = 'temp.svg' + + dom = ET.parse(src_path) + root = dom.getroot() + width, height = int(root.attrib['width']), int(root.attrib['height']) + interval = (1-FRAC)*width/2 + + elements = root.findall('svg:path', namespaces=NS) + group = ET.SubElement(root, "g") + for element in elements: + element.attrib['style'] = f'fill:{color};fill-opacity:1' + group.append(element) + group.attrib['transform'] = f"matrix({FRAC},0,0,{FRAC},{interval},{interval})" + + et = ET.ElementTree(root) + et.write(TEMPFILE, pretty_print=True) + + cmd = f"inkscape --export-filename='{dst_path}' {TEMPFILE} -w 72 2>/dev/null" + exit_code = os.system(cmd) + os.remove(TEMPFILE) + return exit_code + +def magick_convert_svg2png(color, src_path, dst_path): + command = ( + r"convert -trim -scale 36x36 -extent 72x72 -gravity center " + r"-define png:color-type=6 -background none -colorspace sRGB -channel RGB " + rf"-threshold -1 -density 300 -fill \{color} +opaque none " + rf"{src_path} {dst_path}" + ) + return os.system(command) + +if __name__ == '__main__': + for file in os.listdir('./icons'): + basename, ext = os.path.splitext(file) + if ext == '.svg': + inkscape_export_svg2png('#FFFFFF', f'icons/{basename}.svg', f'icons/INK_{basename}.png') diff --git a/matter.py b/matter.py index 6c5b409..ab5b587 100755 --- a/matter.py +++ b/matter.py @@ -11,6 +11,8 @@ from subprocess import run, check_call, PIPE from shutil import which, rmtree, copytree, copyfile +from convert import inkscape_export_svg2png + # Configuration constants MIN_PYTHON_VERSION = (3, 6) # Mainly for f-strings @@ -248,13 +250,14 @@ def convert_icon_svg2png(icon_name): ) src_path = ICON_SVG_PATHF.format(icon_name) dst_path = ICON_PNG_PATHF.format(icon_name) - command = ( - r"convert -trim -scale 36x36 -extent 72x72 -gravity center " - r"-define png:color-type=6 -background none -colorspace sRGB -channel RGB " - rf"-threshold -1 -density 300 -fill \{color} +opaque none " - rf"{src_path} {dst_path}" - ) - exit_code = sh(command) + # command = ( + # r"convert -trim -scale 36x36 -extent 72x72 -gravity center " + # r"-define png:color-type=6 -background none -colorspace sRGB -channel RGB " + # rf"-threshold -1 -density 300 -fill \{color} +opaque none " + # rf"{src_path} {dst_path}" + # ) + # exit_code = sh(command) + exit_code = inkscape_export_svg2png(color, src_path, dst_path) if exit_code != 0: error("Stop. The convert command returned an error")