small fix for primitive

This commit is contained in:
Mario Voigt 2023-08-19 17:46:09 +02:00
parent 85f3281a53
commit 49de97160c
1 changed files with 29 additions and 29 deletions

View File

@ -14,7 +14,7 @@ from inkex import Color
Extension for InkScape 1.X Extension for InkScape 1.X
Features Features
- Primitive - Reproducing images with geometric primitives written in Go. - Primitive - Reproducing images with geometric primitives written in Go.
Author: Mario Voigt / FabLab Chemnitz Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org Mail: mario.voigt@stadtfabrikanten.org
Date: 21.08.2020 Date: 21.08.2020
@ -30,7 +30,7 @@ class Primitive (inkex.EffectExtension):
longcolor = int(pickerColor) longcolor = int(pickerColor)
if longcolor < 0: if longcolor < 0:
longcolor = longcolor & 0xFFFFFFFF longcolor = longcolor & 0xFFFFFFFF
return '#' + format(longcolor >> 8, '06X') return format(longcolor >> 8, '06X')
def checkImagePath(self, node): def checkImagePath(self, node):
xlink = node.get('xlink:href') xlink = node.get('xlink:href')
@ -54,7 +54,7 @@ class Primitive (inkex.EffectExtension):
if (os.path.isfile(path)): if (os.path.isfile(path)):
return path return path
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument("--tab") pars.add_argument("--tab")
pars.add_argument("--keeporiginal", type=inkex.Boolean, default=False, help="Keep original image on canvas") pars.add_argument("--keeporiginal", type=inkex.Boolean, default=False, help="Keep original image on canvas")
@ -63,14 +63,14 @@ class Primitive (inkex.EffectExtension):
pars.add_argument("--m", default=1, help="Mode") pars.add_argument("--m", default=1, help="Mode")
pars.add_argument("--rep", type=int, default=0,help="Extra shapes/iteration") pars.add_argument("--rep", type=int, default=0,help="Extra shapes/iteration")
pars.add_argument("--r", type=int, default=256, help="Resize to size before processing (px)") pars.add_argument("--r", type=int, default=256, help="Resize to size before processing (px)")
pars.add_argument("--s", type=int, default=1024, help="Output image size (px)") pars.add_argument("--s", type=int, default=1024, help="Output image size (px)")
pars.add_argument("--a", type=int, default=128, help="Color alpha") pars.add_argument("--a", type=int, default=128, help="Color alpha")
pars.add_argument("--bg_enabled", type=inkex.Boolean, default=True, help="Use average starting background color") pars.add_argument("--bg_enabled", type=inkex.Boolean, default=True, help="Use average starting background color")
pars.add_argument("--bg", type=Color, default=255, help="Starting background color") pars.add_argument("--bg", type=Color, default=255, help="Starting background color")
pars.add_argument("--j", type=int, default=0, help="Number of parallel workers") pars.add_argument("--j", type=int, default=0, help="Number of parallel workers")
def effect(self): def effect(self):
if (self.options.ids): if (self.options.ids):
for node in self.svg.selected.values(): for node in self.svg.selected.values():
if node.tag == inkex.addNS('image', 'svg'): if node.tag == inkex.addNS('image', 'svg'):
@ -86,11 +86,11 @@ class Primitive (inkex.EffectExtension):
image = Image.open(BytesIO(base64.b64decode(image_string[i + 1:len(image_string)]))) image = Image.open(BytesIO(base64.b64decode(image_string[i + 1:len(image_string)])))
else: else:
image = Image.open(self.path) image = Image.open(self.path)
if node.get('width')[-1].isdigit() is False or node.get('height')[-1].isdigit() is False: if node.get('width')[-1].isdigit() is False or node.get('height')[-1].isdigit() is False:
inkex.utils.debug("Image seems to have some weird dimensions in XML structure. Please remove units from width and height attributes at <svg:image>") inkex.utils.debug("Image seems to have some weird dimensions in XML structure. Please remove units from width and height attributes at <svg:image>")
return return
parent = node.getparent() parent = node.getparent()
if parent is not None and parent != self.document.getroot(): if parent is not None and parent != self.document.getroot():
tpc = parent.composed_transform() tpc = parent.composed_transform()
@ -98,18 +98,18 @@ class Primitive (inkex.EffectExtension):
y_offset = tpc.f y_offset = tpc.f
else: else:
x_offset = 0.0 x_offset = 0.0
y_offset = 0.0 y_offset = 0.0
# Write the embedded or linked image to temporary directory # Write the embedded or linked image to temporary directory
if os.name == "nt": if os.name == "nt":
exportfile = "Primitive.png" exportfile = "Primitive.png"
else: else:
exportfile = "/tmp/Primitive.png" exportfile = "/tmp/Primitive.png"
if image.mode != 'RGB': if image.mode != 'RGB':
image = image.convert('RGB') image = image.convert('RGB')
image.save(exportfile, "png") image.save(exportfile, "png")
## Build up Primitive command according to your settings from extension GUI ## Build up Primitive command according to your settings from extension GUI
if os.name == "nt": if os.name == "nt":
command = "primitive" command = "primitive"
@ -118,17 +118,17 @@ class Primitive (inkex.EffectExtension):
command += " -m " + str(self.options.m) command += " -m " + str(self.options.m)
command += " -rep " + str(self.options.rep) command += " -rep " + str(self.options.rep)
command += " -r " + str(self.options.r) command += " -r " + str(self.options.r)
command += " -s " + str(self.options.s) command += " -s " + str(self.options.s)
command += " -a " + str(self.options.a) command += " -a " + str(self.options.a)
if not self.options.bg_enabled: if self.options.bg_enabled:
command += " -bg " + self.rgbToHex(self.options.bg) command += " -bg " + self.rgbToHex(self.options.bg)
command += " -j " + str(self.options.j) command += " -j " + str(self.options.j)
command += " -i " + exportfile command += " -i " + exportfile
command += " -o " + exportfile + ".svg" command += " -o " + exportfile + ".svg"
command += " -n " + str(self.options.n) command += " -n " + str(self.options.n)
#inkex.utils.debug(command) #inkex.utils.debug(command)
# Create the vector new SVG file # Create the vector new SVG file
with os.popen(command, "r") as proc: with os.popen(command, "r") as proc:
result = proc.read() result = proc.read()
@ -140,7 +140,7 @@ class Primitive (inkex.EffectExtension):
# Delete the temporary png file again because we do not need it anymore # Delete the temporary png file again because we do not need it anymore
if os.path.exists(exportfile): if os.path.exists(exportfile):
os.remove(exportfile) os.remove(exportfile)
# new parse the SVG file and insert it as new group into the current document tree # new parse the SVG file and insert it as new group into the current document tree
doc = etree.parse(exportfile + ".svg").getroot() doc = etree.parse(exportfile + ".svg").getroot()
x = node.get('x') x = node.get('x')
@ -149,15 +149,15 @@ class Primitive (inkex.EffectExtension):
y = node.get('y') y = node.get('y')
if y is None: if y is None:
y = "0" y = "0"
newGroup = self.document.getroot().add(inkex.Group()) newGroup = self.document.getroot().add(inkex.Group())
newGroup.attrib['transform'] = "matrix({:0.6f}, 0, 0, {:0.6f}, {:0.6f}, {:0.6f})".format( newGroup.attrib['transform'] = "matrix({:0.6f}, 0, 0, {:0.6f}, {:0.6f}, {:0.6f})".format(
float(node.get('width')) / float(doc.get('width')), float(node.get('width')) / float(doc.get('width')),
float(node.get('height')) / float(doc.get('height')), float(node.get('height')) / float(doc.get('height')),
float(x) + x_offset, float(x) + x_offset,
float(y) + y_offset float(y) + y_offset
) )
for children in doc: for children in doc:
newGroup.append(children) newGroup.append(children)
@ -167,11 +167,11 @@ class Primitive (inkex.EffectExtension):
os.remove(exportfile + ".svg") os.remove(exportfile + ".svg")
except: except:
pass pass
else: else:
inkex.utils.debug("Error while creating output file! :-( The \"primitive\" executable seems to be missing, has no exec permissions or platform is imcompatible.") inkex.utils.debug("Error while creating output file! :-( The \"primitive\" executable seems to be missing, has no exec permissions or platform is imcompatible.")
exit(1) exit(1)
#remove the old image or not #remove the old image or not
if self.options.keeporiginal is not True: if self.options.keeporiginal is not True:
node.delete() node.delete()
@ -185,9 +185,9 @@ class Primitive (inkex.EffectExtension):
clipPath = etree.SubElement(defs, 'clipPath', clipPathData) clipPath = etree.SubElement(defs, 'clipPath', clipPathData)
#inkex.utils.debug(image.width) #inkex.utils.debug(image.width)
clipBox = { clipBox = {
'x':str(0), 'x':str(0),
'y':str(0), 'y':str(0),
'width':str(doc.get('width')), 'width':str(doc.get('width')),
'height':str(doc.get('height')), 'height':str(doc.get('height')),
'style':'fill:#000000; stroke:none; fill-opacity:1;' 'style':'fill:#000000; stroke:none; fill-opacity:1;'
} }
@ -195,7 +195,7 @@ class Primitive (inkex.EffectExtension):
#etree.SubElement(newGroup, 'g', {inkex.addNS('label','inkscape'):'imagetracerjs', 'clip-path':"url(#imagetracerClipPath)"}) #etree.SubElement(newGroup, 'g', {inkex.addNS('label','inkscape'):'imagetracerjs', 'clip-path':"url(#imagetracerClipPath)"})
newGroup.set('clip-path','url(#imagetracerClipPath)') newGroup.set('clip-path','url(#imagetracerClipPath)')
else: else:
inkex.utils.debug("No image found for tracing. Please select an image first.") inkex.utils.debug("No image found for tracing. Please select an image first.")
if __name__ == '__main__': if __name__ == '__main__':
Primitive().run() Primitive().run()