Updated readme

This commit is contained in:
Meta Vens 2026-04-21 22:34:35 +01:00
commit 6b52613a27
6 changed files with 85 additions and 12 deletions

View file

@ -1,8 +1,20 @@
# Repixelizer
Repixelizer is a standalone Python CLI for turning "fake pixel art" into true grid-aligned pixel art.
Repixelizer is a standalone Python CLI for images that are doing a pixel art impression instead of actually respecting the grid.
The project targets images that look locally pixelated but do not actually sit on one clean lattice. Instead of treating that as a resize problem, Repixelizer treats it as a lattice-inference and local-structure-preservation problem: infer the implied grid, choose real output cells, and snap them back onto a coherent pixel lattice while preserving the source's local adjacency patterns.
It is aimed at the annoying middle ground: sprites, emblems, and logos that look locally pixelated but fall apart the second you ask them to commit to one lattice like grown-ups.
Instead of pretending this is a resize problem, Repixelizer treats it as lattice inference plus local structure preservation: infer the implied grid, choose real output cells, and snap them back onto a coherent pixel lattice while preserving the source's local adjacency patterns.
## Real Example
This input was not chosen because it was easy. It has glossy shading, chunky highlights, and enough large-scale structure to confuse size inference in ways that are frankly a little rude.
The cleanest result here came from forcing `--target-size 128`:
![Real repixelize example](docs/readme-assets/badge-example-sheet.png)
Automatic lattice inference on this badge is much less wrong than it used to be, but this is still a good example of the kind of image where a manual target size can help.
## Current Status

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 KiB

View file

