With this code the image isn't copied to 500/300, it is copied relative to it's old coordinates.
- Is there a way to get the coordinates of the image so that I can add this values to my Pos_New? or - Is there a way to copy the image absolute to the new position?
In order to copy an image to an absolute position in Inkscape using Python, you need to consider the current position of the image and calculate the relative translation to the new position. Here's an example of how you can achieve this:
import copy from svgpathtools import Transform
# Define the symbol ID of the image you want to copy symbol_id = 'IMG1'
# Get the SVG element of the image symbol = self.svg.getElementById(symbol_id)
# Get the current position of the image current_pos = symbol.get('transform')
# Extract the translation values from the current position current_translate = Transform(current_pos).translate
# Define the new absolute position where you want to copy the image new_pos = (500, 300)
# Calculate the relative translation from the current position to the new position translation_x = new_pos[0] - current_translate.real translation_y = new_pos[1] - current_translate.imag
# Create a new copy of the image symbol_new = copy.deepcopy(symbol)
# Apply the relative translation to the new copy of the image symbol_new.transform = Transform(translate=(translation_x, translation_y)) @ symbol_new.transform
# Add the new copy of the image to the SVG self.svg.add(symbol_new) In this code, we extract the current translation values from the image's transform attribute using the Transform class from the svgpathtools library. Then we calculate the relative translation by subtracting the current translation from the new position. Finally, we apply the relative translation to the new copy of the image and add it to the SVG.
Hi all,
I have an inkscape chart with some imported png images.
Now I want to copy one of the images to another position.
If I do it in this way I have a problem:
symbol = 'IMG1'
Pos_New = Transform(translate=(500, 300))
symbol_new = copy.deepcopy(self.svg.getElementById(symbol))
symbol_new.transform = Transform(Pos_New) @ symbol_new.transform
With this code the image isn't copied to 500/300, it is copied relative to it's old coordinates.
- Is there a way to get the coordinates of the image so that I can add this values to my Pos_New? or
- Is there a way to copy the image absolute to the new position?
Regards,
bernie70
In order to copy an image to an absolute position in Inkscape using Python, you need to consider the current position of the image and calculate the relative translation to the new position. Here's an example of how you can achieve this:
import copy
from svgpathtools import Transform
# Define the symbol ID of the image you want to copy
symbol_id = 'IMG1'
# Get the SVG element of the image
symbol = self.svg.getElementById(symbol_id)
# Get the current position of the image
current_pos = symbol.get('transform')
# Extract the translation values from the current position
current_translate = Transform(current_pos).translate
# Define the new absolute position where you want to copy the image
new_pos = (500, 300)
# Calculate the relative translation from the current position to the new position
translation_x = new_pos[0] - current_translate.real
translation_y = new_pos[1] - current_translate.imag
# Create a new copy of the image
symbol_new = copy.deepcopy(symbol)
# Apply the relative translation to the new copy of the image
symbol_new.transform = Transform(translate=(translation_x, translation_y)) @ symbol_new.transform
# Add the new copy of the image to the SVG
self.svg.add(symbol_new)
In this code, we extract the current translation values from the image's transform attribute using the
Transform
class from thesvgpathtools
library. Then we calculate the relative translation by subtracting the current translation from the new position. Finally, we apply the relative translation to the new copy of the image and add it to the SVG.Thank you for this - it works fine.
How can I do the same with an in inkscape drawn (and grouped) object? This object has no x- or y attribute.