perfect-pixel

repository·main·Indexed 23 days ago

https://github.com/theamusing/perfectpixel

A library for auto grid detection and pixel art refinement, version 0.1.4. It uses FFT and gradient-based estimation to detect grid scales and refine AI-generated pixel art. The library provides the `get_perfect_pixel` function with multiple sampling methods (center, median, majority) and includes a custom node integration for ComfyUI for image post-processing. It supports both an OpenCV-accelerated backend for higher performance and a lightweight NumPy-only implementation.

Tokens
2.7K
Snippets
5
Records
15
Agent score
79%

What's inside perfect-pixel

  1. How the Perfect Pixel algorithm works

    main

    The algorithm refines AI-generated pixel art through a three-step process:

    1. Grid Detection: Detects grid size from the FFT (Fast Fourier Transform) magnitude of the original image and generates initial grids.
    2. Edge Refinement: Detects edges using Sobel operators and refines the grids by aligning them to those edges.
    3. Sampling: Uses the refined grids to sample the original image and produce the final scaled, pixel-perfect image.
  2. Install the PerfectPixel ComfyUI Node

    main

    You can install the node using either a one-click Git method or a manual method.

    If you have Git installed, run these commands inside your ComfyUI/custom_nodes directory and then restart ComfyUI:

    git clone https://github.com/TobyKSKGD/perfectPixel-ComfyUI.git
    
    # For the fast version with OpenCV support:
    pip install perfect-pixel[opencv]
    
    # OR for the lightweight NumPy-only version:
    # pip install perfect-pixel

    Manual Installation

    1. Download the repository.
    2. Copy the folder ./integrations/comfyui/perfectPixel-ComfyUI into ComfyUI/custom_nodes.
    3. Copy these two core files from the original Perfect Pixel source into the same PerfectPixelComfy folder:
      • ./src/perfect_pixel/perfect_pixel.py
      • ./src/perfect_pixel/perfect_pixel_noCV2.py
    4. Restart ComfyUI.

    After restarting, the node can be found at: Image → Post Processing → Perfect Pixel (Grid Restore).

    git clone https://github.com/TobyKSKGD/perfectPixel-ComfyUI.git
    
    # Recommended: Fast version with OpenCV support
    pip install perfect-pixel[opencv]
  3. Install Perfect Pixel via pip

    main

    Perfect Pixel offers two installation options depending on your environment and performance needs:

    1. OpenCV Backend (Recommended): Provides better performance. Requires opencv-python and numpy.
    2. Lightweight Backend: Use this if you cannot or do not want to use cv2. Requires only numpy.

    Install the recommended fast version using the [opencv] extra:

    # Recommended: Fast version with OpenCV support
    pip install perfect-pixel[opencv]
    
    # Numpy version: Lightweight (NumPy only)
    pip install perfect-pixel
  4. Install Python dependencies in ComfyUI (Windows)

    main

    On Windows, ComfyUI often uses an embedded Python environment. Installing packages via your system's pip will not make them available to ComfyUI. You must use the specific Python executable used by ComfyUI.

    Step 1: Find the ComfyUI Python executable

    Start ComfyUI and check the terminal startup log. Look for the line starting with ** Python executable:.

    Step 2: Install using that executable

    Open a terminal and use the path found in Step 1 to run pip commands.

    Example for Windows portable builds:

    G:\ComfyUI\ComfyUI_windows_portable\python_embeded\python.exe -m pip install -U pip
    G:\ComfyUI\ComfyUI_windows_portable\python_embeded\python.exe -m pip install "perfect-pixel[opencv]"

    General Command Template:

    [path_to_your_ComfyUI_python]\python.exe -m pip install "perfect-pixel[opencv]"
  5. Choose a sampling method for perfect pixel scaling

    main

    When calling get_perfect_pixel, you can specify how pixels are sampled from the detected grid cells using the sample_method parameter:

    1. "center": The simplest method. It calculates the geometric center of each grid cell and samples the pixel at that coordinate. This is fast but may not represent the cell's color well if the center pixel is an outlier.
    2. "median": Calculates the median color of all pixels within the cell. This is robust against noise and outliers.
    3. "majority": The most advanced method. It uses K-means clustering (with $K=2$) on the pixels within the cell and selects the cluster center that contains the most pixels. This is highly effective for capturing the dominant color in a cell.
  6. Detect grid scale using FFT or Gradients

    main

    The library uses two main strategies to estimate the underlying grid scale of an image:

    • FFT-based estimation (estimate_grid_fft): Uses Fast Fourier Transform to find periodic patterns in the image frequency domain. This is generally the first attempt.
    • Gradient-based estimation (estimate_grid_gradient): If FFT fails or produces inconsistent results, the system falls back to analyzing the magnitude of Sobel gradients to find periodic peaks in edge density.

    If both methods fail, the function returns None, None and the original image is returned by the main entrypoint.

  7. Choose a sampling method for grid cells

    main

    When scaling an image to a new grid, the get_perfect_pixel function uses one of three sampling strategies to determine the color of the new pixels:

    1. "center": Samples the color from the geometric center of the grid cell. This is the fastest method.
    2. "median": Calculates the median color of all pixels within the grid cell. This is robust to outliers.
    3. "majority": Uses an iterative approach to find the most representative color (mode-like) within the cell. This is generally the most accurate for preserving textures but is more computationally intensive.
  8. Use get_perfect_pixel to refine images

    main

    The get_perfect_pixel function automatically detects the optimal grid size from a pixel-style image and returns a refined, perfectly aligned version.

    Input should be an RGB image. The function returns the refined width, refined height, and the refined image itself.

    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)
  9. Configure get_perfect_pixel arguments

    main

    The get_perfect_pixel function accepts several arguments to control the detection and refinement process:

    ArgumentType/OptionsDescription
    imageRGB Image (H * W * 3)The input image to process.
    sample_method"center", "median" or "majority"The method used for sampling.
    grid_size(grid_w, grid_h)Manually set grid size to override auto-detection.
    min_sizeMinimum pixel size to consider valid.
    peak_widthMinimum peak width for peak detection.
    refine_intensity[0, 0.5]Intensity for grid line refinement. Searches in [x * (1 - refine_intensity), x * (1 + refine_intensity)] around estimated line x.
    fix_squareWhether to enforce output to be square when detected image is almost square.
    debugWhether to show debug plots.

    Returns:

    • refined_w: Width of the refined image
    • refined_h: Height of the refined image
    • scaled_image: Refined Image (W * H * 3)
  10. Configure the Perfect Pixel (Grid Restore) node

    main

    The Perfect Pixel (Grid Restore) node provides the following parameters:

    • sampling: The sampling method used when restoring pixel grids.
    • export_scale: The scaling factor applied to the output image.
    • backend: Determines the implementation used for processing:
      • Auto: Automatically selects the best available backend.
      • OpenCV Backend: Uses OpenCV for higher performance.
      • Lightweight Backend: Uses a NumPy-only implementation (no OpenCV required).