8

I have a downloaded vector file (AI format). The file contains simple vector images of various dog breeds. I've loaded the file in inkscape,ungrouped them and would like a way to save out each dog vector as a separate file. Is there a way to automate this with Inkscape or some other OS/Freeware tool? Otherwise I'll have to select each and Save as. Any help is most appreciated!

UPDATE Thanks to JSBueno for the advice on editing the actual SVG file. I didn't realize that SVG was just an XML file. I should be able to easily walk through the file and export out each dog as a separate file. This, by the way, will be way faster than doing it through the interface which was the goal of this post. Thanks to all for the advice.

  • Why can't you select each and save as? Are there really so many images that an automated solution is mandatory? – Scott Apr 23 '15 at 15:12
  • 2
    To be able to automate this you'd have to be able to define "dog" elements to extract from the svg file. In reality your dog elements will consist of single compound lines all the way up to clipped groups. I suggest you learn the keyboard shortcuts required, select some music and get on with it. – user19660 Apr 23 '15 at 15:47
  • Yes, I have 80+ individual objects for this one. I'm just looking for a faster way but will do it manually if that's what it takes. – dinki Apr 23 '15 at 18:01
  • You can certainly script that. It would take someone skilled in Python, for example, at least the time required to "save as" 30 of your 80 dogs to create such a script. An unskilled coder, or even someone skilled but not acquainted with inkscape helerp functions/XML could take far more time than saving the 80 dogs. – jsbueno Apr 24 '15 at 13:09
  • Ultimatelly, you could just pen the SVG file in a text editor (Notepad ++), and check if each dog is contained in a separate SVG tag - tehm itis amatter of copying/pasting text instead of doing it with images on the inkscape UI. It still might be faster using the inkscape UI. – jsbueno Apr 24 '15 at 13:10
5

There are two solutions that I have found for exporting multiple objects from Inkscape as SVGs. These methods have the benefit of being scripts that can be run automatically and don't need editing of SVGs in text editors.

  1. svg-objects-export

The svg-objects-export script works by exporting all objects based on regular expressions. In your case it would work by exporting all objects with a particular ID. To use it first clone or download the script from Github.

In Inkscape you need to change the ID of all the objects you want exporting to have the same filename pattern. To do this open the Object Properties dialogue by going to Object > Object Properties or pressing Ctrl+Shift+O. In the ID field change them all to have the same basename ID e.g. Dog_1, Dog_2, Dog_3 etc (objects cannot have the same ID).

enter image description here

With the file saved and the script downloaded, in a terminal run the following command

python svg-objects-export.py --pattern 'Dog_*' /path/to/your/file.svg

This will export all of the objects with the basename ID "Dog_".

  1. inx-exportobjects

The inx-exportobjects inkscape extension will simply export all selected objects. To use it first clone or download the extension from Gitlab. To install it copy the files export_objects.inx and export_objects.py to .config/inkscape/extensions/. In Inkscape you'll have an option called "Objects to Files..." in Extensions > Exports. Select the objects you want to install and select this option.

enter image description here

In this dialogue box simply change the Export Directory to the place where you want to save your files and change the Basename to the new name of the files and then press Apply.

enter image description here

The files will be exported to your specified directory.

Each of the solutions have many other options, such as animated gif export, which you should definitely experiment with.

  • Excellent solution @hellocatfood Just a tip: Before you export, you can resize the canvas. In my case, I had many icons. I found out the max-width and max-height of the icons. And then resized the canvas to respective height x width. Next, select all the objects, and align them horizontally and vertically, and then start the export process. This way, you will have the svg's with proper height and width. – Anish Nair Feb 27 '18 at 7:07
1

Depending on the precondition, that all your objects are grouped and not combined into bigger groups themselves, here is how to do it. (I used cats in the example, because dogs were not available).

enter image description here In the lower right you see a highlighted cat, object id 5358, which is a single path, so I added a green floor later which changed it to object 5364 which will occur later, so don't let confuse you. :)

You get the xml-editor by hitting button 4 from the right (arrow 1).

I changed g5358 to g-cat-5358 (field 4) to make it easy for a texttool to pick these objects which works for most elements which don't collide with xml-svg-names (color, meta, id, ...).

In the left window you see, that there is already a renamed entity, g-cat-5312 close to the top at the same indentation level.

Inkscape organizes unique ids for groups, but they don't need to be of the form gNUMBER, you can mixin other characters. Just don't pick an already existing identifier.

After changing the identifier, hit 'setzen (set)' (5) to confirm your change. You see a german interface here, YMMV.

Save the svg (as demo-cats.svg). We now switch to scripting.

for cat in $( grep g-cat- demo-cats.svg )
do
  id=${cat//[^0-9]/}
  echo $id
  (head -n 53 demo-cats.svg; sed -n "/$cat/,/^    <\/g>/p" demo-cats.svg; echo "</svg>" ) > cat-${id}.svg
done

What does it do? It greps (search with grep) for the "g-cat-" pattern which is the start of our interesting section. id=${cat//[^0-9]/} extracts the id in bash from the expression. Echo prints it to the screen which should help to find the bug if something goes wrong. head -n 53 demo-cats.svg takes the first 53 lines from the svg, which contains all the svg-boilerplate. Depending on your settings with different svg-formats (inkscape, compressed, normal svg, inkscape-compressed) this might vary. With nl demo-cats.svg | less you can numerate the lines, to find the right cutting point in your case.

  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
     <g

^Up to this part, the lines should be included.

The next command is done with the stream editor sed: sed -n "/$cat/,/^ <\/g>/p" demo-cats.svg; says (-n) no printing (but) searche for the cat group, up to the closing tag g at indentation level 2 which is 4 spaces before the </g> if you didn't change Inkscape preferences, which you might remember if you did. Then add the closing tag for svg: echo "</svg>". Write the output to the file 'cat-$id.svg' (this overwrites silently existing files).

So I end up with two files: cat-5312.svg and cat-5364.svg .

A more elegant solution should be possible with xmlstarlet or similar tools, which are made to search and edit xml-files. Unfortunately I'm not used to it and did't get it working after reading 10 minutes of documentation. You wouldn't need to rename the groups in the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.