Friday, February 18, 2011

Exporting slices from Inkscape, part 2

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 slices layer, 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 foo will 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 layout.svg.

Run the script as follows:./export.py layout.svg
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.

3 comments:

I think replacing --export-png by --export-plain-svg should go a long way towards that. But maybe that simply makes it adjust the boundaries, without clipping or discarding (partly) invisible objects... dunno.

About Me

Me elsewhere

Taekwindow

Taekwindow is a small and light Windows program that I wrote. It enables you to move windows by Alt-dragging anywhere in their interior, and resizing by Alt-right-dragging, just like in many X window managers.