Skip to main content

Slice and scale 9-patch images

Project description

Slice Android style 9-patch images, resize and interactively preview them.

Travis CI

See for a 9-patch description.

Example image


If you want to use the interactive viewer read the additional installation notes under “Interactive viewer”.

$ pip install ninepatch

Python usage

from ninepatch import Ninepatch
ninepatch = Ninepatch('9patch_test.png')
print(ninepatch.fill_area)  # (left, top, right, bottom)
scaled_image = ninepatch.render(500, 400) # creates a new PIL image

Command line usage

Your image must be a PNG image with a transparent background. The scale and fill guide color must be 100% opaque black.

Scale and open image in a viewer (PIL

$ ninepatch render 9patch_test.png 300 300

Save the scaled image to a new file:

$ ninepatch render 9patch_test.png 300 300 scaled.png

Slice the 9patch into tiles:

$ ninepatch slice 9patch_test.png ./outputdir

Interactive viewer

ninepatch viewer screenshot

Interactively resize and preview an image in a Tkinter viewer:

$ ninepatch_viewer 9patch_test.png

or just:

$ ninepatch_viewer

without arguments to see the demo image

If you want to use the viewer then python-pil.imagetk has to be installed.

On Ubuntu do:

$ sudo apt-get install python-pil.imagetk

If you want to install into a virtualenv, pip needs the following packages to compile PIL with Tkinter support:

$  sudo apt-get install python-tk tk8.6-dev

(You can trigger a recompile of PIL with: “pip install -I ninepatch”)


  • new method export_slices()

  • changed command line parameters (render/slice)

  • missing guides are now handled properly

  • parse the fill area

  • switched to setuptools

  • added Tkinter viewer


I wrote this tool for the in my Clutter example project:



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ninepatch-0.1.11.tar.gz (12.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page