I hope this is the right forum to ask this. I have a query about command line actions.
Here's the problem: I'd like to write a script (Python, or a shell script) to take a source SVG and...
collapse any whitespace border around the image;
rescale the 'cropped' image so its width or height is 1000 along the longest side, with the other side scaled to maintain the aspect ratio;
save the result out as a new SVG file.
FYI, here's a detailed explanation why: I need to manipulate SVG diagrams in JavaScript after they have been loaded into a web app. Each drawing is a floor plan, and I'm injecting elements and making parts of the drawing clickable. There's going to be dozens of these floor plan drawings, so I need to come up with a way to normalise/standardise the SVG files as they arrive from the graphic designer who is drawing them. I tried to explain to the designer that I needed drawings without any border padding, and where the coordinates inside every file use a standard scale (I chose 1000 units along the longest side.) This is just to make my code simpler and easier to maintain, by avoiding a lot of tricky math when manipulating the drawings. The designer, however, isn't really used to preparing images for software consumption, so naturally he didn't really follow my instructions. I could insist he modify his files, but I figured it might be a better long-term solution to devise a quick script that can 'standardise' any SVG to my specifications, at least to fix likely common issues.
I know that I can do --query-width and --query-height to read the width and height of an SVG. I have also managed to use the following to strip off the border whitespace: select-all;page-fit-to-selection;export-type:svg;export-plain-svg;export-do
What I'm struggling to work out is how to scale the image so its coordinates fit within given dimensions. I realise I can set the page/canvas size to a given size, but I need to resize the drawing as well as just the canvas.
Btw, this doesn't have to all be done using a single call to Inkscape. Multiple calls with temporary intermediary files along the way is fine, if that makes the solution easier.
EDIT: One problem I'm having is that some of these action commands seem to just add transform/translate operations into the SVG groups, rather than amending the actual coordinates and sizes of each child. (The browser does the heavy lifting of shifting and scaling the coordinate spaces when drawing the SVG, but if you want to work with the SVG DOM via JavaScript, it adds a lot of complications when working out coordinates.)
Any help, or pointers towards relevant resources, gratefully received. Thanks,
I hope this is the right forum to ask this. I have a query about command line actions.
Here's the problem: I'd like to write a script (Python, or a shell script) to take a source SVG and...
FYI, here's a detailed explanation why: I need to manipulate SVG diagrams in JavaScript after they have been loaded into a web app. Each drawing is a floor plan, and I'm injecting elements and making parts of the drawing clickable. There's going to be dozens of these floor plan drawings, so I need to come up with a way to normalise/standardise the SVG files as they arrive from the graphic designer who is drawing them. I tried to explain to the designer that I needed drawings without any border padding, and where the coordinates inside every file use a standard scale (I chose 1000 units along the longest side.) This is just to make my code simpler and easier to maintain, by avoiding a lot of tricky math when manipulating the drawings. The designer, however, isn't really used to preparing images for software consumption, so naturally he didn't really follow my instructions. I could insist he modify his files, but I figured it might be a better long-term solution to devise a quick script that can 'standardise' any SVG to my specifications, at least to fix likely common issues.
I know that I can do
--query-width
and--query-height
to read the width and height of an SVG. I have also managed to use the following to strip off the border whitespace:select-all;page-fit-to-selection;export-type:svg;export-plain-svg;export-do
What I'm struggling to work out is how to scale the image so its coordinates fit within given dimensions. I realise I can set the page/canvas size to a given size, but I need to resize the drawing as well as just the canvas.
Btw, this doesn't have to all be done using a single call to Inkscape. Multiple calls with temporary intermediary files along the way is fine, if that makes the solution easier.
EDIT: One problem I'm having is that some of these action commands seem to just add transform/translate operations into the SVG groups, rather than amending the actual coordinates and sizes of each child. (The browser does the heavy lifting of shifting and scaling the coordinate spaces when drawing the SVG, but if you want to work with the SVG DOM via JavaScript, it adds a lot of complications when working out coordinates.)
Any help, or pointers towards relevant resources, gratefully received. Thanks,
-SEM-
Have a look at this script I just posted.
https://inkscape.org/forums/questions/pad-image-using-command-line-actions/
It uses the dummy rectangle trick, then deletes the rectangle later.