Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

9-Patch PNG Generator

A Python script that generates high-quality 9-patch PNG files with circular shapes, perfect for Android UI development and other applications requiring scalable rounded rectangles.

Features

  • High-quality antialiased output with 4x supersampling
  • 🎯 Two modes: filled circles or border-only circles
  • 📱 Multi-resolution support: automatically generates FHD and HD variants
  • 🔧 Proper 9-patch markers for fixed corner radius behavior
  • 🎨 Clean white shapes on transparent backgrounds

Installation

Requirements

pip install Pillow numpy

Download

git clone https://github.com/rak0njac/ninepatch-generator.git
cd ninepatch-generator

Usage

Basic Usage

# Generate filled circle with radius 50
python ninepatch_generator.py 50

# Generate border circle with radius 50 and border width 5
python ninepatch_generator.py 50 --border 5

Examples

# Filled circles
python ninepatch_generator.py 24    # Small button
python ninepatch_generator.py 48    # Medium button
python ninepatch_generator.py 72    # Large button

# Border circles
python ninepatch_generator.py 24 --border 2    # Thin border
python ninepatch_generator.py 48 --border 4    # Medium border
python ninepatch_generator.py 72 --border 6    # Thick border

Output

The script generates files in two directories:

  • fhd/ - Full HD resolution (original size)
  • hd/ - HD resolution (66.67% of original size)

File Naming

  • Filled circles: fill_{radius}.9.png
  • Border circles: border_{radius}.9.png

Example Output Structure

fhd/
├── fill_24.9.png
├── fill_48.9.png
├── border_24.9.png
└── border_48.9.png
hd/
├── fill_24.9.png      # Actually 16px radius
├── fill_48.9.png      # Actually 32px radius
├── border_24.9.png    # Actually 16px radius
└── border_48.9.png    # Actually 32px radius

9-Patch Format

The generated files include proper 9-patch markers:

  • Stretch markers: 2 pixels on top and left edges for minimal stretching
  • No content markers: Clean output without padding constraints
  • Fixed corner radius: Maintains circular corners when stretched

Technical Details

Anti-aliasing

The script uses 4x supersampling for smooth edges:

  1. Renders at 4x resolution
  2. Applies high-quality downsampling
  3. Results in professional anti-aliased output

Multi-resolution Support

HD variants are generated by scaling the radius rather than the final image:

  • Better quality: Native rendering at each resolution
  • Consistent proportions: Proper 9-patch marker positioning
  • Sharp results: No additional resampling artifacts

Command Line Options

positional arguments:
  radius              Radius of the circle in pixels

optional arguments:
  -h, --help          Show help message
  --border BORDER     Border width in pixels (creates border-only circle)
  -o, --output        Output filename (ignored - uses automatic naming)

Use Cases

  • Android app UI components
  • Web UI rounded buttons and containers
  • Game UI elements
  • Any application requiring scalable rounded rectangles

License

MIT License - feel free to use in your projects!

Contributing

Issues and pull requests welcome! Please feel free to suggest improvements or report bugs.

About

Python script to generate 9-patch PNG files with circular shapes for Android UI development

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages