repixelizer/src/repixelizer/io.py
Meta Vens 9e8c8f6b8d Remove dead bilinear_resize and lattice_source_rgba
REC-117: both functions had definitions but no imports, calls, tests,
or doc references anywhere in the repo.

Kill attempts that failed: none.
- `rg -n -w 'bilinear_resize|lattice_source_rgba' src tests tools
  scripts README.md docs` -> only the two now-removed definitions
  (repeated the census command against current HEAD).
- Cross-repo `rg -n -w 'bilinear_resize|lattice_source_rgba'` over all
  of F:/Projects -> only this repo's own source and the census docs.
- Confirmed `build_source_lattice_reference`, which
  `lattice_source_rgba` called, is still used by two other live
  callers in metrics.py (lines 280, 311) and was left untouched.

Checks run: `python -m py_compile src/repixelizer/io.py
src/repixelizer/metrics.py` -> compiles cleanly. Did not run the
pytest suite: this repo's runtime dependencies include torch>=2.6.0,
which is not installed in this worktree and installing it would be a
multi-GB download outside this pass's focused-check budget; no test
file references either function by name
(`rg -n -w 'bilinear_resize|lattice_source_rgba' tests` -> no
matches), so this is a compile-level check only, stated plainly rather
than reported as a full green test run.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-13 14:22:17 +02:00

47 lines
1.7 KiB
Python

from __future__ import annotations
from pathlib import Path
import numpy as np
from PIL import Image
def load_rgba(path: str | Path) -> np.ndarray:
image = Image.open(path).convert("RGBA")
return np.asarray(image, dtype=np.float32) / 255.0
def save_rgba(path: str | Path, rgba: np.ndarray) -> None:
clipped = np.clip(np.rint(rgba * 255.0), 0, 255).astype(np.uint8)
Image.fromarray(clipped, mode="RGBA").save(path)
def premultiply(rgba: np.ndarray) -> np.ndarray:
premult = rgba.copy()
premult[..., :3] *= premult[..., 3:4]
return premult
def unpremultiply(rgba: np.ndarray, eps: float = 1e-6) -> np.ndarray:
out = rgba.copy()
alpha = np.maximum(out[..., 3:4], eps)
out[..., :3] = np.where(out[..., 3:4] > eps, out[..., :3] / alpha, 0.0)
return np.clip(out, 0.0, 1.0)
def nearest_resize(rgba: np.ndarray, width: int, height: int) -> np.ndarray:
image = Image.fromarray(np.clip(np.rint(rgba * 255.0), 0, 255).astype(np.uint8), mode="RGBA")
resized = image.resize((width, height), resample=Image.Resampling.NEAREST)
return np.asarray(resized, dtype=np.float32) / 255.0
def box_resize(rgba: np.ndarray, width: int, height: int) -> np.ndarray:
image = Image.fromarray(np.clip(np.rint(rgba * 255.0), 0, 255).astype(np.uint8), mode="RGBA")
resized = image.resize((width, height), resample=Image.Resampling.BOX)
return np.asarray(resized, dtype=np.float32) / 255.0
def lanczos_resize(rgba: np.ndarray, width: int, height: int) -> np.ndarray:
image = Image.fromarray(np.clip(np.rint(rgba * 255.0), 0, 255).astype(np.uint8), mode="RGBA")
resized = image.resize((width, height), resample=Image.Resampling.LANCZOS)
return np.asarray(resized, dtype=np.float32) / 255.0