I previously wrote about how to export slices from an Inkscape file, using a bash script. Here's a better script, using Python, which doesn't require you to give your slices any special label.
The new process is as follows:
- First, draw your image as usual.
- Then, add a layer that will hold the slices; name it
slices(this is important). Set the layer's opacity to about 50% to be able to see what you're doing. Enable the grid, and make sure it is set to pixels: you want your slices to align with pixel boundaries.
- Draw your slice rectangles onto the
sliceslayer, aligned to the grid. Ensure that the rectangles have no border; the fill is irrelevant (I use red).
- Right-click a slice, and choose Object Properties. Change Id field to the name of the eventual PNG file, without the extension. The area defined by a rectangle named
foowill be saved to
foo.png. Repeat this for all slices.
- Hide the slices layer. If you forget this, the script will print a warning.
- Save your image. Let's say you called it
- Run the script as follows:
You should see each of your slices being exported to a PNG file in the same directory.
Here's the code for the script. Save this to a file
export.py and make it executable.
#!/usr/bin/env python import sys import os from xml.dom import minidom if len(sys.argv) < 2: print 'Usage: %s filename.svg' % sys.argv sys.exit(0) input_file = sys.argv dom = minidom.parse(input_file) groups = dom.getElementsByTagName('g') for group in groups: if group.getAttribute('inkscape:groupmode') == 'layer' and group.getAttribute('inkscape:label') == 'slices': if 'display:none' not in group.getAttribute('style'): print 'Warning: slices layer might still be visible' for element in group.getElementsByTagName('rect'): export_id = element.getAttribute('id') filename = '%s.png' % export_id print 'Exporting %s...' % filename os.system('inkscape --export-id="%s" --export-png="%s" --file="%s"' % (export_id, filename, input_file)) break else: print 'No layer named "slices" found; not exporting anything' sys.exit(1)