Street art by clustering.
Pics by @alys.cheshire
Take a nice picture :
Generate a 5 colors stencil model :
>>> import pycht
>>> pycht.stencil('cat.jpg', 5)
Stencil 1 | stencil 2 | stencil 3 | stencil 4 | stencil 5 |
---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
Final result rendering with all stencils :
Cut it, paint it, stare at it. Enjoy !
The full documentation for this project is available at:
π https://tlentali.github.io/pycht/
It includes installation instructions, usage examples, and the API reference.
π You need to install Python 3.12 or above.
Installation can be done by using pip
.
There are wheels available for Linux, MacOS, and Windows.
pip install pycht
You can also install the latest development version as so:
pip install git+https://github.com/tlentali/pycht
# Or, through SSH:
pip install git+ssh://[email protected]/tlentali/pycht.git
Imagine pycht
as your personal digital street artist. Here's what happens under the hood, step-by-step:
-
πΌοΈ Image loading
pycht
grabs your input image and flattens it like a pancake β every pixel becomes a 3-value row (B, G, R) in a giant NumPy array. Think of it as turning your photo into a spreadsheet of colors. -
π― K-Means clustering Then comes the science. Using Scikit-Learnβs
kmeans
, we ask: βHey, what are theN
most dominant colors in this image?β The algorithm groups similar pixels intonb_colors
clusters and assigns each one a centroid β like reducing a rainbow into just a few paint buckets. -
π¨ Color mapping Every pixel in your image is replaced by its cluster's centroid. Boom β you've got a stylized version of your image with just
N
bold, poster-style colors. -
π Color separation Now the magic: for each color,
pycht
creates a mask. All pixels that donβt belong to the current color cluster are set to black (and later transparent). Each color gets its own PNG file β like cutting stencils for spray-painting layers IRL. -
π File drop Your output includes:
output.png
β The clustered imagestencil_1.png
,stencil_2.png
, ... β Transparent layers, one per color
It's like building silkscreen layers, but with Python, pixels, and zero mess.
Ready to turn your cat photo into street art? Let pycht
paint it.
You can use pip
or uv. From the pycht
root folder, do:
uv venv --python /path/to/3.12.x/python
. Tips: you can use pyenv to manage and install multiple Python versions. You can find a specific version at~/.pyenv/versions/3.12.2/bin/python
for instance.source .venv/bin/activate
to activate the virtualenv.venv
created byuv
uv sync --inexact
to install all dependenciespre-commit install
(just one time). The pre-commit hook will run black, isort and pylint before your commit :)
You're ready to hack!
You can use pycht
as a command-line tool to generate stencil layers from an image β perfect for street art, posters, or digital illustration.
Install in editable mode (dev mode) with uv or pip:
uv pip install -e .
Make sure you have the required dependencies listed in pyproject.toml
.
pycht <input-img> [OPTIONS]
Arguments:
<input-img>
: Path to the input image (JPEG, PNG, etc.)
Options:
--output-path TEXT
β Directory where output layers will be saved (default:./output
)--nb-colors INTEGER
β Number of stencil layers to generate (default:3
)
pycht misc/cat.jpg --nb-colors 4 --output-path .
This will create 4 stencil layers and save them in the current folder.
Feel free to contribute in any way you like, we're always open to new ideas and approaches. If you want to contribute to the code base please check out the CONTRIBUTING.md file. Also take a look at the issue tracker and see if anything takes your fancy.
This project follows the all-contributors specification. Again, contributions of any kind are welcome!
pycht
is free and open-source software licensed under the MIT license.