Simple SVG editing (like combining two images) in batch mode?
Is it possible to use inkscape (if not, something else) to do simple SVG editing, like combining two images into one? So one can script it...
My typical use case is - I have collection of images and I need to generate their versions on a few different backgrounds. Doing it manually is very tedious.
With bitmaps Image::Magick works great, sth like $img->Read(picture); $img2->Read(background); $img2->Composite(image => $img, gravity => 'center'); $img2->Write(output) and I am done (here it is perl scripting, but that's not very important).
Any chance for something similar with SVG?
Hi
There are Perl modules to manipulate SVG, but you if you want to do it through Inkscape, I can't help here. http://search.cpan.org/modlist/Graphics
Tibo
On 10/25/07, Marcin Kasperski <Marcin.Kasperski@...2379...> wrote:
Is it possible to use inkscape (if not, something else) to do simple SVG editing, like combining two images into one? So one can script it...
My typical use case is - I have collection of images and I need to generate their versions on a few different backgrounds. Doing it manually is very tedious.
With bitmaps Image::Magick works great, sth like $img->Read(picture); $img2->Read(background); $img2->Composite(image => $img, gravity => 'center'); $img2->Write(output) and I am done (here it is perl scripting, but that's not very important).
Any chance for something similar with SVG?
This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Inkscape-user mailing list Inkscape-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-user
There are Perl modules to manipulate SVG,
You mean this one: http://search.cpan.org/~ronan/SVG-2.36/lib/SVG/Manual.pm ? I must take a closer look...
but you if you want to do it through Inkscape, I can't help here.
That's not hard to notice that inkscape does not have command line options for the purpose, but I hoped that maybe it would be possible to use some remote objects protocol to instrument it...
Any chance for something similar with SVG?
Any reason why you can't go SVG -> alpha PNG -> Imagemagick -> repeat ?
I mean, prepare two pngs with transparent backgrounds from 2 SVG files (via Inkscape command line) and then merge them any way you like.
Alternatives are - perhaps - looking at Cairo and libSVG. Haven't gone there myself.
/d
Donn <donn.ingle@...155...> writes:
Any chance for something similar with SVG?
Any reason why you can't go SVG -> alpha PNG -> Imagemagick -> repeat ?
I mean, prepare two pngs with transparent backgrounds from 2 SVG files (via Inkscape command line) and then merge them any way you like.
This is what I do. But then the result is some PNG file. I ask the question because I would like to get SVG result....
This is what I do. But then the result is some PNG file. I ask the question because I would like to get SVG result....
Oh sorry, I should read the post first! Still, check Cairo out, it has potential.
Otherwise ... could you not merge the svg files with some xml voodoo? Put the additional files onto new layers above/below the first file. You'd have to figure out how Inkscape names all it's nodes etc, but at least it's all text! Save to a new svg and crank that thru Inkscape.
/d
I generally do something similar in PHP, but as i simply call inkscape executable you should be able to do the same with every scripting language that supports shell command execution (system, exec and similar).
For your case, it seems you simply need to nest 2 different SVGS into one, placing them accordingly.
A generic syntax is in this form:
<svg width="[SVG_W]in" height="[SVG_H]in"> <g transform="translate(0,0)"> [SVG FILE 1] </g> <g transform="translate(0,0)"> [SVG FILE 2] </g> </svg>
There are some things o keep in mind anyhow. This general form will show an effect like gravity="NortWest" in Magick. If you want to simulate the gravity="center" effect, you will have to alter the "translate" property of the SVG nested images. But again, remember that vectors may generally be sizeless, so to calc the sizes correctly you will first need to determine your [SVG FILE 1] and [SVG FILE 2] width and height. To do this i generally use a small trick and i create aspect ratios. Here's the full steps i'd suggest (combining inkscape and magick):
Let's say what you know are the SVG filenames, call the [SVG FILE 1] and [SVG FILE 2] [SVG FILE 1] is the BG image.
----------------- inkscape -z -d 90 -e temp.png [SVG FILE 1] [SVG FILE 1 W] = identify -format "%w" temp.png [SVG FILE 1 H] = identify -format "%h" temp.png
inkscape -z -d 90 -e temp.png [SVG FILE 2] [SVG FILE 2 W] = identify -format "%w" temp.png [SVG FILE 2 H] = identify -format "%h" temp.png
delete temp.png -----------------
This first part will give you real sizes of both images at 90 DPI. It's just something to calc on real numbers. Next you need to alter my initial generic synatx in this way:
-----------------
[SVG_W]=max([SVG FILE 1 W],[SVG FILE 2 W]) [SVG_H]=max([SVG FILE 1 H],[SVG FILE 2 H]) [SVG FILE 1 X]=([SVG_W]-[SVG FILE 1 W])/2 [SVG FILE 1 Y]=([SVG_H]-[SVG FILE 1 H])/2 [SVG FILE 2 X]=([SVG_W]-[SVG FILE 2 W])/2 [SVG FILE 2 Y]=([SVG_H]-[SVG FILE 2 H])/2
<svg width="[SVG_W]in" height="[SVG_H]in"> <g transform="translate([SVG FILE 1 X],[SVG FILE 1 Y])"> [SVG FILE 1] </g> <g transform="translate([SVG FILE 2 X],[SVG FILE 2 Y])"> [SVG FILE 2] </g> </svg>
-----------------
Hope this helps,
Alex
Marcin Kasperski wrote:
Is it possible to use inkscape (if not, something else) to do simple SVG editing, like combining two images into one? So one can script it...
My typical use case is - I have collection of images and I need to generate their versions on a few different backgrounds. Doing it manually is very tedious.
With bitmaps Image::Magick works great, sth like $img->Read(picture); $img2->Read(background); $img2->Composite(image => $img, gravity => 'center'); $img2->Write(output) and I am done (here it is perl scripting, but that's not very important).
Any chance for something similar with SVG?
This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Inkscape-user mailing list Inkscape-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-user
Sorry if i'm sending this twice, not sure the prev sending went fine (i didn't received it).
I generally do something similar in PHP, but as i simply call inkscape executable you should be able to do the same with every scripting language that supports shell command execution (system, exec and similar).
For your case, it seems you simply need to nest 2 different SVGS into one, placing them accordingly.
A generic syntax is in this form:
<svg width="[SVG_W]in" height="[SVG_H]in"> <g transform="translate(0,0)"> [SVG FILE 1] </g> <g transform="translate(0,0)"> [SVG FILE 2] </g> </svg>
There are some things o keep in mind anyhow. This general form will show an effect like gravity="NortWest" in Magick. If you want to simulate the gravity="center" effect, you will have to alter the "translate" property of the SVG nested images. But again, remember that vectors may generally be sizeless, so to calc the sizes correctly you will first need to determine your [SVG FILE 1] and [SVG FILE 2] width and height. To do this i generally use a small trick and i create aspect ratios. Here's the full steps i'd suggest (combining inkscape and magick):
Let's say what you know are the SVG filenames, call the [SVG FILE 1] and [SVG FILE 2] [SVG FILE 1] is the BG image.
----------------- inkscape -z -d 90 -e temp.png [SVG FILE 1] [SVG FILE 1 W] = identify -format "%w" temp.png [SVG FILE 1 H] = identify -format "%h" temp.png
inkscape -z -d 90 -e temp.png [SVG FILE 2] [SVG FILE 2 W] = identify -format "%w" temp.png [SVG FILE 2 H] = identify -format "%h" temp.png
delete temp.png -----------------
This first part will give you real sizes of both images at 90 DPI. It's just something to calc on real numbers. Next you need to alter my initial generic synatx in this way:
-----------------
[SVG_W]=max([SVG FILE 1 W],[SVG FILE 2 W]) [SVG_H]=max([SVG FILE 1 H],[SVG FILE 2 H]) [SVG FILE 1 X]=([SVG_W]-[SVG FILE 1 W])/2 [SVG FILE 1 Y]=([SVG_H]-[SVG FILE 1 H])/2 [SVG FILE 2 X]=([SVG_W]-[SVG FILE 2 W])/2 [SVG FILE 2 Y]=([SVG_H]-[SVG FILE 2 H])/2
<svg width="[SVG_W]in" height="[SVG_H]in"> <g transform="translate([SVG FILE 1 X],[SVG FILE 1 Y])"> [SVG FILE 1] </g> <g transform="translate([SVG FILE 2 X],[SVG FILE 2 Y])"> [SVG FILE 2] </g> </svg>
-----------------
Hope this helps,
Alex
Marcin Kasperski wrote:
Is it possible to use inkscape (if not, something else) to do simple SVG editing, like combining two images into one? So one can script it...
My typical use case is - I have collection of images and I need to generate their versions on a few different backgrounds. Doing it manually is very tedious.
With bitmaps Image::Magick works great, sth like $img->Read(picture); $img2->Read(background); $img2->Composite(image => $img, gravity => 'center'); $img2->Write(output) and I am done (here it is perl scripting, but that's not very important).
Any chance for something similar with SVG?
This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Inkscape-user mailing list Inkscape-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-user
Marcin Kasperski wrote:
Is it possible to use inkscape (if not, something else) to do simple SVG editing, like combining two images into one? So one can script it...
My typical use case is - I have collection of images and I need to generate their versions on a few different backgrounds. Doing it manually is very tedious.
With bitmaps Image::Magick works great, sth like $img->Read(picture); $img2->Read(background); $img2->Composite(image => $img, gravity => 'center'); $img2->Write(output) and I am done (here it is perl scripting, but that's not very important).
Any chance for something similar with SVG?
SVG is XML. So any XML processing library in your favorite language should allow you to do this. Simply copy the elements in the root of each SVG into the root of a third empty SVG. There is however the issue of id collision. (Id collision is an issue we haven't properly solved in Inkscape's copy and paste code yet either.) My prefered tools for doing something like this would be python with the fantastic lxml library.
A very simplistic (and untested) attempt (without id collision resolution) in python might look something like:
<code> from lxml import etree
files_to_combine = ['file1.svg','file2.svg'] output_file = open('output.svg','w')
base_svg_text = ''' <svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" > </svg> '''
base_svg = etree.fromstring(base_svg_text)
for filename in files_to_combine: text = open(part_filename,'r').read() for element in etree.fromstring(text): base_svg.append(element)
output_file.write(etree.tostring(base_svg, pretty_print=True)) </code>
And oddly enough the guys in the Memaker project (https://launchpad.net/memaker) are working to solve this problem right now. So you can always check back to their code for new method refinements.
Aaron Spike
participants (5)
-
Aaron Spike
-
Alexander Bonivento
-
Donn
-
Marcin Kasperski
-
Thibaut Raballand