Skip to main content

A library for auto grid detection and pixel art refinement.

Project description

Perfect Pixel

Auto detect and Get perfect Pixel art

Python License

Standard scaling often fails to sample AI-generated pixel art due to inconsistent sizes and non-square grids.

This tool automatically detects the optimal grid and delivers perfectly aligned, pixel-perfect results.

Features

  • Automatically detect grid size from pixel style images.
  • Refines AI generated pixel style image to perfectly aligned grids.
  • Easy to integrate into your own workflow.

Try the Web Demo

Installation

Perfect Pixel provides two implementations of the same core algorithm. The Lighweight Backend is designed in case you can't or don't want to use cv2. You can choose the one that best fits your environment:

Feature OpenCV Backend (perfectPixel.py) Lightweight Backend (perfectPixelnoCV2.py)
Dependencies opencv-python, numpy numpy

You can install Perfect Pixel via pip. It is recommended to install the OpenCV version for better performance.

# Recommended: Fast version with OpenCV support
pip install perfect-pixel[opencv]

# Numpy version: Lightweight (NumPy only)
pip install perfect-pixel

Usage

Step 1: Get pixel style image

First you need extra tools to get a pixel styled image. The recommanded size is between 512 to 1024.

You can use Stable Diffusion with any Pixel Style Lora, or you can use ChatGPT or Gemini to generate one.

For example, I used ChatGPT to transfer an image into pixel style.

prompt: Convert the input image into a TRUE perler bead pixel pattern designed for physical bead crafting, not digital illustration. Canvas size must be exactly 32×32 pixels OR 16×16 pixels, where each pixel represents exactly one perler bead. Use extremely large, chunky pixels with very few active pixels overall. Simplicity is critical. Only keep the main subject. Remove the entire background. For human characters, make sure the face is flat and no shadow. The subject must be centered with clear empty bead rows around all edges to allow easy mounting on a bead board. Add a clean, continuous dark outline around the subject so the silhouette is clearly readable when made with beads. Use a very limited solid color palette (maximum 6–8 colors total). No gradients, no shading, no lighting, no dithering, no texture. No anti-aliasing or smoothing — every pixel must be a perfect square bead aligned to the grid. The output image should be pixel-perfect, each grid only contains one color. Background must be pure solid white.

The image is in pixel style but the grids are distorted. Also we don't know the number of grids.

Step 2: Use Perfect Pixel to refine your image

import cv2
from perfect-pixel import get_perfect_pixel

bgr = cv2.imread("images/avatar.png", cv2.IMREAD_COLOR)
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

w, h, out = get_perfect_pixel(rgb)

Also see example.py.

python example.py

The grid size is automatically detected, and the image is refined.

Try integrate it into your own projects!

API Reference

Args Description
image RGB Image (H * W * 3)
sample_method "majority" or "center"
grid_size Manually set grid size (grid_w, grid_h) to override auto-detection
min_size Minimum pixel size to consider valid
peak_width Minimum peak width for peak detection.
refine_intensity Intensity for grid line refinement. Recommended range is [0, 0.5]. Given original estimated grid line at x, the refinement will search in [x * (1 - refine_intensity), x * (1 + refine_intensity)].
fix_square Whether to enforce output to be square when detected image is almost square.
debug Whether to show debug plots.
Returns Description
refined_w Width of the refined image
refined_h Height of the refined image
scaled_image Refined Image(W * H * 3)

Algorithm

The whole algorithm mainly contains 3 steps:

  1. Detect grid size from FFT magnitude of the original image and generate grids.
  2. Detect edges using Sobel and refine the grids by aligning them to edges.
  3. Use the grids to sample the original image and to get the scaled image.

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

perfect_pixel-0.1.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

perfect_pixel-0.1.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file perfect_pixel-0.1.1.tar.gz.

File metadata

  • Download URL: perfect_pixel-0.1.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for perfect_pixel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9c78a15b644e1dbe5b5e0fc141a4dcbf2372ab55415f318b6f37ad687861a0d3
MD5 cd4a432cb68fdde4ddae8a6fb0c83b2c
BLAKE2b-256 48af6c40d26ecc0a049b1e2d29105b0b48da798e6f861d7efc239037eda33d67

See more details on using hashes here.

File details

Details for the file perfect_pixel-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: perfect_pixel-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for perfect_pixel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49b008e6d531aefe1b1212339587901763f85aed6aad623257898e24b9ef976c
MD5 6a004eef1a13214687d7b3ff8ad9fa55
BLAKE2b-256 0a5de1db4341276bf087372b6eb6afbaeba3460f92ff268f6cc7b510e432b47d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page