2020-08-18 20:17:36 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import inkex
|
|
|
|
import os
|
|
|
|
import base64
|
|
|
|
import urllib.request as urllib
|
|
|
|
from PIL import Image
|
|
|
|
from io import BytesIO
|
|
|
|
from lxml import etree
|
|
|
|
|
|
|
|
"""
|
|
|
|
Extension for InkScape 1.X
|
|
|
|
Features
|
|
|
|
- will vectorize your beautiful image into a more beautiful SVG trace with separated infills(break apart into single surfaces like a puzzle)
|
|
|
|
|
|
|
|
Author: Mario Voigt / FabLab Chemnitz
|
|
|
|
Mail: mario.voigt@stadtfabrikanten.org
|
|
|
|
Date: 18.08.2020
|
2021-04-23 15:21:30 +02:00
|
|
|
Last patch: 23.04.2021
|
2020-08-18 20:17:36 +02:00
|
|
|
License: GNU GPL v3
|
|
|
|
|
|
|
|
Used version of imagetracerjs: https://github.com/jankovicsandras/imagetracerjs/commit/4d0f429efbb936db1a43db80815007a2cb113b34
|
2021-04-23 15:21:30 +02:00
|
|
|
|
|
|
|
ToDo:
|
|
|
|
- fix resizing if one or all of the following sizes are zero:
|
|
|
|
img_w = image.get('width')
|
|
|
|
img_h = image.get('height')
|
|
|
|
img_x = image.get('x')
|
|
|
|
img_y = image.get('y')
|
2020-08-18 20:17:36 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-16 14:41:12 +02:00
|
|
|
class Imagetracerjs (inkex.EffectExtension):
|
2020-08-18 20:17:36 +02:00
|
|
|
|
2021-04-23 15:21:30 +02:00
|
|
|
def checkImagePath(self, element):
|
|
|
|
xlink = element.get('xlink:href')
|
2020-08-18 20:17:36 +02:00
|
|
|
if xlink and xlink[:5] == 'data:':
|
|
|
|
# No need, data alread embedded
|
|
|
|
return
|
|
|
|
|
|
|
|
url = urllib.urlparse(xlink)
|
|
|
|
href = urllib.url2pathname(url.path)
|
|
|
|
|
|
|
|
# Primary location always the filename itself.
|
|
|
|
path = self.absolute_href(href or '')
|
|
|
|
|
|
|
|
# Backup directory where we can find the image
|
|
|
|
if not os.path.isfile(path):
|
2021-04-23 15:21:30 +02:00
|
|
|
path = element.get('sodipodi:absref', path)
|
2020-08-18 20:17:36 +02:00
|
|
|
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
inkex.errormsg('File not found "{}". Unable to embed image.').format(path)
|
|
|
|
return
|
|
|
|
|
|
|
|
if (os.path.isfile(path)):
|
|
|
|
return path
|
|
|
|
|
2021-04-16 14:41:12 +02:00
|
|
|
def add_arguments(self, pars):
|
2021-04-19 20:54:38 +02:00
|
|
|
pars.add_argument("--tab")
|
2021-04-16 14:41:12 +02:00
|
|
|
pars.add_argument("--keeporiginal", type=inkex.Boolean, default=False, help="Keep original image on canvas")
|
|
|
|
pars.add_argument("--ltres", type=float, default=1.0, help="Error treshold straight lines")
|
|
|
|
pars.add_argument("--qtres", type=float, default=1.0, help="Error treshold quadratic splines")
|
|
|
|
pars.add_argument("--pathomit", type=int, default=8, help="Noise reduction - discard edge node paths shorter than")
|
|
|
|
pars.add_argument("--rightangleenhance", type=inkex.Boolean, default=True, help="Enhance right angle corners")
|
|
|
|
pars.add_argument("--colorsampling", default="2",help="Color sampling")
|
|
|
|
pars.add_argument("--numberofcolors", type=int, default=16, help="Number of colors to use on palette")
|
|
|
|
pars.add_argument("--mincolorratio", type=int, default=0, help="Color randomization ratio")
|
|
|
|
pars.add_argument("--colorquantcycles", type=int, default=3, help="Color quantization will be repeated this many times")
|
|
|
|
pars.add_argument("--layering", default="0",help="Layering")
|
|
|
|
pars.add_argument("--strokewidth", type=float, default=1.0, help="SVG stroke-width")
|
|
|
|
pars.add_argument("--linefilter", type=inkex.Boolean, default=False, help="Noise reduction line filter")
|
|
|
|
#pars.add_argument("--scale", type=float, default=1.0, help="Coordinate scale factor")
|
|
|
|
pars.add_argument("--roundcoords", type=int, default=1, help="Decimal places for rounding")
|
|
|
|
pars.add_argument("--viewbox", type=inkex.Boolean, default=False, help="Enable or disable SVG viewBox")
|
|
|
|
pars.add_argument("--desc", type=inkex.Boolean, default=False, help="SVG descriptions")
|
|
|
|
pars.add_argument("--blurradius", type=int, default=0, help="Selective Gaussian blur preprocessing")
|
|
|
|
pars.add_argument("--blurdelta", type=float, default=20.0, help="RGBA delta treshold for selective Gaussian blur preprocessing")
|
2020-08-18 20:17:36 +02:00
|
|
|
|
|
|
|
def effect(self):
|
2021-04-23 15:21:30 +02:00
|
|
|
|
2020-08-18 20:17:36 +02:00
|
|
|
# internal overwrite for scale:
|
|
|
|
self.options.scale = 1.0
|
|
|
|
|
2021-04-23 15:21:30 +02:00
|
|
|
if len(self.svg.selected) > 0:
|
|
|
|
images = self.svg.selection.filter(inkex.Image).values()
|
|
|
|
if len(images) > 0:
|
|
|
|
for image in images:
|
|
|
|
self.path = self.checkImagePath(image) # This also ensures the file exists
|
2020-08-18 20:17:36 +02:00
|
|
|
if self.path is None: # check if image is embedded or linked
|
2021-04-23 15:21:30 +02:00
|
|
|
image_string = image.get('{http://www.w3.org/1999/xlink}href')
|
2020-08-18 20:17:36 +02:00
|
|
|
# find comma position
|
|
|
|
i = 0
|
|
|
|
while i < 40:
|
|
|
|
if image_string[i] == ',':
|
|
|
|
break
|
|
|
|
i = i + 1
|
2021-04-23 15:21:30 +02:00
|
|
|
img = Image.open(BytesIO(base64.b64decode(image_string[i + 1:len(image_string)])))
|
2020-08-18 20:17:36 +02:00
|
|
|
else:
|
2021-04-23 15:21:30 +02:00
|
|
|
img = Image.open(self.path)
|
2020-08-18 20:17:36 +02:00
|
|
|
|
|
|
|
# Write the embedded or linked image to temporary directory
|
2020-08-30 12:29:46 +02:00
|
|
|
if os.name == "nt":
|
|
|
|
exportfile = "imagetracerjs.png"
|
|
|
|
else:
|
|
|
|
exportfile ="/tmp/imagetracerjs.png"
|
2021-04-23 15:21:30 +02:00
|
|
|
img.save(exportfile, "png")
|
2020-08-18 20:17:36 +02:00
|
|
|
|
|
|
|
nodeclipath = os.path.join("imagetracerjs-master", "nodecli", "nodecli.js")
|
|
|
|
|
|
|
|
## Build up imagetracerjs command according to your settings from extension GUI
|
|
|
|
command = "node " # "node.exe" or "node" on Windows or just "node" on Linux
|
|
|
|
if os.name=="nt": # your OS is Windows. We handle path separator as "\\" instead of unix-like "/"
|
|
|
|
command += str(nodeclipath).replace("\\", "\\\\")
|
|
|
|
else:
|
|
|
|
command += str(nodeclipath)
|
|
|
|
command += " " + exportfile
|
|
|
|
command += " ltres " + str(self.options.ltres)
|
|
|
|
command += " qtres " + str(self.options.qtres)
|
|
|
|
command += " pathomit " + str(self.options.pathomit)
|
|
|
|
command += " rightangleenhance " + str(self.options.rightangleenhance).lower()
|
|
|
|
command += " colorsampling " + str(self.options.colorsampling)
|
|
|
|
command += " numberofcolors " + str(self.options.numberofcolors)
|
|
|
|
command += " mincolorratio " + str(self.options.mincolorratio)
|
|
|
|
command += " numberofcolors " + str(self.options.numberofcolors)
|
|
|
|
command += " colorquantcycles " + str(self.options.colorquantcycles)
|
|
|
|
command += " layering " + str(self.options.layering)
|
|
|
|
command += " strokewidth " + str(self.options.strokewidth)
|
|
|
|
command += " linefilter " + str(self.options.linefilter).lower()
|
|
|
|
command += " scale " + str(self.options.scale)
|
|
|
|
command += " roundcoords " + str(self.options.roundcoords)
|
|
|
|
command += " viewbox " + str(self.options.viewbox).lower()
|
|
|
|
command += " desc " + str(self.options.desc).lower()
|
|
|
|
command += " blurradius " + str(self.options.blurradius)
|
|
|
|
command += " blurdelta " + str(self.options.blurdelta)
|
2021-04-23 15:21:30 +02:00
|
|
|
|
2020-08-18 20:17:36 +02:00
|
|
|
# Create the vector traced SVG file
|
|
|
|
with os.popen(command, "r") as tracerprocess:
|
|
|
|
result = tracerprocess.read()
|
2021-04-23 15:21:30 +02:00
|
|
|
if "was saved!" not in result:
|
2021-04-22 15:53:35 +02:00
|
|
|
self.msg("Error while processing input: " + result)
|
|
|
|
self.msg("Check the image file (maybe convert and save as new file) and try again.")
|
|
|
|
self.msg("\nYour parser command:")
|
|
|
|
self.msg(command)
|
|
|
|
|
2021-04-23 15:21:30 +02:00
|
|
|
|
2020-08-18 20:17:36 +02:00
|
|
|
# proceed if traced SVG file was successfully created
|
|
|
|
if os.path.exists(exportfile + ".svg"):
|
|
|
|
# Delete the temporary png file again because we do not need it anymore
|
|
|
|
if os.path.exists(exportfile):
|
|
|
|
os.remove(exportfile)
|
|
|
|
|
|
|
|
# new parse the SVG file and insert it as new group into the current document tree
|
|
|
|
doc = etree.parse(exportfile + ".svg").getroot()
|
|
|
|
newGroup = self.document.getroot().add(inkex.Group())
|
2021-03-09 13:39:53 +01:00
|
|
|
trace_width = None
|
|
|
|
trace_height = None
|
|
|
|
if doc.get('width') is not None and doc.get('height') is not None:
|
|
|
|
trace_width = doc.get('width')
|
|
|
|
trace_height = doc.get('height')
|
|
|
|
else:
|
|
|
|
viewBox = doc.get('viewBox') #eg "0 0 700 600"
|
|
|
|
trace_width = viewBox.split(' ')[2]
|
|
|
|
trace_height = viewBox.split(' ')[3]
|
2021-04-23 15:21:30 +02:00
|
|
|
|
|
|
|
img_w = image.get('width')
|
|
|
|
img_h = image.get('height')
|
|
|
|
img_x = image.get('x')
|
|
|
|
img_y = image.get('y')
|
|
|
|
|
|
|
|
if img_w is not None and img_h is not None and img_x is not None and img_y is not None:
|
|
|
|
transform = "matrix({:1.6f}, 0, 0, {:1.6f}, {:1.6f}, {:1.6f})"\
|
|
|
|
.format(float(img_w) / float(trace_width), float(img_h) / float(trace_height), float(img_x), float(img_y))
|
|
|
|
newGroup.attrib['transform'] = transform
|
|
|
|
|
|
|
|
for child in doc.getchildren():
|
|
|
|
newGroup.append(child)
|
|
|
|
|
2021-04-02 10:19:08 +02:00
|
|
|
# Delete the temporary svg file
|
2020-08-18 20:17:36 +02:00
|
|
|
if os.path.exists(exportfile + ".svg"):
|
|
|
|
os.remove(exportfile + ".svg")
|
|
|
|
|
|
|
|
#remove the old image or not
|
|
|
|
if self.options.keeporiginal is not True:
|
2021-04-23 15:21:30 +02:00
|
|
|
image.delete()
|
|
|
|
else:
|
|
|
|
self.msg("No images found in selection! Check if you selected a group instead.")
|
2020-08-18 20:17:36 +02:00
|
|
|
else:
|
2021-04-23 15:21:30 +02:00
|
|
|
self.msg("Selection is empty. Please select one or more images.")
|
2020-08-18 20:17:36 +02:00
|
|
|
|
2020-08-31 21:25:41 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
Imagetracerjs().run()
|