The Problem


We generate much of our artwork using SVG files, automatically converting them to PNG files of the size we need at different resolutions.  Converting them all to 9-patch format using the draw9patch program with Android is tedious and prone to error.  The problem gets worse the more icons we have.  We need a way to automate the flow.

The Solution

We've created the script mark9patch to do this.  It reads a PNG file, adds a 1 pixel border on all sides, and then sets the pixels you specify to #00000000.  The script reads the pixel list off the command line, so you can easily add it to a general automation script.

The Script

mark9patch is written in Tcl/Tk8.4 and uses the Img package to read and write PNG files.  The command-line specification is:


     mark9patch [[-x|-y][stretch|content] <ptslist>] <input file> [<output file>]


The stretch specification is on the left (x) and top (y) sides of the image, the content on the right (x) and bottom (y).  The points list is a comma-separated list (without spaces) of numbers or ranges, which are two numbers separated by a dash.  (The range includes both its endpoints).  If a side is not specified, then no marks are added to it.  Values are 0-based.  As a convenience, any substring of 'stretch' and 'content' can be used, including 's' and 'c'.


If the output file is not provided, then we add the suffix '.9.png' to the input file, after removing any suffix that already exists.


As an example, this command line will convert an 8 x 8 PNG to a 10 x 10 9-patch, adding the marked pixels to the periphery.  This isn't meant to be a valid spec!  It just indicates how the points list is parsed.


     mark9patch -xs 1,3-6,8 -ys 2-6 -xc 4-5 -yc 3,5,7 test.png


mark9patch example


Script with README in tarball / zip file.



If you have questions or comments about this script, or improvements, please contact us.