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