GitHub - theamusing/perfectPixel: Refine and quantize messy AI pixel art into clean, perfect pixels.
Service

GitHub - theamusing/perfectPixel: Refine and quantize messy AI pixel art into clean, perfect pixels.

theamusing
2026.08.05
·GitHub·by Mineru
#AI#ComfyUI#Image Processing#Pixel Art#Python

Key Points

  • 1Perfect Pixel is an automated tool designed to rectify AI-generated pixel art by detecting optimal grid sizes and aligning distorted pixels into a clean, consistent format.
  • 2The library offers both an OpenCV-based backend for high performance and a lightweight NumPy-only alternative to suit various technical environments.
  • 3The core algorithm identifies grids using FFT magnitude, refines them through Sobel edge detection, and resamples the original image to produce a perfectly aligned, pixel-perfect output.

Perfect Pixel: Automated Grid Alignment for Pixel Art

Perfect Pixel is a specialized computational tool designed to resolve the common issues of inconsistent sizing and grid misalignment inherent in AI-generated pixel art. It automates the detection of optimal grid structures and refines distorted images to ensure they adhere to a perfect, pixel-aligned format. The tool offers two implementation backends—an OpenCV-based version for optimal performance and a lightweight, NumPy-only alternative.

Core Methodology

The algorithmic process of Perfect Pixel is structured into three primary technical phases:

  1. Grid Size Detection via FFT:
The system begins by performing a Fast Fourier Transform (FFT) on the input image to analyze its frequency components. Because pixel art is characterized by periodic spatial patterns (the grid lines), these manifest as distinct peaks in the frequency domain. The algorithm computes the magnitude of the FFT to isolate these dominant spatial frequencies, effectively determining the original grid dimensions (grid_w,grid_hgrid\_w, grid\_h) even when the input is distorted.

  1. Edge Detection and Grid Refinement:
To account for slight inaccuracies in the FFT-detected grid, the algorithm employs a Sobel operator to identify image gradients, effectively mapping the "edges" where pixel colors transition. It performs a local search refinement around the initial grid lines detected in the previous step. By minimizing the discrepancy between the theoretical grid positions and the actual intensity gradients detected by the Sobel filter, the algorithm aligns the grid precisely to the intended pixel boundaries. The refinement is constrained by a parameter ρ\rho, where the search for the optimal grid line x′x' is bounded within the range:
x′∈[x⋅(1−refine_intensity),x⋅(1+refine_intensity)]x' \in [x \cdot (1 - \text{refine\_intensity}), x \cdot (1 + \text{refine\_intensity})]
This ensures the grid snaps correctly to the center of the visual pixels.

  1. Image Sampling:
Once the optimal grid is established, the tool performs a re-sampling of the original image to produce the final, clean output. Users can choose between three sampling strategies to determine the representative color for each grid cell:
  • Center: Samples the pixel value strictly from the center of each detected grid cell.
  • Median: Calculates the median color value within the grid cell boundaries.
  • Majority: Selects the most frequent color present in the grid cell, which is often the most robust method for noisy or distorted AI-generated inputs.

Integration and Usage

The tool is designed for modularity, providing a Python API that accepts RGB images and allows for manual overrides of grid sizes or peak detection parameters. It includes native support for ComfyUI, enabling seamless integration into generative AI pipelines. By enforcing strict alignment, Perfect Pixel transforms semi-coherent AI illustrations into clean, usable assets for applications such as perler bead patterns or retro-style digital projects.