Skip to content

Commit

Permalink
Partial solution to remove ImageMagick dependency
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
heyzec committed Apr 2, 2021
1 parent 3d83c23 commit c8948cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
43 changes: 43 additions & 0 deletions convert.py
Original file line number Diff line number Diff line change
@@ -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')
17 changes: 10 additions & 7 deletions matter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit c8948cd

Please sign in to comment.