Refactor app layout and load defaults from config
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
"""Logic utilities and mixins for processing and configuration."""
|
||||
|
||||
from .constants import BASE_DIR, DEFAULTS, IMAGES_DIR, PREVIEW_MAX_SIZE
|
||||
from .image_processing import ImageProcessingMixin
|
||||
from .reset import ResetMixin
|
||||
|
||||
__all__ = [
|
||||
"BASE_DIR",
|
||||
"DEFAULTS",
|
||||
"IMAGES_DIR",
|
||||
"PREVIEW_MAX_SIZE",
|
||||
"ImageProcessingMixin",
|
||||
"ResetMixin",
|
||||
]
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Shared configuration constants for the application."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
try: # Python 3.11 and above
|
||||
import tomllib # type: ignore[attr-defined]
|
||||
except ModuleNotFoundError: # pragma: no cover - fallback for <3.11
|
||||
with contextlib.suppress(ModuleNotFoundError):
|
||||
import tomli as tomllib # type: ignore[assignment]
|
||||
if "tomllib" not in globals():
|
||||
tomllib = None # type: ignore[assignment]
|
||||
|
||||
PREVIEW_MAX_SIZE = (1200, 800)
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parents[2]
|
||||
IMAGES_DIR = BASE_DIR / "images"
|
||||
CONFIG_FILE = BASE_DIR / "config.toml"
|
||||
|
||||
_DEFAULTS_BASE = {
|
||||
"hue_min": 250.0,
|
||||
"hue_max": 310.0,
|
||||
"sat_min": 15.0,
|
||||
"val_min": 15.0,
|
||||
"val_max": 100.0,
|
||||
"alpha": 120,
|
||||
}
|
||||
|
||||
_DEFAULT_TYPES: dict[str, Callable[[Any], Any]] = {
|
||||
"hue_min": float,
|
||||
"hue_max": float,
|
||||
"sat_min": float,
|
||||
"val_min": float,
|
||||
"val_max": float,
|
||||
"alpha": int,
|
||||
}
|
||||
|
||||
|
||||
def _load_default_overrides() -> dict[str, Any]:
|
||||
"""Load default slider overrides from config.toml if available."""
|
||||
if tomllib is None or not CONFIG_FILE.exists():
|
||||
return {}
|
||||
decode_error = getattr(tomllib, "TOMLDecodeError", ValueError) # type: ignore[attr-defined]
|
||||
try:
|
||||
with CONFIG_FILE.open("rb") as handle:
|
||||
data = tomllib.load(handle)
|
||||
except (OSError, AttributeError, decode_error, TypeError): # type: ignore[arg-type]
|
||||
return {}
|
||||
settings = data.get("defaults")
|
||||
if not isinstance(settings, dict):
|
||||
return {}
|
||||
overrides: dict[str, Any] = {}
|
||||
for key, caster in _DEFAULT_TYPES.items():
|
||||
if key not in settings:
|
||||
continue
|
||||
try:
|
||||
overrides[key] = caster(settings[key])
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
return overrides
|
||||
|
||||
|
||||
DEFAULTS = {**_DEFAULTS_BASE, **_load_default_overrides()}
|
||||
@@ -0,0 +1,231 @@
|
||||
"""Image loading, processing, and statistics logic."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import colorsys
|
||||
from pathlib import Path
|
||||
from typing import Iterable, Tuple
|
||||
|
||||
from tkinter import filedialog, messagebox
|
||||
|
||||
from PIL import Image, ImageDraw, ImageTk
|
||||
|
||||
from .constants import IMAGES_DIR, PREVIEW_MAX_SIZE
|
||||
|
||||
|
||||
class ImageProcessingMixin:
|
||||
"""Handles all image related operations."""
|
||||
|
||||
image_path: Path | None
|
||||
orig_img: Image.Image | None
|
||||
preview_img: Image.Image | None
|
||||
preview_tk: ImageTk.PhotoImage | None
|
||||
overlay_tk: ImageTk.PhotoImage | None
|
||||
|
||||
def load_image(self) -> None:
|
||||
default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd()
|
||||
path = filedialog.askopenfilename(
|
||||
title="Bild wählen",
|
||||
filetypes=[("Images", "*.webp *.png *.jpg *.jpeg *.bmp")],
|
||||
initialdir=str(default_dir),
|
||||
)
|
||||
if not path:
|
||||
return
|
||||
self.image_path = Path(path)
|
||||
try:
|
||||
image = Image.open(path).convert("RGBA")
|
||||
except Exception as exc:
|
||||
messagebox.showerror("Fehler", f"Bild konnte nicht geladen werden: {exc}")
|
||||
return
|
||||
self.orig_img = image
|
||||
self.prepare_preview()
|
||||
self.update_preview()
|
||||
self.status.config(
|
||||
text=f"Geladen: {self.image_path.name} — {self.orig_img.width}x{self.orig_img.height}"
|
||||
)
|
||||
|
||||
def save_overlay(self) -> None:
|
||||
if self.orig_img is None:
|
||||
messagebox.showinfo("Info", "Kein Bild geladen.")
|
||||
return
|
||||
if self.preview_img is None:
|
||||
messagebox.showerror("Fehler", "Keine Preview vorhanden.")
|
||||
return
|
||||
|
||||
overlay = self._build_overlay_image(
|
||||
self.orig_img,
|
||||
self.exclude_rects,
|
||||
alpha=int(self.alpha.get()),
|
||||
scale_from_preview=self.preview_img.size,
|
||||
is_purple_fn=self.is_purple_pixel,
|
||||
)
|
||||
merged = Image.alpha_composite(self.orig_img.convert("RGBA"), overlay)
|
||||
|
||||
out_path = filedialog.asksaveasfilename(
|
||||
defaultextension=".png", filetypes=[("PNG", "*.png")], title="Overlay speichern als"
|
||||
)
|
||||
if not out_path:
|
||||
return
|
||||
merged.save(out_path)
|
||||
messagebox.showinfo("Gespeichert", f"Overlay gespeichert: {out_path}")
|
||||
|
||||
def prepare_preview(self) -> None:
|
||||
if self.orig_img is None:
|
||||
return
|
||||
width, height = self.orig_img.size
|
||||
max_w, max_h = PREVIEW_MAX_SIZE
|
||||
scale = min(max_w / width, max_h / height, 1.0)
|
||||
size = (max(1, int(width * scale)), max(1, int(height * scale)))
|
||||
self.preview_img = self.orig_img.resize(size, Image.LANCZOS)
|
||||
self.preview_tk = ImageTk.PhotoImage(self.preview_img)
|
||||
self.canvas_orig.delete("all")
|
||||
self.canvas_orig.config(width=size[0], height=size[1])
|
||||
self.canvas_overlay.config(width=size[0], height=size[1])
|
||||
self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
|
||||
|
||||
def update_preview(self) -> None:
|
||||
if self.preview_img is None:
|
||||
return
|
||||
merged = self.create_overlay_preview()
|
||||
if merged is None:
|
||||
return
|
||||
self.overlay_tk = ImageTk.PhotoImage(merged)
|
||||
self.canvas_overlay.delete("all")
|
||||
self.canvas_overlay.create_image(0, 0, anchor="nw", image=self.overlay_tk)
|
||||
|
||||
self.canvas_orig.delete("all")
|
||||
self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
|
||||
for (x0, y0, x1, y1) in self.exclude_rects:
|
||||
self.canvas_orig.create_rectangle(x0, y0, x1, y1, outline="yellow", width=3)
|
||||
|
||||
stats = self.compute_stats_preview()
|
||||
if stats:
|
||||
p_all, t_all = stats["all"]
|
||||
p_keep, t_keep = stats["keep"]
|
||||
p_ex, t_ex = stats["excl"]
|
||||
r_with = (p_keep / t_keep * 100) if t_keep else 0.0
|
||||
r_no = (p_all / t_all * 100) if t_all else 0.0
|
||||
excl_share = (t_ex / t_all * 100) if t_all else 0.0
|
||||
excl_purp = (p_ex / t_ex * 100) if t_ex else 0.0
|
||||
self.ratio_label.config(
|
||||
text=(
|
||||
f"Purple (mit Excludes): {r_with:.2f}% | "
|
||||
f"Purple (ohne Excludes): {r_no:.2f}% | "
|
||||
f"Excluded: {excl_share:.2f}% vom Bild, davon {excl_purp:.2f}% purple"
|
||||
)
|
||||
)
|
||||
|
||||
bg = "#0f0f10" if self.theme == "dark" else "#1e1e1e"
|
||||
self.canvas_orig.configure(bg=bg)
|
||||
self.canvas_overlay.configure(bg=bg)
|
||||
|
||||
def create_overlay_preview(self) -> Image.Image | None:
|
||||
if self.preview_img is None:
|
||||
return None
|
||||
base = self.preview_img.convert("RGBA")
|
||||
overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
|
||||
draw = ImageDraw.Draw(overlay)
|
||||
pixels = base.load()
|
||||
width, height = base.size
|
||||
alpha = int(self.alpha.get())
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if self._is_excluded(x, y):
|
||||
continue
|
||||
r, g, b, a = pixels[x, y]
|
||||
if a == 0:
|
||||
continue
|
||||
if self.is_purple_pixel(r, g, b):
|
||||
draw.point((x, y), fill=(255, 0, 0, alpha))
|
||||
merged = Image.alpha_composite(base, overlay)
|
||||
for (x0, y0, x1, y1) in self.exclude_rects:
|
||||
ImageDraw.Draw(merged).rectangle([x0, y0, x1, y1], outline=(255, 215, 0, 200), width=3)
|
||||
return merged
|
||||
|
||||
def compute_stats_preview(self):
|
||||
if self.preview_img is None:
|
||||
return None
|
||||
px = self.preview_img.convert("RGBA").load()
|
||||
width, height = self.preview_img.size
|
||||
purple_all = total_all = 0
|
||||
purple_keep = total_keep = 0
|
||||
purple_excl = total_excl = 0
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
r, g, b, a = px[x, y]
|
||||
if a == 0:
|
||||
continue
|
||||
is_excluded = self._is_excluded(x, y)
|
||||
total_all += 1
|
||||
if self.is_purple_pixel(r, g, b):
|
||||
purple_all += 1
|
||||
if not is_excluded:
|
||||
total_keep += 1
|
||||
if self.is_purple_pixel(r, g, b):
|
||||
purple_keep += 1
|
||||
else:
|
||||
total_excl += 1
|
||||
if self.is_purple_pixel(r, g, b):
|
||||
purple_excl += 1
|
||||
return {"all": (purple_all, total_all), "keep": (purple_keep, total_keep), "excl": (purple_excl, total_excl)}
|
||||
|
||||
def is_purple_pixel(self, r, g, b) -> bool:
|
||||
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
|
||||
hue = h * 360.0
|
||||
hmin = float(self.hue_min.get())
|
||||
hmax = float(self.hue_max.get())
|
||||
smin = float(self.sat_min.get()) / 100.0
|
||||
vmin = float(self.val_min.get()) / 100.0
|
||||
vmax = float(self.val_max.get()) / 100.0
|
||||
if hmin <= hmax:
|
||||
hue_ok = hmin <= hue <= hmax
|
||||
else:
|
||||
hue_ok = (hue >= hmin) or (hue <= hmax)
|
||||
return hue_ok and (s >= smin) and (v >= vmin) and (v <= vmax)
|
||||
|
||||
def _is_excluded(self, x: int, y: int) -> bool:
|
||||
return any(x0 <= x <= x1 and y0 <= y <= y1 for (x0, y0, x1, y1) in self.exclude_rects)
|
||||
|
||||
@staticmethod
|
||||
def _map_preview_excludes(
|
||||
excludes: Iterable[Tuple[int, int, int, int]],
|
||||
orig_size: Tuple[int, int],
|
||||
preview_size: Tuple[int, int],
|
||||
) -> list[Tuple[int, int, int, int]]:
|
||||
scale_x = orig_size[0] / preview_size[0]
|
||||
scale_y = orig_size[1] / preview_size[1]
|
||||
mapped = []
|
||||
for x0, y0, x1, y1 in excludes:
|
||||
mapped.append(
|
||||
(int(x0 * scale_x), int(y0 * scale_y), int(x1 * scale_x), int(y1 * scale_y))
|
||||
)
|
||||
return mapped
|
||||
|
||||
@classmethod
|
||||
def _build_overlay_image(
|
||||
cls,
|
||||
image: Image.Image,
|
||||
excludes_preview: Iterable[Tuple[int, int, int, int]],
|
||||
*,
|
||||
alpha: int,
|
||||
scale_from_preview: Tuple[int, int],
|
||||
is_purple_fn,
|
||||
) -> Image.Image:
|
||||
overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
|
||||
draw = ImageDraw.Draw(overlay)
|
||||
pixels = image.load()
|
||||
width, height = image.size
|
||||
excludes = cls._map_preview_excludes(excludes_preview, image.size, scale_from_preview)
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if any(x0 <= x <= x1 and y0 <= y <= y1 for (x0, y0, x1, y1) in excludes):
|
||||
continue
|
||||
r, g, b, a = pixels[x, y]
|
||||
if a == 0:
|
||||
continue
|
||||
if is_purple_fn(r, g, b):
|
||||
draw.point((x, y), fill=(255, 0, 0, alpha))
|
||||
return overlay
|
||||
|
||||
|
||||
__all__ = ["ImageProcessingMixin"]
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Utility mixin for restoring default slider values."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class ResetMixin:
|
||||
def reset_sliders(self):
|
||||
self.hue_min.set(self.DEFAULTS["hue_min"])
|
||||
self.hue_max.set(self.DEFAULTS["hue_max"])
|
||||
self.sat_min.set(self.DEFAULTS["sat_min"])
|
||||
self.val_min.set(self.DEFAULTS["val_min"])
|
||||
self.val_max.set(self.DEFAULTS["val_max"])
|
||||
self.alpha.set(self.DEFAULTS["alpha"])
|
||||
self.update_preview()
|
||||
|
||||
|
||||
__all__ = ["ResetMixin"]
|
||||
Reference in New Issue
Block a user