added enhancements for image placing in imagetracerjs and primitive

This commit is contained in:
Mario Voigt 2021-05-25 17:16:34 +02:00
parent 5232f7159b
commit 349729ee0a
2 changed files with 39 additions and 11 deletions

View File

@ -160,13 +160,28 @@ class Imagetracerjs (inkex.EffectExtension):
trace_height = viewBox.split(' ')[3] trace_height = viewBox.split(' ')[3]
# add transformation to fit previous XY coordinates and width/height # add transformation to fit previous XY coordinates and width/height
# image might also be influenced by other transformations from parent:
parent = image.getparent()
if parent is not None and parent != self.document.getroot():
tpc = parent.composed_transform()
x_offset = tpc.e
y_offset = tpc.f
else:
x_offset = 0.0
y_offset = 0.0
img_w = image.get('width') img_w = image.get('width')
img_h = image.get('height') img_h = image.get('height')
img_x = image.get('x') img_x = image.get('x')
img_y = image.get('y') 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: if img_w is not None and img_h is not None and img_x is not None and img_y is not None:
#if width/height are not unitless but end with px, mm, in etc. we have to convert to a float number
if img_w[-1].isdigit() is False:
img_w = self.svg.uutounit(img_w)
if img_h[-1].isdigit() is False:
img_h = self.svg.uutounit(img_h)
transform = "matrix({:1.6f}, 0, 0, {:1.6f}, {:1.6f}, {:1.6f})"\ 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)) .format(float(img_w) / float(trace_width), float(img_h) / float(trace_height), float(img_x) + x_offset, float(img_y) + y_offset)
newGroup.attrib['transform'] = transform newGroup.attrib['transform'] = transform
else: else:
t = image.composed_transform() t = image.composed_transform()
@ -175,7 +190,7 @@ class Imagetracerjs (inkex.EffectExtension):
img_x = t.e img_x = t.e
img_y = t.f img_y = t.f
transform = "matrix({:1.6f}, 0, 0, {:1.6f}, {:1.6f}, {:1.6f})"\ 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)) .format(float(img_w) / float(trace_width), float(img_h) / float(trace_height), float(img_x) + x_offset, float(img_y) + y_offset)
newGroup.attrib['transform'] = transform newGroup.attrib['transform'] = transform
for child in doc.getchildren(): for child in doc.getchildren():

View File

@ -89,6 +89,19 @@ 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:
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
parent = node.getparent()
if parent is not None and parent != self.document.getroot():
tpc = parent.composed_transform()
x_offset = tpc.e
y_offset = tpc.f
else:
x_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":
@ -130,16 +143,16 @@ 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()
newGroup = self.document.getroot().add(inkex.Group()) newGroup = self.document.getroot().add(inkex.Group())
newGroup.attrib['transform'] = "matrix(" + \ newGroup.attrib['transform'] = "matrix({:0.6f}, 0, 0, {:0.6f}, {:0.6f}, {:0.6f})".format(
str(float(node.get('width')) / float(doc.get('width'))) + \ float(node.get('width')) / float(doc.get('width')),
", 0, 0 , " + \ float(node.get('height')) / float(doc.get('height')),
str(float(node.get('height')) / float(doc.get('height'))) + \ float(node.get('x')) + x_offset,
"," + node.get('x') + \ float(node.get('y')) + y_offset
"," + node.get('y') + ")" )
newGroup.append(doc) newGroup.append(doc)
# Delete the temporary svg file # Delete the temporary svg file