@ -137,14 +137,47 @@ def _estimate_lattice_spacing(rgba: np.ndarray) -> tuple[tuple[float | None, flo
return _estimate_spacing_cell_size(delta_x, premult_rgba.shape[1]), _estimate_spacing_cell_size(delta_y, premult_rgba.shape[0])
def _blend_prior(spacing: float | None, spacing_confidence: float, autocorr: float) -> float:
def _weighted_geometric_mean(values: list[float], weights: list[float]) -> float:
safe_values = [float(max(1e-6, value)) for value in values]
safe_weights = [float(max(0.0, weight)) for weight in weights]
total_weight = float(sum(safe_weights))
if total_weight <= 1e-6:
return float(np.mean(safe_values))
logs = np.log(np.asarray(safe_values, dtype=np.float32))
return float(np.exp(np.dot(logs, np.asarray(safe_weights, dtype=np.float32)) / total_weight))
def _axis_prior_from_estimates(spacing: float | None, spacing_confidence: float, autocorr: float) -> tuple[float, float]:
if spacing is None:
return float(autocorr)
weight = float(np.clip(spacing_confidence, 0.0, 0.85))
return float(spacing * weight + autocorr * (1.0 - weight))
return float(autocorr), 0.12
ratio = max(autocorr, spacing) / max(1e-6, min(autocorr, spacing))
suspicious_multiple = ratio >= 1.9 and abs(ratio - round(ratio)) <= 0.2
autocorr_weight = float(np.clip(1.0 - spacing_confidence, 0.05, 0.65))
if suspicious_multiple and spacing_confidence >= 0.15:
autocorr_weight *= 0.35
prior = _weighted_geometric_mean([spacing, autocorr], [1.0, autocorr_weight])
reliability = float(np.clip(0.15 + 0.75 * spacing_confidence, 0.1, 0.9))
if suspicious_multiple:
reliability *= 0.9
return prior, float(reliability)
def _estimate_lattice_prior(rgba: np.ndarray) -> tuple[float, float]:
def _combine_axis_priors(axis_priors: list[tuple[float, float]]) -> tuple[float, float]:
priors = [float(prior) for prior, weight in axis_priors if weight > 1e-6]
weights = [float(weight) for _, weight in axis_priors if weight > 1e-6]
if not priors:
return 16.0, 0.0
shared_prior = _weighted_geometric_mean(priors, weights)
if len(priors) == 1:
return shared_prior, float(np.clip(weights[0], 0.1, 0.9))
log_priors = np.log(np.asarray(priors, dtype=np.float32))
spread = float(np.mean(np.abs(log_priors - np.mean(log_priors))))
consistency = float(np.exp(-spread * 1.25))
reliability = float(np.clip((sum(weights) / len(weights)) * consistency, 0.08, 0.9))
return shared_prior, reliability
def _estimate_lattice_prior_details(rgba: np.ndarray) -> tuple[float, float, float]:
alpha = rgba[..., 3]
luminance = rgba[..., 0] * 0.2126 + rgba[..., 1] * 0.7152 + rgba[..., 2] * 0.0722
dx = np.zeros_like(luminance)
@ -156,7 +189,15 @@ def _estimate_lattice_prior(rgba: np.ndarray) -> tuple[float, float]:
spacing_x, spacing_y = _estimate_lattice_spacing(rgba)
autocorr_x = _estimate_cell_size(profile_x)
autocorr_y = _estimate_cell_size(profile_y)
return _blend_prior(spacing_x[0], spacing_x[1], autocorr_x), _blend_prior(spacing_y[0], spacing_y[1], autocorr_y)
axis_x = _axis_prior_from_estimates(spacing_x[0], spacing_x[1], autocorr_x)
axis_y = _axis_prior_from_estimates(spacing_y[0], spacing_y[1], autocorr_y)
shared_prior, reliability = _combine_axis_priors([axis_x, axis_y])
return shared_prior, shared_prior, reliability
def _estimate_lattice_prior(rgba: np.ndarray) -> tuple[float, float]:
prior_x, prior_y, _ = _estimate_lattice_prior_details(rgba)
return prior_x, prior_y
def _build_phase_grid(
@ -261,6 +302,7 @@ def _score_phase_group(
target_height: int,
prior_cell_x: float,
prior_cell_y: float,
prior_reliability: float,
phase_values: np.ndarray,
device: str,
):
@ -291,7 +333,8 @@ def _score_phase_group(
prior_score_x = np.exp(-abs(np.log((cell_x + 1e-6) / (prior_cell_x + 1e-6))) * 0.8)
prior_score_y = np.exp(-abs(np.log((cell_y + 1e-6) / (prior_cell_y + 1e-6))) * 0.8)
size_prior = float((prior_score_x + prior_score_y) * 0.5)
score = breakdown["coherence_score"] * 0.55 + size_prior * 0.45
size_prior_weight = float(np.clip(0.10 + prior_reliability * 0.35, 0.10, 0.45))
score = breakdown["coherence_score"] * (1.0 - size_prior_weight) + size_prior * size_prior_weight
score_np = score.detach().cpu().numpy()
candidates: list[InferenceCandidate] = []
@ -305,6 +348,7 @@ def _score_phase_group(
"color_chatter": float(breakdown["color_chatter"][index].detach().cpu().item()),
"coherence_score": float(breakdown["coherence_score"][index].detach().cpu().item()),
"size_prior": size_prior,
"size_prior_weight": size_prior_weight,
}
candidates.append(
InferenceCandidate(
@ -323,7 +367,7 @@ def infer_lattice(rgba: np.ndarray, target_size: int | None = None, device: str
torch, _ = _require_torch()
resolved_device = _resolve_device(torch, device)
height, width = rgba.shape[:2]
prior_cell_x, prior_cell_y = _estimate_lattice_prior(rgba)
prior_cell_x, prior_cell_y, prior_reliability = _estimate_lattice_prior_details(rgba)
phase_values = np.linspace(-0.4, 0.4, num=5, dtype=np.float32)
candidates: list[InferenceCandidate] = []
@ -335,6 +379,7 @@ def infer_lattice(rgba: np.ndarray, target_size: int | None = None, device: str
target_height=target_height,
prior_cell_x=prior_cell_x,
prior_cell_y=prior_cell_y,
prior_reliability=prior_reliability,
phase_values=phase_values,
device=resolved_device,
)

View file

@ -1,9 +1,25 @@
from __future__ import annotations
from repixelizer.inference import infer_lattice
from repixelizer.inference import (
_axis_prior_from_estimates,
_combine_axis_priors,
infer_lattice,
)
from repixelizer.synthetic import fake_pixelize, make_emblem
def test_axis_prior_stays_close_to_spacing_when_autocorr_hits_large_multiple() -> None:
prior, reliability = _axis_prior_from_estimates(8.0, 0.75, 32.0)
assert prior < 10.0
assert reliability > 0.6
def test_combined_axis_prior_prefers_consistent_shared_cell_size() -> None:
shared_prior, reliability = _combine_axis_priors([(9.0, 0.7), (11.2, 0.2)])
assert 8.5 < shared_prior < 10.5
assert 0.2 < reliability < 0.6
def test_infer_lattice_recovers_emblem_scale() -> None:
source = make_emblem(32, 32)
fake = fake_pixelize(