Converts pixel-art-style images such as those from generative models or low-quality sprite web uploads to true resolution usable assets.
Project description
Proper Pixel Art
Summary
-
Converts noisy, high resolution pixel-art-style images such as those produced by generative models to true pixel resolution assets.
-
Clean screenshots or low-quality web uploads of sprites.
Installation
Clone the Repository
git clone git@github.com:KennethJAllen/proper-pixel-art.git
cd proper-pixel-art
Create Virtual Environment
- Install uv if not already installed.
- Sync environments
uv sync
Usage
First, obtain a source pixel-art-style image (e.g. a pixel-art-style image generated by GPT-4o or a screenshot of pixel-art).
CLI
uv run ppa <input_path> -o <output_path> -c <num_colors> -s <result_scale> [-t]
# or directly using uvx
uvx --from https://github.com/KennethJAllen/proper-pixel-art.git ppa <input_path> -o <output_path> -c <num_colors> -s <result_scale> [-t]
Flags
| Flag | Description |
|---|---|
| INPUT (positional) | Source file in pixel-art-style |
-o, --output <path> |
Output directory or file path for result. (default: '.') |
-c, --colors <int> |
Number of colors for output. May need to try a few different values. (default 16) |
-s, --scale-result <int> |
Width/height of each "pixel" in the output. (default: 1) |
-t, --transparent <bool> |
Output with transparent background. (default: off) |
-u, --initial-upscale <int> |
Initial image upscale factor. Increasing this may help detect pixel edges. (default 2) |
-w, --pixel-width <int> |
Width of the pixels in the input image. If not set, it will be determined automatically. (default: None) |
Example
uv run ppa assets/blob/blob.png -c 16 -s 20 -t
Python
from PIL import Image
from proper_pixel_art.pixelate import pixelate
image = Image.open('path/to/input.png')
result = pixelate(image, num_colors=16)
result.save('path/to/output.png')
Parameters
-
image:PIL.Image.Image- A PIL image to pixelate.
-
num_colors:int- The number of colors in result.
- May need to try a few values if the colors don't look right.
- 8, 16, 32, or 64 typically works.
-
initial_upscale:int- Upscale result after algorithm is complete if not None.
-
scale_result:int- Upscale initial image. This may help detect lines.
-
transparent_background:bool- If True, flood fills each corner of the result with transparent alpha.
-
intermediate_dir:Path | None- Directory to save images visualizing intermediate steps of algorithm. Useful for development.
-
pixel_width:int | None- Width of the pixels in the input image. If not set, it will be determined automatically. It may be helpful to increase this parameter if not enough pixel edges are being detected.
Returns
A PIL image with true pixel resolution and quantized colors.
Web Interface
Local:
uv sync --extra web
uv run ppa-web
# Opens http://127.0.0.1:7860
Without cloning:
uvx --from "proper-pixel-art[web]" ppa-web
Examples
The algorithm is robust. It performs well for images that are already approximately alligned to a grid.
Here are a few examples. A mesh is computed, where each cell corresponds to one pixel.
Bat
- Generated by GPT-4o.
|
Noisy, High Resolution |
Mesh |
True Pixel Resolution |
Ash
- Screenshot from Google images of Pokemon asset.
|
Noisy, High Resolution |
Mesh |
True Pixel Resolution |
Demon
- Original image generated by GPT-4o.
|
Noisy, High Resolution |
Mesh |
True Pixel Resolution |
Pumpkin
- Screenshot from Google Images of Stardew Valley asset. This is an adversarial example as the source image is both low quality and the object is round.
|
Noisy, High Resolution |
Mesh |
True Pixel Resolution |
Real Images To Pixel Art
-
This tool can also be used to convert real images to pixel art by first requesting a pixelated version of the original image from GPT-4o, then using the tool to get the true pixel-resolution image.
-
Consider this image of a mountain
- Here are the results of first requesting a pixelated version of the mountain, then using the tool to get a true resolution pixel art version.
|
Noisy, High Resolution |
Mesh |
True Pixel Resolution |
Challenges
The result of pixel-art style images from LLMs are noisy, high resolution images with a non-uniform grid and random artifacts. Due to these issues, standard downsampling techniques do not work. How can we recover the pixel art with "true" resolution and colors?
The current approach to turning pixel art into useable assets for games are either
- Use naive downsampling which does not give a result that is faithful to the original image.
- Manually re-create the image in the appropriate resolution pixel by pixel.
Algorithm
- The main algorithm solves these challenges. Here is a high level overview. We will apply it step by step on this example image of blob pixel art that was generated from GPT-4o.
- Note that this image is high resolution and noisy.
-
Trim the edges of the image and zero out pixels with more than 50% alpha.
- This is to work around some issues with models such as GPT-4o not giving a perfectly transparent background.
-
Upscale by a factor of 2 using nearest neighbor.
- This can help identify the correct pixel mesh.
-
Find edges of the pixel art using Canny edge detection.
- Close small gaps in edges with a morphological closing.
- Take the probabalistic Hough transform to get the coordinates of lines in the detected edges. Only keep lines that are close to vertical or horizontal giving some grid coordinates. Cluster lines that are closeby together.
- Find the grid spacing by filtering outliers and taking the median of the spacings, then complete the mesh.
-
Quantize the original image to a small number of colors.
- Note: The result is sensitive to the number of colors chosen.
- The parameter is not difficult to tune, but the script may need to be re-run if the colors don't look right.
- 8, 16, 32, or 64 typically works.
-
In each cell specified by the mesh, choose the most common color in the cell as the color for the pixel. Recreate the original image with one pixel per cell.
- Result upscaled by a factor of $20 \times$ using nearest neighbor.
Testing
To test algorithm changes and verify output quality:
uv run pytest -s
The tests pixelate all assets in assets/{name}/{name}.png and save outputs to tests/outputs/ for manual visual inspection.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file proper_pixel_art-1.3.4.tar.gz.
File metadata
- Download URL: proper_pixel_art-1.3.4.tar.gz
- Upload date:
- Size: 19.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94e99e7eac5753aa9363e8098cf60159e4a54291d0c868171f03b77308d21c4d
|
|
| MD5 |
2e50bcaf096a4e65bd508c6784fcdbc5
|
|
| BLAKE2b-256 |
3f568dd85725b71f0ca321190790cb07155da6e18079e67c9403cc32a244ae7a
|
Provenance
The following attestation bundles were made for proper_pixel_art-1.3.4.tar.gz:
Publisher:
workflow.yml on KennethJAllen/proper-pixel-art
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
proper_pixel_art-1.3.4.tar.gz -
Subject digest:
94e99e7eac5753aa9363e8098cf60159e4a54291d0c868171f03b77308d21c4d - Sigstore transparency entry: 731149337
- Sigstore integration time:
-
Permalink:
KennethJAllen/proper-pixel-art@d0a1e9f405dcc9d636136516501982eb53e2f5ca -
Branch / Tag:
refs/tags/v1.3.4 - Owner: https://github.com/KennethJAllen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@d0a1e9f405dcc9d636136516501982eb53e2f5ca -
Trigger Event:
release
-
Statement type:
File details
Details for the file proper_pixel_art-1.3.4-py3-none-any.whl.
File metadata
- Download URL: proper_pixel_art-1.3.4-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aed43be44ed27c5b14eb7999dd09affc2997d956965609250aab0ed57fa4de69
|
|
| MD5 |
300589268a0010e147fbf8a06f03d6f0
|
|
| BLAKE2b-256 |
cd8bee1cb73c71a7d4644140ae094dfc0417d82a72b3d1ae759b40190340034a
|
Provenance
The following attestation bundles were made for proper_pixel_art-1.3.4-py3-none-any.whl:
Publisher:
workflow.yml on KennethJAllen/proper-pixel-art
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
proper_pixel_art-1.3.4-py3-none-any.whl -
Subject digest:
aed43be44ed27c5b14eb7999dd09affc2997d956965609250aab0ed57fa4de69 - Sigstore transparency entry: 731149339
- Sigstore integration time:
-
Permalink:
KennethJAllen/proper-pixel-art@d0a1e9f405dcc9d636136516501982eb53e2f5ca -
Branch / Tag:
refs/tags/v1.3.4 - Owner: https://github.com/KennethJAllen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@d0a1e9f405dcc9d636136516501982eb53e2f5ca -
Trigger Event:
release
-
Statement type: