Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Slice and scale 9-patch images

Project Description

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

See for a 9-patch description.


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('ninepatch_bubble.9.png')
print(ninepatch.content_area)  # content_area(left=23, top=20, right=27, bottom=59)

# render the image to a specific size
scaled_image = ninepatch.render(500, 400) # creates a new PIL image

# render the image so it's content area fits (width, height)
image_fit = ninepatch.render_fit(300, 200)

# render the image so it wraps another PIL image
image_to_wrap ='image_to_wrap.png')
wrapped_image = ninepatch.render_wrap(image_to_wrap)

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 ninepatch_bubble.9.png 300 300

Save the scaled image to a new file:

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

Render an image so it’s content area fits a given width and height

$ ninepatch fit ninepatch_bubble.9.png 150 150 fit.png

Render an image to include another image

$ ninepatch wrap ninepatch_bubble.9.png image_to_wrap.png wrapped.png

Slice the 9patch into tiles:

$ ninepatch slice ninepatch_bubble.9.png ./outputdir

Interactive viewer

Interactively resize and preview an image in a Tkinter viewer:

$ ninepatch_viewer ninepatch_bubble.9.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 commands fit and wrap courtesy of Nicolas Laurance
  • fixed error in caching
  • optional caching for slice() and render()
  • new method export_slices()
  • changed command line parameters (render/slice)

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(14.0 kB) Copy SHA256 Hash SHA256
Source None Jun 30, 2015

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting