Add reproducible README preview generator
This commit is contained in:
parent
6f566d2731
commit
e8ef6be027
9 changed files with 415 additions and 5 deletions
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import numpy as np
|
||||
|
||||
from .io import box_resize
|
||||
from .io import box_resize, lanczos_resize
|
||||
from .palette import derive_palette
|
||||
|
||||
|
||||
|
|
@ -10,6 +10,10 @@ def naive_resize_baseline(rgba: np.ndarray, width: int, height: int) -> np.ndarr
|
|||
return box_resize(rgba, width=width, height=height)
|
||||
|
||||
|
||||
def lanczos_resize_baseline(rgba: np.ndarray, width: int, height: int) -> np.ndarray:
|
||||
return lanczos_resize(rgba, width=width, height=height)
|
||||
|
||||
|
||||
def _nearest_palette_color(pixel: np.ndarray, palette: np.ndarray) -> np.ndarray:
|
||||
distances = np.sum((palette - pixel[None, :]) ** 2, axis=1)
|
||||
return palette[int(np.argmin(distances))]
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from typing import Any
|
|||
import numpy as np
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
from .baselines import error_diffusion_baseline, naive_resize_baseline
|
||||
from .baselines import error_diffusion_baseline, lanczos_resize_baseline, naive_resize_baseline
|
||||
from .diagnostics import write_compare_csv, write_json
|
||||
from .io import nearest_resize, save_rgba
|
||||
from .metrics import (
|
||||
|
|
@ -30,6 +30,7 @@ def _to_uint8(rgba: np.ndarray) -> np.ndarray:
|
|||
def _contact_sheet(
|
||||
source: np.ndarray,
|
||||
optimized: np.ndarray,
|
||||
lanczos: np.ndarray,
|
||||
naive: np.ndarray,
|
||||
diffusion: np.ndarray,
|
||||
path: str | Path,
|
||||
|
|
@ -38,6 +39,7 @@ def _contact_sheet(
|
|||
panels = [
|
||||
("Source", source),
|
||||
("Optimized", nearest_resize(optimized, width=width, height=height)),
|
||||
("Lanczos", nearest_resize(lanczos, width=width, height=height)),
|
||||
("Naive", nearest_resize(naive, width=width, height=height)),
|
||||
("Diffusion", nearest_resize(diffusion, width=width, height=height)),
|
||||
]
|
||||
|
|
@ -79,6 +81,7 @@ def run_compare(
|
|||
)
|
||||
source = result.source_rgba
|
||||
palette = load_palette(palette_path) if palette_path else None
|
||||
lanczos = lanczos_resize_baseline(source, width=result.inference.target_width, height=result.inference.target_height)
|
||||
naive = naive_resize_baseline(source, width=result.inference.target_width, height=result.inference.target_height)
|
||||
diffusion = error_diffusion_baseline(
|
||||
source,
|
||||
|
|
@ -86,10 +89,11 @@ def run_compare(
|
|||
height=result.inference.target_height,
|
||||
palette=palette,
|
||||
)
|
||||
_contact_sheet(source, result.output_rgba, naive, diffusion, diagnostics_path / "compare-sheet.png")
|
||||
_contact_sheet(source, result.output_rgba, lanczos, naive, diffusion, diagnostics_path / "compare-sheet.png")
|
||||
rows = []
|
||||
for name, image in (
|
||||
("optimized", result.output_rgba),
|
||||
("lanczos", lanczos),
|
||||
("naive", naive),
|
||||
("diffusion", diffusion),
|
||||
):
|
||||
|
|
|
|||
|
|
@ -45,3 +45,9 @@ def bilinear_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.BILINEAR)
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue