Compare commits
4
Commits
9ff56ce7ef
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e42b24110 | ||
|
|
e13bef7f52 | ||
|
|
46e1eb0925 | ||
|
|
dab226f55e |
@@ -10,15 +10,15 @@
|
||||
- **High-Performance Image Processing:** Native, vectorized NumPy operations for lightning-fast HSV conversion and color matching.
|
||||
- **Automatic Background Exclusion:** Intelligently ignores background pixels (configurable in `config.toml`) to ensure they don't interfere with your analysis.
|
||||
- **Grouping Score (Clustering):** A high-performance 9x9 box-sum algorithm that rewards solid "splashes" of color and penalizes thin lines or fragmented noise.
|
||||
- **Batch Processing & Customizable Export:** Load a folder of images and instantly export `icra_stats.csv` and `icra_settings.json`. Features a dedicated **Weighting Dialog** to customize the impact of each core component.
|
||||
- **Import/Export Settings:** Save your HSV ranges, exclusion zones, and weighting preferences to a JSON file and reload them later for consistent analysis across different sessions.
|
||||
- **Batch Processing & Customizable Export:** Load a folder of images and instantly export `icra_stats.csv` and `icra_settings.json`. Features a dedicated **Weighting Dialog** to customize the impact of each core component, outputting precise percentage-based column headers.
|
||||
- **Import/Export Settings:** Save your HSV ranges, exclusion zones, weighting preferences, and custom overlay colors to a JSON file and reload them later for consistent analysis across different sessions.
|
||||
- **Eyedropper Tool:** Quickly pick target matching colors directly from the image canvas.
|
||||
- **Advanced Selection:** Support for exclusion zones (rectangles and free-draw polygons) overlaid on the image, all dynamically rendered via `QGraphicsView`.
|
||||
- **Modern UI & UX:**
|
||||
- Drag-and-drop support for files and folders.
|
||||
- Custom dark and light themes with native-feeling borderless titlebars and a categorized menu bar.
|
||||
- Custom dark and light themes with native-feeling borderless titlebars.
|
||||
- Context-aware hover tooltips across the cleanly categorized menu bar.
|
||||
- Window size and position persistence between launches.
|
||||
- Keyboard shortcuts for rapid workflow.
|
||||
- **Configurable:** Uses `config.toml` to drive everything from overlay matching colors to default sliders and application language (`en`/`de`).
|
||||
|
||||
## Core Concepts
|
||||
@@ -43,6 +43,9 @@ To ensure accurate statistics, ICRA automatically filters out the image backgrou
|
||||
### 4. Exclusion Zones
|
||||
Use the **Exclusion Tool** to draw rectangles or polygons over areas you want to ignore. This is essential for focusing your analysis on specific parts of a complex image while ignoring background noise or irrelevant details.
|
||||
|
||||
### 5. Configuration (`config.toml`)
|
||||
The application is highly customizable via `config.toml`. This file is included in the repository and contains detailed comments explaining each available setting—from default sliders and language selection to UI masking colors and initial background exclusion tolerances.
|
||||
|
||||
## Typical Workflow
|
||||
|
||||
1. **Load Data:** Drag and drop a folder or use the `File` menu to load your images.
|
||||
|
||||
+5
-4
@@ -40,11 +40,12 @@ def _load_translations(lang: str) -> Dict[str, str]:
|
||||
data = tomllib.load(handle)
|
||||
except (OSError, AttributeError, ValueError, TypeError): # type: ignore[arg-type]
|
||||
return {}
|
||||
translations = data.get("translations")
|
||||
if not isinstance(translations, dict):
|
||||
return {}
|
||||
|
||||
# Merge all dictionaries found in the TOML (e.g. [translations] and [tooltip])
|
||||
out: Dict[str, str] = {}
|
||||
for key, value in translations.items():
|
||||
for section_name, section_data in data.items():
|
||||
if isinstance(section_data, dict):
|
||||
for key, value in section_data.items():
|
||||
if isinstance(key, str) and isinstance(value, str):
|
||||
out[key] = value
|
||||
return out
|
||||
|
||||
+31
-5
@@ -3,8 +3,8 @@
|
||||
"toolbar.open_image" = "Bild laden"
|
||||
"toolbar.open_folder" = "Ordner laden"
|
||||
"toolbar.choose_color" = "Farbe wählen"
|
||||
"toolbar.pick_from_image" = "Farbe aus Bild klicken"
|
||||
"toolbar.save_overlay" = "Overlay speichern"
|
||||
"toolbar.pick_from_image" = "Farbe aus Bild auswählen"
|
||||
"toolbar.save_overlay" = "Bild speichern"
|
||||
"toolbar.clear_excludes" = "Ausschlüsse löschen"
|
||||
"toolbar.toggle_free_draw" = "Freihandmodus umschalten"
|
||||
"toolbar.undo_exclude" = "Letzten Ausschluss entfernen"
|
||||
@@ -25,6 +25,7 @@
|
||||
"status.pick_mode_ended" = "Pick-Modus beendet."
|
||||
"status.pick_mode_from_image" = "Farbe vom Bild gewählt: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
|
||||
"palette.current" = "Farbe:"
|
||||
"palette.overlay_color" = "Overlay:"
|
||||
"palette.more" = "Weitere Farben:"
|
||||
"palette.swatch.red" = "Rot"
|
||||
"palette.swatch.orange" = "Orange"
|
||||
@@ -45,17 +46,19 @@
|
||||
"sliders.val_max" = "Helligkeit Max (%)"
|
||||
"sliders.alpha" = "Overlay Alpha"
|
||||
"stats.placeholder" = "Markierungen (mit Ausschlüssen): —"
|
||||
"stats.summary" = "Wertung: {score:.2f}% | Treffer (mit Exkl.): {with_pct:.2f}% | Treffer: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Gruppierung: {grouping:.1f}% | Ausgeschlossen: {excluded_pct:.2f}%"
|
||||
"stats.summary" = "Gesamtwertung: {score:.2f}% | Treffer (m. Ausschl.): {with_pct:.2f}% | Treffer: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Gruppierung: {grouping:.1f}% | Kontinuität: {continuity:.1f}% | Rand: {border:.1f}%"
|
||||
"stats.brightness_label" = "Helligkeit"
|
||||
"stats.darkness_label" = "Dunkelheit"
|
||||
"stats.grouping_label" = "Gruppierung"
|
||||
"stats.continuity_label" = "Kontinuität"
|
||||
"stats.border_label" = "Rand"
|
||||
"menu.copy" = "Kopieren"
|
||||
"dialog.info_title" = "Info"
|
||||
"dialog.error_title" = "Fehler"
|
||||
"dialog.saved_title" = "Gespeichert"
|
||||
"dialog.open_image_title" = "Bild wählen"
|
||||
"dialog.open_folder_title" = "Ordner mit Bildern wählen"
|
||||
"dialog.save_overlay_title" = "Overlay speichern als"
|
||||
"dialog.save_overlay_title" = "Bild speichern als"
|
||||
"dialog.choose_color_title" = "Farbe wählen"
|
||||
"dialog.images_filter" = "Bilder"
|
||||
"dialog.folder_not_found" = "Der Ordner wurde nicht gefunden."
|
||||
@@ -81,7 +84,9 @@
|
||||
"toolbar.export_folder" = "Ordner-Statistik"
|
||||
"menu.file" = "Datei"
|
||||
"menu.edit" = "Bearbeiten"
|
||||
"menu.exclusions" = "Ausschlüsse"
|
||||
"menu.view" = "Ansicht"
|
||||
"menu.view_log" = "Protokoll anzeigen"
|
||||
"menu.tools" = "Werkzeuge"
|
||||
|
||||
"toolbar.pull_patterns" = "Muster-Bilder laden"
|
||||
@@ -98,5 +103,26 @@
|
||||
"dialog.weight_match_keep" = "Treffer (Behalten) %"
|
||||
"dialog.weight_brightness" = "Helligkeit/Dunkelheit %"
|
||||
"dialog.weight_grouping" = "Gruppierung %"
|
||||
"dialog.weight_continuity" = "Kontinuität %"
|
||||
"dialog.weight_border" = "Rand Sauberkeit %"
|
||||
"dialog.total_weight" = "Gesamt:"
|
||||
"dialog.weight_error" = "Die Summe muss genau 100% sein (aktuell {total}%)."
|
||||
"dialog.weight_error" = "Gewichtungen müssen exakt 100% ergeben (aktuell {total}%)."
|
||||
|
||||
[tooltip]
|
||||
"tooltip.open_image" = "Öffnet ein einzelnes Bild zur Analyse"
|
||||
"tooltip.open_folder" = "Öffnet einen Ordner mit Bildern für die Stapelverarbeitung"
|
||||
"tooltip.export_folder" = "Exportiert die Ergebnisse für den aktuellen Ordner als CSV"
|
||||
"tooltip.export_settings" = "Speichert die aktuellen Schieberegler- und Farbeinstellungen in einer JSON-Datei"
|
||||
"tooltip.import_settings" = "Lädt gespeicherte Einstellungen aus einer JSON-Datei"
|
||||
"tooltip.save_overlay" = "Speichert das aktuell sichtbare zusammengesetzte Overlay-Bild"
|
||||
"tooltip.open_app_folder" = "Öffnet das Installationsverzeichnis der Anwendung"
|
||||
"tooltip.reset_sliders" = "Setzt alle Schieberegler auf ihre Standardwerte zurück"
|
||||
"tooltip.pick_from_image" = "Klicken Sie auf das Bild, um eine Zielfarbe auszuwählen"
|
||||
"tooltip.prefer_dark" = "Priorisiert dunklere Farben gegenüber helleren bei der Gesamtbewertung"
|
||||
"tooltip.undo_exclude" = "Macht die zuletzt gezeichnete Ausschlussform rückgängig"
|
||||
"tooltip.clear_excludes" = "Entfernt alle Ausschlussformen aus dem Bild"
|
||||
"tooltip.toggle_free_draw" = "Wechselt zwischen dem Zeichnen von Rechtecken und Freiform-Polygonen"
|
||||
"tooltip.exclude_bg" = "Ignoriert basierend auf den Einstellungen automatisch Hintergrundfarben"
|
||||
"tooltip.pull_patterns" = "Lädt Musterbilder von einer Remote-Quelle herunter"
|
||||
"tooltip.toggle_theme" = "Wechselt zwischen hellem und dunklem UI-Design"
|
||||
"tooltip.view_log" = "Zeigt aktuelle Anwendungsstatusmeldungen und Protokolle an"
|
||||
|
||||
+30
-4
@@ -3,8 +3,8 @@
|
||||
"toolbar.open_image" = "Open image"
|
||||
"toolbar.open_folder" = "Open folder"
|
||||
"toolbar.choose_color" = "Choose color"
|
||||
"toolbar.pick_from_image" = "Pick from image"
|
||||
"toolbar.save_overlay" = "Save overlay"
|
||||
"toolbar.pick_from_image" = "Select color from image"
|
||||
"toolbar.save_overlay" = "Save Image"
|
||||
"toolbar.clear_excludes" = "Clear exclusions"
|
||||
"toolbar.toggle_free_draw" = "Toggle free-draw"
|
||||
"toolbar.undo_exclude" = "Undo last exclusion"
|
||||
@@ -25,6 +25,7 @@
|
||||
"status.pick_mode_ended" = "Pick mode ended."
|
||||
"status.pick_mode_from_image" = "Color picked from image: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
|
||||
"palette.current" = "Color:"
|
||||
"palette.overlay_color" = "Overlay:"
|
||||
"palette.more" = "More colors:"
|
||||
"palette.swatch.red" = "Red"
|
||||
"palette.swatch.orange" = "Orange"
|
||||
@@ -45,17 +46,19 @@
|
||||
"sliders.val_max" = "Value max (%)"
|
||||
"sliders.alpha" = "Overlay alpha"
|
||||
"stats.placeholder" = "Matches (with exclusions): —"
|
||||
"stats.summary" = "Score: {score:.2f}% | Matches (w/ excl.): {with_pct:.2f}% | Matches: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Grouping: {grouping:.1f}% | Excluded: {excluded_pct:.2f}%"
|
||||
"stats.summary" = "Composite Score: {score:.2f}% | Matches (w/ excl.): {with_pct:.2f}% | Matches: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Grouping: {grouping:.1f}% | Continuity: {continuity:.1f}% | Border: {border:.1f}%"
|
||||
"stats.brightness_label" = "Brightness"
|
||||
"stats.darkness_label" = "Darkness"
|
||||
"stats.grouping_label" = "Grouping"
|
||||
"stats.continuity_label" = "Continuity"
|
||||
"stats.border_label" = "Border"
|
||||
"menu.copy" = "Copy"
|
||||
"dialog.info_title" = "Info"
|
||||
"dialog.error_title" = "Error"
|
||||
"dialog.saved_title" = "Saved"
|
||||
"dialog.open_image_title" = "Select image"
|
||||
"dialog.open_folder_title" = "Select folder"
|
||||
"dialog.save_overlay_title" = "Save overlay as"
|
||||
"dialog.save_overlay_title" = "Save Image as"
|
||||
"dialog.choose_color_title" = "Choose color"
|
||||
"dialog.images_filter" = "Images"
|
||||
"dialog.folder_not_found" = "The folder could not be found."
|
||||
@@ -81,7 +84,9 @@
|
||||
"toolbar.export_folder" = "Export Folder Stats"
|
||||
"menu.file" = "File"
|
||||
"menu.edit" = "Edit"
|
||||
"menu.exclusions" = "Exclusions"
|
||||
"menu.view" = "View"
|
||||
"menu.view_log" = "View Log"
|
||||
"menu.tools" = "Tools"
|
||||
|
||||
"toolbar.pull_patterns" = "Pull Pattern Images"
|
||||
@@ -98,5 +103,26 @@
|
||||
"dialog.weight_match_keep" = "Match (Keep) %"
|
||||
"dialog.weight_brightness" = "Brightness/Darkness %"
|
||||
"dialog.weight_grouping" = "Grouping %"
|
||||
"dialog.weight_continuity" = "Continuity %"
|
||||
"dialog.weight_border" = "Border Cleanliness %"
|
||||
"dialog.total_weight" = "Total:"
|
||||
"dialog.weight_error" = "Weights must sum exactly to 100% (currently {total}%)."
|
||||
|
||||
[tooltip]
|
||||
"tooltip.open_image" = "Open a single image for analysis"
|
||||
"tooltip.open_folder" = "Open a folder of images to process in batch"
|
||||
"tooltip.export_folder" = "Export CSV results for the current folder"
|
||||
"tooltip.export_settings" = "Save current slider and color settings to a JSON file"
|
||||
"tooltip.import_settings" = "Load saved settings from a JSON file"
|
||||
"tooltip.save_overlay" = "Save the currently visible composite overlay image"
|
||||
"tooltip.open_app_folder" = "Open the application installation directory"
|
||||
"tooltip.reset_sliders" = "Reset all sliders to their default values"
|
||||
"tooltip.pick_from_image" = "Click on the image to select a target color"
|
||||
"tooltip.prefer_dark" = "Prioritize darker colors over brighter ones in the composite score"
|
||||
"tooltip.undo_exclude" = "Undo the last exclusion shape drawn"
|
||||
"tooltip.clear_excludes" = "Remove all exclusion shapes from the image"
|
||||
"tooltip.toggle_free_draw" = "Toggle between drawing rectangles and free-form polygons"
|
||||
"tooltip.exclude_bg" = "Automatically ignore background colors based on settings"
|
||||
"tooltip.pull_patterns" = "Download pattern images from a remote source"
|
||||
"tooltip.toggle_theme" = "Switch between light and dark UI themes"
|
||||
"tooltip.view_log" = "View recent application status messages and logs"
|
||||
|
||||
@@ -8,6 +8,7 @@ from .constants import (
|
||||
OVERLAY_COLOR,
|
||||
EXCLUDE_BG_COLOR,
|
||||
EXCLUDE_BG_TOLERANCE,
|
||||
WEIGHTS,
|
||||
PREVIEW_MAX_SIZE,
|
||||
RESET_EXCLUSIONS_ON_IMAGE_CHANGE,
|
||||
SUPPORTED_IMAGE_EXTENSIONS,
|
||||
@@ -20,7 +21,7 @@ __all__ = [
|
||||
"LANGUAGE",
|
||||
"OVERLAY_COLOR",
|
||||
"EXCLUDE_BG_COLOR",
|
||||
"EXCLUDE_BG_TOLERANCE",
|
||||
"WEIGHTS",
|
||||
"PREVIEW_MAX_SIZE",
|
||||
"RESET_EXCLUSIONS_ON_IMAGE_CHANGE",
|
||||
"SUPPORTED_IMAGE_EXTENSIONS",
|
||||
|
||||
@@ -101,6 +101,15 @@ _OPTION_DEFAULTS = {
|
||||
"exclude_bg_tolerance": 5,
|
||||
}
|
||||
|
||||
_WEIGHT_DEFAULTS = {
|
||||
"match_all": 20,
|
||||
"match_keep": 20,
|
||||
"brightness": 10,
|
||||
"grouping": 10,
|
||||
"continuity": 20,
|
||||
"border": 20,
|
||||
}
|
||||
|
||||
|
||||
def _extract_options(data: dict[str, Any]) -> dict[str, Any]:
|
||||
section = data.get("options")
|
||||
@@ -122,6 +131,18 @@ def _extract_options(data: dict[str, Any]) -> dict[str, Any]:
|
||||
return result
|
||||
|
||||
|
||||
def _extract_weights(data: dict[str, Any]) -> dict[str, int]:
|
||||
section = data.get("weights")
|
||||
if not isinstance(section, dict):
|
||||
return {}
|
||||
result: dict[str, int] = {}
|
||||
for key in _WEIGHT_DEFAULTS:
|
||||
value = section.get(key)
|
||||
if isinstance(value, int):
|
||||
result[key] = max(0, min(100, value))
|
||||
return result
|
||||
|
||||
|
||||
DEFAULTS = {**_DEFAULTS_BASE, **_extract_default_overrides(_CONFIG_DATA)}
|
||||
LANGUAGE = _extract_language(_CONFIG_DATA)
|
||||
OPTIONS = {**_OPTION_DEFAULTS, **_extract_options(_CONFIG_DATA)}
|
||||
@@ -129,3 +150,4 @@ RESET_EXCLUSIONS_ON_IMAGE_CHANGE = OPTIONS["reset_exclusions_on_image_change"]
|
||||
OVERLAY_COLOR = OPTIONS["overlay_color"]
|
||||
EXCLUDE_BG_COLOR = OPTIONS["exclude_bg_color"]
|
||||
EXCLUDE_BG_TOLERANCE = OPTIONS["exclude_bg_tolerance"]
|
||||
WEIGHTS = {**_WEIGHT_DEFAULTS, **_extract_weights(_CONFIG_DATA)}
|
||||
|
||||
+2
-1
@@ -46,11 +46,12 @@ def create_application() -> QtWidgets.QApplication:
|
||||
def run() -> int:
|
||||
"""Run the PySide6 GUI."""
|
||||
app = create_application()
|
||||
from app.logic import OVERLAY_COLOR, EXCLUDE_BG_COLOR, EXCLUDE_BG_TOLERANCE
|
||||
from app.logic import OVERLAY_COLOR, EXCLUDE_BG_COLOR, EXCLUDE_BG_TOLERANCE, WEIGHTS
|
||||
window = MainWindow(
|
||||
language=LANGUAGE,
|
||||
defaults=DEFAULTS.copy(),
|
||||
reset_exclusions=RESET_EXCLUSIONS_ON_IMAGE_CHANGE,
|
||||
weights=WEIGHTS.copy(),
|
||||
overlay_color=OVERLAY_COLOR,
|
||||
exclude_bg_color=EXCLUDE_BG_COLOR,
|
||||
exclude_bg_tolerance=EXCLUDE_BG_TOLERANCE,
|
||||
|
||||
+361
-8
@@ -24,6 +24,8 @@ class Stats:
|
||||
total_excl: int = 0
|
||||
brightness_score: float = 0.0
|
||||
grouping_score: float = 0.0
|
||||
continuity_score: float = 0.0
|
||||
border_score: float = 0.0
|
||||
prefer_dark: bool = False
|
||||
|
||||
@property
|
||||
@@ -36,16 +38,20 @@ class Stats:
|
||||
pct_all = (self.matches_all / self.total_all * 100) if self.total_all else 0.0
|
||||
pct_keep = (self.matches_keep / self.total_keep * 100) if self.total_keep else 0.0
|
||||
|
||||
# weights keys: match_all, match_keep, brightness, grouping
|
||||
# weights keys: match_all, match_keep, brightness, grouping, continuity, border
|
||||
w_all = weights.get("match_all", 30) / 100.0
|
||||
w_keep = weights.get("match_keep", 50) / 100.0
|
||||
w_keep = weights.get("match_keep", 30) / 100.0
|
||||
w_bright = weights.get("brightness", 10) / 100.0
|
||||
w_group = weights.get("grouping", 10) / 100.0
|
||||
w_cont = weights.get("continuity", 10) / 100.0
|
||||
w_bord = weights.get("border", 10) / 100.0
|
||||
|
||||
return (w_all * pct_all +
|
||||
w_keep * pct_keep +
|
||||
w_bright * self.effective_brightness +
|
||||
w_group * self.grouping_score)
|
||||
w_group * self.grouping_score +
|
||||
w_cont * self.continuity_score +
|
||||
w_bord * self.border_score)
|
||||
|
||||
def summary(self, translate, weights: dict[str, int]) -> str:
|
||||
if self.total_all == 0:
|
||||
@@ -63,6 +69,8 @@ class Stats:
|
||||
brightness_label=brightness_label,
|
||||
brightness=self.effective_brightness,
|
||||
grouping=self.grouping_score,
|
||||
continuity=self.continuity_score,
|
||||
border=self.border_score,
|
||||
excluded_pct=excluded_pct,
|
||||
)
|
||||
|
||||
@@ -98,6 +106,221 @@ def _rgb_to_hsv_numpy(arr: np.ndarray) -> np.ndarray:
|
||||
return np.stack([h, s * 100.0, v * 100.0], axis=-1)
|
||||
|
||||
|
||||
def _calculate_border_score(mask: np.ndarray, val: np.ndarray, alpha_ch: np.ndarray, prefer_dark: bool, excl_mask: np.ndarray | None = None) -> float:
|
||||
"""Measure border cleanliness: penalizes extremely dark (or bright) pixels along the match perimeter.
|
||||
Uses Top-10% percentile to ensure local artifacts (halos) aren't diluted by clean edges.
|
||||
"""
|
||||
if not mask.any():
|
||||
return 100.0
|
||||
|
||||
dilated = mask.copy()
|
||||
# Manual morphological 1-pixel dilation
|
||||
dilated[:-1, :] |= mask[1:, :]
|
||||
dilated[1:, :] |= mask[:-1, :]
|
||||
dilated[:, :-1] |= mask[:, 1:]
|
||||
dilated[:, 1:] |= mask[:, :-1]
|
||||
|
||||
dil2 = dilated.copy()
|
||||
dil2[:-1, :] |= dilated[1:, :]
|
||||
dil2[1:, :] |= dilated[:-1, :]
|
||||
dil2[:, :-1] |= dilated[:, 1:]
|
||||
dil2[:, 1:] |= dilated[:, :-1]
|
||||
|
||||
# Target exterior pixels that aren't transparent and NOT excluded
|
||||
outer = dil2 & ~mask & (alpha_ch >= 128)
|
||||
if excl_mask is not None:
|
||||
outer &= ~excl_mask
|
||||
|
||||
if not outer.any():
|
||||
return 100.0
|
||||
|
||||
border_vals = val[outer]
|
||||
if prefer_dark:
|
||||
# Penalize super bright edges (white/silver > 60)
|
||||
penalties = np.clip(border_vals - 60.0, 0, None)
|
||||
else:
|
||||
# Penalize super dark edges (black/heavy shadows < 40)
|
||||
penalties = np.clip(40.0 - border_vals, 0, None)
|
||||
|
||||
# Hammer down harsh cuts: focus on the 'worst' parts of the border
|
||||
if not penalties.any():
|
||||
return 100.0
|
||||
|
||||
# Using 4th power penalty for 'catastrophic' edge detection.
|
||||
# A single pitch-black line (high diff) is now exponentially worse than a gray transition.
|
||||
total_penalty = np.sum(penalties ** 4)
|
||||
# Collector's Grade: only 20 pixels at full intensity (40^4)
|
||||
# are required for a 1% drop in the Border Score.
|
||||
max_penalty_sum = 20.0 * (40.0 ** 4)
|
||||
|
||||
score = 100.0 * (1.0 - (total_penalty / max_penalty_sum))
|
||||
return max(0.0, float(score))
|
||||
|
||||
|
||||
def _export_worker(args: tuple) -> tuple:
|
||||
"""Standalone worker for ProcessPoolExecutor batch export.
|
||||
|
||||
Receives ``(image_path, params)`` where *params* is the dict produced by
|
||||
``QtImageProcessor.get_export_params()``. Opens the image, runs the
|
||||
full stats pipeline, and returns a plain results tuple. No processor
|
||||
instance is needed so nothing has to be pickled.
|
||||
"""
|
||||
image_path, params = args
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
img_path = Path(image_path)
|
||||
hue_min = params["hue_min"]
|
||||
hue_max = params["hue_max"]
|
||||
sat_min = params["sat_min"]
|
||||
sat_max = params["sat_max"]
|
||||
val_min = params["val_min"]
|
||||
val_max = params["val_max"]
|
||||
exclude_bg = params["exclude_bg"]
|
||||
exclude_bg_rgb = tuple(params["exclude_bg_rgb"])
|
||||
exclude_bg_tolerance = params["exclude_bg_tolerance"]
|
||||
prefer_dark = params["prefer_dark"]
|
||||
exclude_shapes = params["exclude_shapes"]
|
||||
exclude_ref_size = params["exclude_ref_size"]
|
||||
|
||||
img = Image.open(img_path).convert("RGBA")
|
||||
arr = np.asarray(img, dtype=np.float32)
|
||||
|
||||
rgb = arr[..., :3] / 255.0
|
||||
alpha_ch = arr[..., 3].copy()
|
||||
|
||||
if exclude_bg:
|
||||
r_bg, g_bg, b_bg = exclude_bg_rgb
|
||||
tol = exclude_bg_tolerance
|
||||
bg_mask = (
|
||||
(np.abs(arr[..., 0] - r_bg) <= tol) &
|
||||
(np.abs(arr[..., 1] - g_bg) <= tol) &
|
||||
(np.abs(arr[..., 2] - b_bg) <= tol)
|
||||
)
|
||||
alpha_ch[bg_mask] = 0
|
||||
|
||||
hsv = _rgb_to_hsv_numpy(rgb)
|
||||
hue = hsv[..., 0]
|
||||
sat = hsv[..., 1]
|
||||
val = hsv[..., 2]
|
||||
|
||||
if hue_min <= hue_max:
|
||||
hue_ok = (hue >= hue_min) & (hue <= hue_max)
|
||||
else:
|
||||
hue_ok = (hue >= hue_min) | (hue <= hue_max)
|
||||
|
||||
match_mask = (
|
||||
hue_ok
|
||||
& (sat >= sat_min)
|
||||
& (sat <= sat_max)
|
||||
& (val >= val_min)
|
||||
& (val <= val_max)
|
||||
& (alpha_ch >= 128)
|
||||
)
|
||||
|
||||
# Build exclusion mask
|
||||
w, h = img.size
|
||||
if not exclude_shapes:
|
||||
excl_mask = np.zeros((h, w), dtype=bool)
|
||||
else:
|
||||
target_w, target_h = w, h
|
||||
ref_w, ref_h = exclude_ref_size or (w, h)
|
||||
sx = target_w / ref_w if ref_w > 0 else 1.0
|
||||
sy = target_h / ref_h if ref_h > 0 else 1.0
|
||||
mask_img = Image.new("L", (w, h), 0)
|
||||
draw = ImageDraw.Draw(mask_img)
|
||||
for shape in exclude_shapes:
|
||||
kind = shape.get("kind")
|
||||
if kind == "rect":
|
||||
x0, y0, x1, y1 = shape["coords"]
|
||||
draw.rectangle([x0 * sx, y0 * sy, x1 * sx, y1 * sy], fill=255)
|
||||
elif kind == "polygon":
|
||||
points = shape.get("points", [])
|
||||
if len(points) >= 3:
|
||||
scaled_pts = [(int(px * sx), int(py * sy)) for px, py in points]
|
||||
draw.polygon(scaled_pts, fill=255)
|
||||
excl_mask = np.asarray(mask_img, dtype=bool)
|
||||
|
||||
keep_match = match_mask & ~excl_mask
|
||||
visible = alpha_ch >= 128
|
||||
keep_visible = visible & ~excl_mask
|
||||
if keep_visible.any():
|
||||
v_vals = val[keep_visible]
|
||||
mean_v = float(v_vals.mean())
|
||||
std_v = float(v_vals.std())
|
||||
# Collector's Purity: multiply mean by a factor derived from variance
|
||||
# A perfectly uniform pattern (std=0) gets 100% of its mean.
|
||||
# Blotchy patterns (std > 10) get a significant reduction.
|
||||
purity_factor = max(0.0, 1.0 - (std_v / 20.0))
|
||||
brightness = mean_v * purity_factor
|
||||
else:
|
||||
brightness = 0.0
|
||||
|
||||
# Grouping score (inline for worker isolation)
|
||||
if not keep_match.any():
|
||||
grouping = 0.0
|
||||
else:
|
||||
mh, mw = keep_match.shape
|
||||
padded = np.pad(keep_match, 5, mode='constant', constant_values=0)
|
||||
cumsum = padded.astype(np.int32).cumsum(axis=0).cumsum(axis=1)
|
||||
y2, x2 = np.arange(9, 9 + mh)[:, None], np.arange(9, 9 + mw)
|
||||
y1_1, x1_1 = np.arange(0, mh)[:, None], np.arange(0, mw)
|
||||
window_sums = cumsum[y2, x2] - cumsum[y1_1, x2] - cumsum[y2, x1_1] + cumsum[y1_1, x1_1]
|
||||
neighbors = (window_sums - keep_match.astype(np.int32)).clip(min=0)
|
||||
match_neighbors = neighbors[keep_match]
|
||||
grouping = float(((match_neighbors / 80.0) ** 2).mean() * 100.0)
|
||||
|
||||
matches_all = int(match_mask[visible].sum())
|
||||
total_all = int(visible.sum())
|
||||
matches_keep = int(keep_match[visible].sum())
|
||||
total_keep = int(keep_visible.sum())
|
||||
|
||||
# Continuity score (inline for worker isolation)
|
||||
continuity = 0.0
|
||||
if keep_match.any():
|
||||
area = keep_match.sum()
|
||||
y_idx, x_idx = np.nonzero(keep_match)
|
||||
unvisited = set(zip(y_idx, x_idx))
|
||||
max_cc_area = 0
|
||||
while unvisited:
|
||||
start_node = unvisited.pop()
|
||||
queue = [start_node]
|
||||
cc_area = 0
|
||||
while queue:
|
||||
cy, cx = queue.pop()
|
||||
cc_area += 1
|
||||
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
|
||||
if (ny, nx) in unvisited:
|
||||
unvisited.remove((ny, nx))
|
||||
queue.append((ny, nx))
|
||||
if cc_area > max_cc_area:
|
||||
max_cc_area = cc_area
|
||||
continuity = float(max_cc_area / area * 100.0) if area > 0 else 0.0
|
||||
|
||||
eff_brightness = (100.0 - brightness) if prefer_dark else brightness
|
||||
|
||||
# Border Cleanliness score calculation using standalone util
|
||||
border = _calculate_border_score(keep_match, val, alpha_ch, prefer_dark, excl_mask)
|
||||
|
||||
pct_all = (matches_all / total_all * 100) if total_all else 0.0
|
||||
pct_keep = (matches_keep / total_keep * 100) if total_keep else 0.0
|
||||
|
||||
weights = params["weights"]
|
||||
w_all = weights.get("match_all", 30) / 100.0
|
||||
w_keep = weights.get("match_keep", 30) / 100.0
|
||||
w_bright = weights.get("brightness", 10) / 100.0
|
||||
w_group = weights.get("grouping", 10) / 100.0
|
||||
w_cont = weights.get("continuity", 10) / 100.0
|
||||
w_bord = weights.get("border", 10) / 100.0
|
||||
composite = (w_all * pct_all + w_keep * pct_keep + w_bright * eff_brightness +
|
||||
w_group * grouping + w_cont * continuity + w_bord * border)
|
||||
|
||||
img.close()
|
||||
return (img_path.name, pct_all, pct_keep, eff_brightness, grouping, continuity, border, composite)
|
||||
except Exception:
|
||||
return (img_path.name, None, None, None, None, None, None, None)
|
||||
|
||||
|
||||
class QtImageProcessor:
|
||||
"""Process images and build overlays for the Qt UI."""
|
||||
|
||||
@@ -143,10 +366,12 @@ class QtImageProcessor:
|
||||
self.exclude_bg_rgb: Tuple[int, int, int] = (31, 41, 55)
|
||||
self.exclude_bg_tolerance: int = 5
|
||||
self.weights: Dict[str, int] = {
|
||||
"match_all": 30,
|
||||
"match_keep": 50,
|
||||
"match_all": 20,
|
||||
"match_keep": 20,
|
||||
"brightness": 10,
|
||||
"grouping": 10
|
||||
"grouping": 10,
|
||||
"continuity": 20,
|
||||
"border": 20
|
||||
}
|
||||
|
||||
def set_defaults(self, defaults: dict) -> None:
|
||||
@@ -290,11 +515,24 @@ class QtImageProcessor:
|
||||
|
||||
# Brightness: mean Value (0-100) of ALL non-excluded visible pixels
|
||||
keep_visible = visible & ~excl_mask
|
||||
brightness = float(val[keep_visible].mean()) if keep_visible.any() else 0.0
|
||||
if keep_visible.any():
|
||||
v_vals = val[keep_visible]
|
||||
mean_v = float(v_vals.mean())
|
||||
std_v = float(v_vals.std())
|
||||
# Purity factor: subtract deviation from mean to punish blotchy patterns
|
||||
brightness = max(0.0, mean_v - (std_v * 1.5))
|
||||
else:
|
||||
brightness = 0.0
|
||||
|
||||
# Grouping: measure clustering of match_mask
|
||||
grouping = self._calculate_grouping_score(keep_match)
|
||||
|
||||
# Continuity: Measure connectivity of matched area
|
||||
continuity = self._calculate_continuity_score(keep_match)
|
||||
|
||||
# Border Cleanliness: Calculate hard edges based on preference
|
||||
border = _calculate_border_score(keep_match, val, alpha_ch, self.prefer_dark, excl_mask)
|
||||
|
||||
# Build overlay image
|
||||
overlay_arr = np.zeros((base.height, base.width, 4), dtype=np.uint8)
|
||||
overlay_arr[keep_match, 0] = self.overlay_r
|
||||
@@ -312,6 +550,8 @@ class QtImageProcessor:
|
||||
total_excl=total_excl,
|
||||
brightness_score=brightness,
|
||||
grouping_score=grouping,
|
||||
continuity_score=continuity,
|
||||
border_score=border,
|
||||
prefer_dark=self.prefer_dark,
|
||||
)
|
||||
|
||||
@@ -364,8 +604,20 @@ class QtImageProcessor:
|
||||
visible = alpha_ch >= 128
|
||||
matches_keep_count = int(keep_match[visible].sum())
|
||||
keep_visible = visible & ~excl_mask
|
||||
brightness = float(val[keep_visible].mean()) if keep_visible.any() else 0.0
|
||||
if keep_visible.any():
|
||||
v_vals = val[keep_visible]
|
||||
mean_v = float(v_vals.mean())
|
||||
std_v = float(v_vals.std())
|
||||
# Collector's Purity: multiply mean by a factor derived from variance
|
||||
# A perfectly uniform pattern (std=0) gets 100% of its mean.
|
||||
# Blotchy patterns (std > 10) get a significant reduction.
|
||||
purity_factor = max(0.0, 1.0 - (std_v / 20.0))
|
||||
brightness = mean_v * purity_factor
|
||||
else:
|
||||
brightness = 0.0
|
||||
grouping = self._calculate_grouping_score(keep_match)
|
||||
continuity = self._calculate_continuity_score(keep_match)
|
||||
border = _calculate_border_score(keep_match, val, alpha_ch, self.prefer_dark, excl_mask)
|
||||
|
||||
return Stats(
|
||||
matches_all=int(match_mask[visible].sum()),
|
||||
@@ -376,6 +628,8 @@ class QtImageProcessor:
|
||||
total_excl=int((visible & excl_mask).sum()),
|
||||
brightness_score=brightness,
|
||||
grouping_score=grouping,
|
||||
continuity_score=continuity,
|
||||
border_score=border,
|
||||
prefer_dark=self.prefer_dark,
|
||||
)
|
||||
|
||||
@@ -404,6 +658,79 @@ class QtImageProcessor:
|
||||
score = ( (match_neighbors / 80.0) ** 2 ).mean() * 100.0
|
||||
return float(score)
|
||||
|
||||
def _calculate_continuity_score(self, mask: np.ndarray) -> float:
|
||||
"""Measure continuity: largest connected component ratio and surface smoothness (0-100).
|
||||
Penalizes jaggedness and 'perforated' patterns with many internal holes.
|
||||
"""
|
||||
if not mask.any():
|
||||
return 0.0
|
||||
|
||||
area = mask.sum()
|
||||
|
||||
# 1. Connectivity Ratio
|
||||
y_idx, x_idx = np.nonzero(mask)
|
||||
unvisited = set(zip(y_idx, x_idx))
|
||||
max_cc_area = 0
|
||||
while unvisited:
|
||||
start_node = unvisited.pop()
|
||||
queue = [start_node]
|
||||
cc_area = 0
|
||||
while queue:
|
||||
cy, cx = queue.pop()
|
||||
cc_area += 1
|
||||
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
|
||||
if (ny, nx) in unvisited:
|
||||
unvisited.remove((ny, nx))
|
||||
queue.append((ny, nx))
|
||||
if cc_area > max_cc_area:
|
||||
max_cc_area = cc_area
|
||||
|
||||
connectivity = max_cc_area / area
|
||||
|
||||
# 2. Smoothness / Jaggedness (Perimeter-to-Area)
|
||||
# Theoretically perfect smoothness (circle) has perimeter 2*sqrt(pi*area)
|
||||
# We penalize departure from 'ideal' shape density
|
||||
eroded = mask.copy()
|
||||
eroded[:-1, :] &= mask[1:, :]
|
||||
eroded[1:, :] &= mask[:-1, :]
|
||||
eroded[:, :-1] &= mask[:, 1:]
|
||||
eroded[:, 1:] &= mask[:, :-1]
|
||||
perimeter = np.count_nonzero(mask ^ eroded)
|
||||
|
||||
# min_perim for a circle
|
||||
min_perim = 2.0 * np.sqrt(np.pi * area)
|
||||
# Jaggedness factor (0 is perfect, higher is messier)
|
||||
# We normalize by the expected complexity of the item (e.g. 15 for Karambit)
|
||||
# but here we use a general sensitivity factor
|
||||
jaggedness = max(0.0, (perimeter / min_perim) - 1.0)
|
||||
|
||||
# Penalty increases as jaggedness goes up.
|
||||
# For Urban Masked, we are more lenient (factor of 40 instead of 20)
|
||||
smoothness_factor = 1.0 / (1.0 + (jaggedness / 40.0))
|
||||
|
||||
# 3. Island Count Penalty
|
||||
# Premium patterns should be unified. Each separate piece (island)
|
||||
# adds a small deduction to the continuity score.
|
||||
y, x = np.nonzero(mask)
|
||||
unvisited = set(zip(y, x))
|
||||
islands = 0
|
||||
while unvisited:
|
||||
islands += 1
|
||||
node = unvisited.pop()
|
||||
q = [node]
|
||||
while q:
|
||||
cy, cx = q.pop()
|
||||
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
|
||||
if (ny, nx) in unvisited:
|
||||
unvisited.remove((ny, nx))
|
||||
q.append((ny, nx))
|
||||
|
||||
# Collector's factor: 2000 is now the baseline for Karambits.
|
||||
island_factor = max(0.0, 1.0 - (islands / 2000.0))
|
||||
|
||||
score = connectivity * smoothness_factor * island_factor * 100.0
|
||||
return float(score)
|
||||
|
||||
# helpers ----------------------------------------------------------------
|
||||
|
||||
def _matches(self, r: int, g: int, b: int) -> bool:
|
||||
@@ -546,7 +873,33 @@ class QtImageProcessor:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def get_export_params(self) -> dict:
|
||||
"""Extract all parameters needed for headless batch processing.
|
||||
|
||||
Called once before a batch export so that each worker receives
|
||||
a plain dict instead of re-reading instance attributes.
|
||||
"""
|
||||
return {
|
||||
"hue_min": float(self.hue_min),
|
||||
"hue_max": float(self.hue_max),
|
||||
"sat_min": float(self.sat_min),
|
||||
"sat_max": float(self.sat_max),
|
||||
"val_min": float(self.val_min),
|
||||
"val_max": float(self.val_max),
|
||||
"exclude_bg": self.exclude_bg,
|
||||
"exclude_bg_rgb": self.exclude_bg_rgb,
|
||||
"exclude_bg_tolerance": self.exclude_bg_tolerance,
|
||||
"prefer_dark": self.prefer_dark,
|
||||
"exclude_shapes": self.exclude_shapes,
|
||||
"exclude_ref_size": self.exclude_ref_size,
|
||||
"weights": self.weights,
|
||||
}
|
||||
|
||||
@property
|
||||
def exclude_bg_color_hex(self) -> str:
|
||||
r, g, b = self.exclude_bg_rgb
|
||||
return f"#{r:02x}{g:02x}{b:02x}"
|
||||
|
||||
@property
|
||||
def overlay_color_hex(self) -> str:
|
||||
return f"#{self.overlay_r:02x}{self.overlay_g:02x}{self.overlay_b:02x}"
|
||||
|
||||
+265
-103
@@ -153,6 +153,20 @@ class SliderControl(QtWidgets.QWidget):
|
||||
self.slider.valueChanged.connect(self._sync_value)
|
||||
layout.addWidget(self.slider)
|
||||
|
||||
# Allow slider to receive focus for keyboard control
|
||||
self.setFocusPolicy(QtCore.Qt.StrongFocus)
|
||||
self.slider.setFocusPolicy(QtCore.Qt.NoFocus)
|
||||
|
||||
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
|
||||
if event.key() == QtCore.Qt.Key_Left:
|
||||
self.slider.setValue(self.slider.value() - 1)
|
||||
event.accept()
|
||||
elif event.key() == QtCore.Qt.Key_Right:
|
||||
self.slider.setValue(self.slider.value() + 1)
|
||||
event.accept()
|
||||
else:
|
||||
super().keyPressEvent(event)
|
||||
|
||||
def _sync_value(self, value: int) -> None:
|
||||
self.value_edit.setText(str(value))
|
||||
self.value_changed.emit(self.key, value)
|
||||
@@ -434,11 +448,11 @@ class TitleBar(QtWidgets.QWidget):
|
||||
layout.addWidget(self.title_label)
|
||||
layout.addStretch(1)
|
||||
|
||||
self.min_btn = self._create_button("–", "Minimise")
|
||||
self.min_btn = self._create_button("–", "Minimize")
|
||||
self.min_btn.clicked.connect(window.showMinimized)
|
||||
layout.addWidget(self.min_btn)
|
||||
|
||||
self.max_btn = self._create_button("❐", "Maximise / Restore")
|
||||
self.max_btn = self._create_button("❐", "Maximize / Restore")
|
||||
self.max_btn.clicked.connect(window.toggle_maximise)
|
||||
layout.addWidget(self.max_btn)
|
||||
|
||||
@@ -538,6 +552,8 @@ class WeightingDialog(QtWidgets.QDialog):
|
||||
("match_keep", "dialog.weight_match_keep"),
|
||||
("brightness", "dialog.weight_brightness"),
|
||||
("grouping", "dialog.weight_grouping"),
|
||||
("continuity", "dialog.weight_continuity"),
|
||||
("border", "dialog.weight_border"),
|
||||
]
|
||||
|
||||
for i, (key, label_key) in enumerate(specs):
|
||||
@@ -594,7 +610,7 @@ class WeightingDialog(QtWidgets.QDialog):
|
||||
class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
"""Main application window containing all controls."""
|
||||
|
||||
def __init__(self, language: str, defaults: dict, reset_exclusions: bool, overlay_color: str | None = None, exclude_bg_color: str | None = None, exclude_bg_tolerance: int = 5) -> None:
|
||||
def __init__(self, language: str, defaults: dict, reset_exclusions: bool, weights: dict[str, int], overlay_color: str | None = None, exclude_bg_color: str | None = None, exclude_bg_tolerance: int = 5) -> None:
|
||||
super().__init__()
|
||||
self.init_i18n(language)
|
||||
self.setWindowTitle(self._t("app.title"))
|
||||
@@ -614,6 +630,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
|
||||
self.content = QtWidgets.QWidget()
|
||||
self.processor = QtImageProcessor()
|
||||
self.processor.weights = weights.copy()
|
||||
self.processor.set_defaults(defaults)
|
||||
self.processor.reset_exclusions_on_switch = reset_exclusions
|
||||
# Always use red for the overlay regardless of the target color
|
||||
@@ -639,6 +656,14 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
self._current_image_path: Path | None = None
|
||||
self._current_color = DEFAULT_COLOR
|
||||
self._toolbar_actions: Dict[str, Callable[[], None]] = {}
|
||||
|
||||
# Debounced log history
|
||||
self._log_history: List[Tuple[str, str]] = []
|
||||
self._pending_log_msg: str | None = None
|
||||
self._log_timer = QtCore.QTimer()
|
||||
self._log_timer.setSingleShot(True)
|
||||
self._log_timer.timeout.connect(self._commit_log)
|
||||
|
||||
self._register_default_actions()
|
||||
|
||||
self.exclude_mode = "rect"
|
||||
@@ -696,54 +721,66 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
def _build_menu_bar(self) -> QtWidgets.QMenuBar:
|
||||
self.menu_bar = QtWidgets.QMenuBar(self)
|
||||
|
||||
def add_item(menu: QtWidgets.QMenu, icon: str, text_key: str, slot, tooltip_key: str):
|
||||
action = menu.addAction(f"{icon} {self._t(text_key)}", slot)
|
||||
action.setToolTip(self._t(tooltip_key))
|
||||
return action
|
||||
|
||||
# File Menu
|
||||
file_menu = self.menu_bar.addMenu(self._t("menu.file"))
|
||||
file_menu.addAction("🖼 " + self._t("toolbar.open_image"), lambda: self._invoke_action("open_image"), "Ctrl+O")
|
||||
add_item(file_menu, "🖼", "toolbar.open_image", lambda: self._invoke_action("open_image"), "tooltip.open_image")
|
||||
file_menu.addSeparator()
|
||||
file_menu.addAction("📂 " + self._t("toolbar.open_folder"), lambda: self._invoke_action("open_folder"), "Ctrl+Shift+O")
|
||||
file_menu.addAction("📊 " + self._t("toolbar.export_folder"), lambda: self._invoke_action("export_folder"))
|
||||
add_item(file_menu, "📂", "toolbar.open_folder", lambda: self._invoke_action("open_folder"), "tooltip.open_folder")
|
||||
add_item(file_menu, "📊", "toolbar.export_folder", lambda: self._invoke_action("export_folder"), "tooltip.export_folder")
|
||||
file_menu.addSeparator()
|
||||
file_menu.addAction("📤 " + self._t("toolbar.export_settings"), lambda: self._invoke_action("export_settings"), "Ctrl+E")
|
||||
file_menu.addAction("📥 " + self._t("toolbar.import_settings"), lambda: self._invoke_action("import_settings"), "Ctrl+I")
|
||||
add_item(file_menu, "📤", "toolbar.export_settings", lambda: self._invoke_action("export_settings"), "tooltip.export_settings")
|
||||
add_item(file_menu, "📥", "toolbar.import_settings", lambda: self._invoke_action("import_settings"), "tooltip.import_settings")
|
||||
file_menu.addSeparator()
|
||||
file_menu.addAction("💾 " + self._t("toolbar.save_overlay"), lambda: self._invoke_action("save_overlay"), "Ctrl+S")
|
||||
add_item(file_menu, "💾", "toolbar.save_overlay", lambda: self._invoke_action("save_overlay"), "tooltip.save_overlay")
|
||||
file_menu.addSeparator()
|
||||
add_item(file_menu, "📁", "toolbar.open_app_folder", lambda: self._invoke_action("open_app_folder"), "tooltip.open_app_folder")
|
||||
|
||||
# Edit Menu
|
||||
edit_menu = self.menu_bar.addMenu(self._t("menu.edit"))
|
||||
edit_menu.addAction("↩ " + self._t("toolbar.undo_exclude"), lambda: self._invoke_action("undo_exclude"), "Ctrl+Z")
|
||||
edit_menu.addAction("🧹 " + self._t("toolbar.clear_excludes"), lambda: self._invoke_action("clear_excludes"))
|
||||
add_item(edit_menu, "🔄", "toolbar.reset_sliders", lambda: self._invoke_action("reset_sliders"), "tooltip.reset_sliders")
|
||||
add_item(edit_menu, "🖱", "toolbar.pick_from_image", lambda: self._invoke_action("pick_from_image"), "tooltip.pick_from_image")
|
||||
edit_menu.addSeparator()
|
||||
edit_menu.addAction("🔄 " + self._t("toolbar.reset_sliders"), lambda: self._invoke_action("reset_sliders"), "Ctrl+R")
|
||||
|
||||
# Tools Menu
|
||||
tools_menu = self.menu_bar.addMenu(self._t("menu.tools"))
|
||||
tools_menu.addAction("🎨 " + self._t("toolbar.choose_color"), lambda: self._invoke_action("choose_color"))
|
||||
tools_menu.addAction("🖱 " + self._t("toolbar.pick_from_image"), lambda: self._invoke_action("pick_from_image"))
|
||||
self.free_draw_action = QtGui.QAction("△ " + self._t("toolbar.toggle_free_draw"), self)
|
||||
self.free_draw_action.setCheckable(True)
|
||||
self.free_draw_action.setChecked(False)
|
||||
self.free_draw_action.triggered.connect(lambda: self._invoke_action("toggle_free_draw"))
|
||||
tools_menu.addAction(self.free_draw_action)
|
||||
tools_menu.addSeparator()
|
||||
tools_menu.addAction("📥 " + self._t("toolbar.pull_patterns"), lambda: self._invoke_action("pull_patterns"))
|
||||
|
||||
# View Menu
|
||||
view_menu = self.menu_bar.addMenu(self._t("menu.view"))
|
||||
view_menu.addAction("🌓 " + self._t("toolbar.toggle_theme"), lambda: self._invoke_action("toggle_theme"))
|
||||
self.prefer_dark_action = QtGui.QAction("🌑 " + self._t("toolbar.prefer_dark"), self)
|
||||
self.prefer_dark_action.setCheckable(True)
|
||||
self.prefer_dark_action.setChecked(False)
|
||||
self.prefer_dark_action.setToolTip(self._t("tooltip.prefer_dark"))
|
||||
self.prefer_dark_action.triggered.connect(lambda: self._invoke_action("toggle_prefer_dark"))
|
||||
view_menu.addAction(self.prefer_dark_action)
|
||||
edit_menu.addAction(self.prefer_dark_action)
|
||||
|
||||
# Exclusions Menu
|
||||
exclusions_menu = self.menu_bar.addMenu(self._t("menu.exclusions"))
|
||||
add_item(exclusions_menu, "🔙", "toolbar.undo_exclude", lambda: self._invoke_action("undo_exclude"), "tooltip.undo_exclude")
|
||||
add_item(exclusions_menu, "🧹", "toolbar.clear_excludes", lambda: self._invoke_action("clear_excludes"), "tooltip.clear_excludes")
|
||||
exclusions_menu.addSeparator()
|
||||
|
||||
self.free_draw_action = QtGui.QAction("🖌 " + self._t("toolbar.toggle_free_draw"), self)
|
||||
self.free_draw_action.setCheckable(True)
|
||||
self.free_draw_action.setChecked(False)
|
||||
self.free_draw_action.setToolTip(self._t("tooltip.toggle_free_draw"))
|
||||
self.free_draw_action.triggered.connect(lambda: self._invoke_action("toggle_free_draw"))
|
||||
exclusions_menu.addAction(self.free_draw_action)
|
||||
|
||||
# Tools Menu
|
||||
tools_menu = self.menu_bar.addMenu(self._t("menu.tools"))
|
||||
self.exclude_bg_action = QtGui.QAction("🖼 " + self._t("toolbar.exclude_bg", color=self.processor.exclude_bg_color_hex), self)
|
||||
self.exclude_bg_action.setCheckable(True)
|
||||
self.exclude_bg_action.setChecked(True)
|
||||
self.exclude_bg_action.setToolTip(self._t("tooltip.exclude_bg"))
|
||||
self.exclude_bg_action.triggered.connect(lambda: self._invoke_action("toggle_exclude_bg"))
|
||||
view_menu.addAction(self.exclude_bg_action)
|
||||
tools_menu.addAction(self.exclude_bg_action)
|
||||
tools_menu.addSeparator()
|
||||
add_item(tools_menu, "📥", "toolbar.pull_patterns", lambda: self._invoke_action("pull_patterns"), "tooltip.pull_patterns")
|
||||
|
||||
view_menu.addSeparator()
|
||||
view_menu.addAction("📁 " + self._t("toolbar.open_app_folder"), lambda: self._invoke_action("open_app_folder"))
|
||||
# View Menu
|
||||
view_menu = self.menu_bar.addMenu(self._t("menu.view"))
|
||||
add_item(view_menu, "🌓", "toolbar.toggle_theme", lambda: self._invoke_action("toggle_theme"), "tooltip.toggle_theme")
|
||||
add_item(view_menu, "📋", "menu.view_log", self.show_log_dialog, "tooltip.view_log")
|
||||
|
||||
# Status label logic remains but moved to palette layout or kept minimal
|
||||
# We will add it to the palette layout so that it stays on top
|
||||
@@ -757,16 +794,27 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
|
||||
current_group = QtWidgets.QHBoxLayout()
|
||||
current_group.setSpacing(8)
|
||||
|
||||
self.current_label = QtWidgets.QLabel(self._t("palette.current"))
|
||||
current_group.addWidget(self.current_label)
|
||||
|
||||
self.current_color_swatch = QtWidgets.QLabel()
|
||||
self.current_color_swatch.setFixedSize(28, 28)
|
||||
self.current_color_swatch.setStyleSheet(f"background-color: {DEFAULT_COLOR}; border-radius: 6px;")
|
||||
# Make current color swatch clickable using ColorSwatch matching style
|
||||
self.current_color_swatch = ColorSwatch("palette.current", DEFAULT_COLOR, lambda h, l: self._invoke_action("choose_color"))
|
||||
current_group.addWidget(self.current_color_swatch)
|
||||
|
||||
self.current_color_label = QtWidgets.QLabel(f"({DEFAULT_COLOR})")
|
||||
current_group.addWidget(self.current_color_label)
|
||||
|
||||
# Add overlay color group
|
||||
current_group.addSpacing(16)
|
||||
self.overlay_label = QtWidgets.QLabel(self._t("palette.overlay_color"))
|
||||
current_group.addWidget(self.overlay_label)
|
||||
|
||||
from app.logic import OVERLAY_COLOR
|
||||
overlay_default = OVERLAY_COLOR if OVERLAY_COLOR else DEFAULT_OVERLAY_HEX
|
||||
self.overlay_color_swatch = ColorSwatch("palette.overlay_color", overlay_default, lambda h, l: self._choose_overlay_color())
|
||||
current_group.addWidget(self.overlay_color_swatch)
|
||||
|
||||
layout.addLayout(current_group)
|
||||
|
||||
self.more_label = QtWidgets.QLabel(self._t("palette.more"))
|
||||
@@ -966,6 +1014,11 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
if not directory:
|
||||
return
|
||||
folder = Path(directory)
|
||||
|
||||
# Check if there is an 'images' subfolder when the selected folder isn't named 'images'
|
||||
if folder.name.lower() != "images" and (folder / "images").is_dir():
|
||||
folder = folder / "images"
|
||||
|
||||
paths = sorted(
|
||||
(p for p in folder.iterdir() if p.suffix.lower() in SUPPORTED_IMAGE_EXTENSIONS and p.is_file()),
|
||||
key=lambda p: p.name.lower(),
|
||||
@@ -1021,6 +1074,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
"prefer_dark": self.processor.prefer_dark,
|
||||
"weights": self.processor.weights,
|
||||
"current_color": self._current_color,
|
||||
"overlay_color": self.processor.overlay_color_hex,
|
||||
"exclude_ref_size": self.processor.exclude_ref_size,
|
||||
"shapes": self.image_view.shapes
|
||||
}
|
||||
@@ -1028,7 +1082,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
try:
|
||||
with open(path_str, "w", encoding="utf-8") as f:
|
||||
json.dump(settings, f, indent=4)
|
||||
self.status_label.setText(self._t("status.settings_exported", path=Path(path_str).name))
|
||||
self.set_status(self._t("status.settings_exported", path=Path(path_str).name))
|
||||
except Exception as e:
|
||||
QtWidgets.QMessageBox.warning(self, self._t("dialog.error_title"), str(e))
|
||||
|
||||
@@ -1061,12 +1115,16 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
with open(path_str, "r", encoding="utf-8") as f:
|
||||
settings = json.load(f)
|
||||
|
||||
# 1. Apply color (UI ONLY)
|
||||
# 1. Apply colors
|
||||
if "current_color" in settings:
|
||||
self._current_color = settings["current_color"]
|
||||
# Specifically NOT setting processor color to keep it RED
|
||||
self._update_color_display(self._current_color, self._t("palette.current"))
|
||||
|
||||
if "overlay_color" in settings:
|
||||
overlay_hex = settings["overlay_color"]
|
||||
self.processor.set_overlay_color(overlay_hex)
|
||||
self.overlay_color_swatch.setStyleSheet(f"background-color: {overlay_hex}; border-radius: 6px;")
|
||||
|
||||
# 2. Apply slider values
|
||||
keys = ["hue_min", "hue_max", "sat_min", "sat_max", "val_min", "val_max", "alpha"]
|
||||
for key in keys:
|
||||
@@ -1097,7 +1155,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
|
||||
self._sync_sliders_from_processor()
|
||||
self._refresh_views()
|
||||
self.status_label.setText(self._t("status.settings_imported"))
|
||||
self.set_status(self._t("status.settings_imported"))
|
||||
|
||||
except Exception as e:
|
||||
QtWidgets.QMessageBox.warning(self, self._t("dialog.error_title"), str(e))
|
||||
@@ -1147,62 +1205,78 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
delimiter = ";"
|
||||
decimal = ","
|
||||
|
||||
# Weights mapping
|
||||
w_all = self.processor.weights.get("match_all", 20)
|
||||
w_keep = self.processor.weights.get("match_keep", 30)
|
||||
w_bright = self.processor.weights.get("brightness", 10)
|
||||
w_group = self.processor.weights.get("grouping", 10)
|
||||
w_cont = self.processor.weights.get("continuity", 15)
|
||||
w_bord = self.processor.weights.get("border", 15)
|
||||
|
||||
brightness_col = self._t("stats.darkness_label") if self.processor.prefer_dark else self._t("stats.brightness_label")
|
||||
headers = [
|
||||
"Filename",
|
||||
"Color",
|
||||
"Matching Pixels",
|
||||
"Matching Pixels w/ Exclusions",
|
||||
"Excluded Pixels",
|
||||
brightness_col,
|
||||
self._t("stats.grouping_label"),
|
||||
f"Matching Pixels ({w_all}%)",
|
||||
f"Matching Pixels w/ Exclusions ({w_keep}%)",
|
||||
f"{brightness_col} ({w_bright}%)",
|
||||
f"{self._t('stats.grouping_label')} ({w_group}%)",
|
||||
f"{self._t('stats.continuity_label')} ({w_cont}%)",
|
||||
f"{self._t('stats.border_label')} ({w_bord}%)",
|
||||
"Composite Score"
|
||||
]
|
||||
|
||||
# Color and Excluded pixels removed from headers
|
||||
rows = [headers]
|
||||
|
||||
def process_image(img_path):
|
||||
try:
|
||||
img = Image.open(img_path)
|
||||
s = self.processor.get_stats_headless(img)
|
||||
|
||||
pct_all = (s.matches_all / s.total_all * 100) if s.total_all else 0.0
|
||||
pct_keep = (s.matches_keep / s.total_keep * 100) if s.total_keep else 0.0
|
||||
pct_excl = (s.total_excl / s.total_all * 100) if s.total_all else 0.0
|
||||
|
||||
pct_all_str = f"{pct_all:.2f}".replace(".", decimal)
|
||||
pct_keep_str = f"{pct_keep:.2f}".replace(".", decimal)
|
||||
pct_excl_str = f"{pct_excl:.2f}".replace(".", decimal)
|
||||
brightness_str = f"{s.effective_brightness:.2f}".replace(".", decimal)
|
||||
grouping_str = f"{s.grouping_score:.2f}".replace(".", decimal)
|
||||
composite_str = f"{s.composite_score(self.processor.weights):.2f}".replace(".", decimal)
|
||||
|
||||
img.close()
|
||||
return [
|
||||
img_path.name,
|
||||
self._current_color,
|
||||
pct_all_str,
|
||||
pct_keep_str,
|
||||
pct_excl_str,
|
||||
brightness_str,
|
||||
grouping_str,
|
||||
composite_str
|
||||
]
|
||||
except Exception:
|
||||
return [img_path.name, self._current_color, "Error", "Error", "Error", "Error", "Error", "Error"]
|
||||
params = self.processor.get_export_params()
|
||||
tasks = [(str(p), params) for p in self.processor.preview_paths]
|
||||
|
||||
results = [None] * total
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
future_to_idx = {executor.submit(process_image, p): i for i, p in enumerate(self.processor.preview_paths)}
|
||||
|
||||
from app.qt.image_processor import _export_worker
|
||||
with concurrent.futures.ProcessPoolExecutor() as executor:
|
||||
future_to_idx = {executor.submit(_export_worker, t): i for i, t in enumerate(tasks)}
|
||||
done_count = 0
|
||||
for future in concurrent.futures.as_completed(future_to_idx):
|
||||
idx = future_to_idx[future]
|
||||
results[idx] = future.result()
|
||||
res = future.result()
|
||||
name, pct_all, pct_keep, eff_brightness, grouping, continuity, border, composite_score = res
|
||||
|
||||
if pct_keep is None:
|
||||
# Error parsing image
|
||||
results[idx] = [name, "Error", "Error", "Error", "Error", "Error", "Error", -1.0]
|
||||
else:
|
||||
results[idx] = [
|
||||
name,
|
||||
pct_all,
|
||||
pct_keep,
|
||||
eff_brightness,
|
||||
grouping,
|
||||
continuity,
|
||||
border,
|
||||
composite_score
|
||||
]
|
||||
|
||||
done_count += 1
|
||||
if done_count % 10 == 0 or done_count == total:
|
||||
self.status_label.setText(self._t("status.exporting", current=str(done_count), total=str(total)))
|
||||
self.set_status(self._t("status.exporting", current=str(done_count), total=str(total)))
|
||||
QtWidgets.QApplication.processEvents()
|
||||
|
||||
rows.extend(results)
|
||||
# Sort results by composite_score (last element) descending
|
||||
results.sort(key=lambda x: x[-1] if isinstance(x[-1], (int, float)) else -1.0, reverse=True)
|
||||
|
||||
# Convert numbers to strings with custom decimal separator for CSV
|
||||
final_rows = []
|
||||
for r in results:
|
||||
str_row = []
|
||||
for item in r:
|
||||
if isinstance(item, (int, float)):
|
||||
str_row.append(f"{item:.2f}".replace(".", decimal))
|
||||
else:
|
||||
str_row.append(str(item))
|
||||
final_rows.append(str_row)
|
||||
|
||||
rows.extend(final_rows)
|
||||
|
||||
# Compute max width per column for alignment, plus extra space so it's not cramped
|
||||
col_widths = [max(len(str(item)) for item in col) + 4 for col in zip(*rows)]
|
||||
@@ -1217,7 +1291,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
|
||||
# Restore overlay state for currently viewed image
|
||||
self.processor._rebuild_overlay()
|
||||
self.status_label.setText(self._t("status.export_done", path=csv_path))
|
||||
self.set_status(self._t("status.export_done", path=Path(csv_path).name))
|
||||
|
||||
def show_previous_image(self) -> None:
|
||||
if not self.processor.preview_paths:
|
||||
@@ -1245,6 +1319,18 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
self.processor.set_exclusions([])
|
||||
self._refresh_views()
|
||||
|
||||
def _try_show_previous_image(self) -> None:
|
||||
focused = QtWidgets.QApplication.focusWidget()
|
||||
if isinstance(focused, (QtWidgets.QSlider, QtWidgets.QLineEdit)):
|
||||
return
|
||||
self.show_previous_image()
|
||||
|
||||
def _try_show_next_image(self) -> None:
|
||||
focused = QtWidgets.QApplication.focusWidget()
|
||||
if isinstance(focused, (QtWidgets.QSlider, QtWidgets.QLineEdit)):
|
||||
return
|
||||
self.show_next_image()
|
||||
|
||||
# Helpers ----------------------------------------------------------------
|
||||
|
||||
def _update_color_display(self, hex_code: str, label: str, update_range: bool = False) -> None:
|
||||
@@ -1252,7 +1338,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
self.current_color_swatch.setStyleSheet(f"background-color: {hex_code}; border-radius: 6px;")
|
||||
self.current_color_label.setText(f"({hex_code})")
|
||||
if label:
|
||||
self.status_label.setText(f"{label}: {hex_code}")
|
||||
self.set_status(f"{label}: {hex_code}")
|
||||
|
||||
if update_range:
|
||||
# Convert hex to HSV and update sliders/thresholds
|
||||
@@ -1286,7 +1372,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
def _on_slider_change(self, key: str, value: int) -> None:
|
||||
self.processor.set_threshold(key, value)
|
||||
label = self._slider_title(key)
|
||||
self.status_label.setText(f"{label}: {value}")
|
||||
self.set_status(f"{label}: {value}")
|
||||
self._slider_timer.start()
|
||||
|
||||
def _reset_sliders(self) -> None:
|
||||
@@ -1296,7 +1382,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
default_value = int(self.processor.defaults.get(attr, getattr(self.processor, attr)))
|
||||
control.set_value(default_value)
|
||||
self.processor.set_threshold(attr, default_value)
|
||||
self.status_label.setText(self._t("status.defaults_restored"))
|
||||
self.set_status(self._t("status.defaults_restored"))
|
||||
self._refresh_overlay_only()
|
||||
|
||||
# Shortcuts --------------------------------------------------------------
|
||||
@@ -1308,8 +1394,8 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
(QtGui.QKeySequence.Save, self.save_overlay),
|
||||
(QtGui.QKeySequence.Undo, self.undo_exclusion),
|
||||
(QtGui.QKeySequence("Ctrl+R"), self._reset_sliders),
|
||||
(QtGui.QKeySequence(QtCore.Qt.Key_Left), self.show_previous_image),
|
||||
(QtGui.QKeySequence(QtCore.Qt.Key_Right), self.show_next_image),
|
||||
(QtGui.QKeySequence(QtCore.Qt.Key_Left), self._try_show_previous_image),
|
||||
(QtGui.QKeySequence(QtCore.Qt.Key_Right), self._try_show_next_image),
|
||||
(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self._exit_pick_mode),
|
||||
]
|
||||
for seq, slot in shortcuts:
|
||||
@@ -1327,13 +1413,13 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
self._pick_mode = True
|
||||
self.image_view.pick_mode = True
|
||||
self.image_view.setCursor(QtCore.Qt.CrossCursor)
|
||||
self.status_label.setText(self._t("status.pick_mode_ready"))
|
||||
self.set_status(self._t("status.pick_mode_ready"))
|
||||
|
||||
def _exit_pick_mode(self) -> None:
|
||||
self._pick_mode = False
|
||||
self.image_view.pick_mode = False
|
||||
self.image_view.setCursor(QtCore.Qt.ArrowCursor)
|
||||
self.status_label.setText(self._t("status.pick_mode_ended"))
|
||||
self.set_status(self._t("status.pick_mode_ended"))
|
||||
|
||||
def _on_pixel_picked(self, x: int, y: int) -> None:
|
||||
result = self.processor.pick_color(x, y)
|
||||
@@ -1362,19 +1448,68 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
self.processor.set_threshold(attr, value)
|
||||
|
||||
# Update color swatch to the picked pixel color
|
||||
h_norm = hue / 360.0
|
||||
s_norm = sat / 100.0
|
||||
v_norm = val / 100.0
|
||||
import colorsys
|
||||
r, g, b = colorsys.hsv_to_rgb(h_norm, s_norm, v_norm)
|
||||
hex_code = "#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
|
||||
self._update_color_display(hex_code, "")
|
||||
|
||||
self.status_label.setText(
|
||||
self._t("status.pick_mode_from_image", hue=hue, saturation=sat, value=val)
|
||||
)
|
||||
if not self._current_image_path:
|
||||
return
|
||||
try:
|
||||
from PIL import Image
|
||||
img = Image.open(self._current_image_path).convert("RGB")
|
||||
r, g, b = img.getpixel((x, y)) # type: ignore[misc]
|
||||
img.close()
|
||||
hex_code = f"#{r:02x}{g:02x}{b:02x}"
|
||||
hue, sat, val = self.processor.rgb_to_hsv(r, g, b)
|
||||
msg = self._t("status.pick_mode_from_image", hue=hue, saturation=sat, value=val)
|
||||
self._update_color_display(hex_code, "Picked Color")
|
||||
self.set_status(msg)
|
||||
# Auto-exit pick mode after selection
|
||||
self._invoke_action("pick_from_image")
|
||||
except Exception as e:
|
||||
self.set_status(self._t("dialog.image_open_failed", error=str(e)))
|
||||
self._refresh_overlay_only()
|
||||
|
||||
def set_status(self, msg: str) -> None:
|
||||
self.status_label.setText(msg)
|
||||
self._pending_log_msg = msg
|
||||
self._log_timer.start(250)
|
||||
|
||||
def _commit_log(self) -> None:
|
||||
if not self._pending_log_msg:
|
||||
return
|
||||
import datetime
|
||||
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
|
||||
self._log_history.append((timestamp, self._pending_log_msg))
|
||||
if len(self._log_history) > 10:
|
||||
self._log_history.pop(0)
|
||||
self._pending_log_msg = None
|
||||
|
||||
def show_log_dialog(self) -> None:
|
||||
dialog = QtWidgets.QDialog(self)
|
||||
dialog.setWindowTitle(self._t("menu.view_log"))
|
||||
|
||||
screen = self.screen()
|
||||
if screen:
|
||||
rect = screen.availableGeometry()
|
||||
dialog.resize(max(400, int(rect.width() * 0.5)), max(300, int(rect.height() * 0.5)))
|
||||
else:
|
||||
dialog.setMinimumSize(400, 300)
|
||||
|
||||
layout = QtWidgets.QVBoxLayout(dialog)
|
||||
|
||||
text_edit = QtWidgets.QTextEdit()
|
||||
text_edit.setReadOnly(True)
|
||||
|
||||
log_text = ""
|
||||
for ts, msg in self._log_history:
|
||||
log_text += f"[{ts}] {msg}\n"
|
||||
|
||||
text_edit.setPlainText(log_text if log_text else "No logs yet.")
|
||||
layout.addWidget(text_edit)
|
||||
|
||||
btn_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok)
|
||||
btn_box.accepted.connect(dialog.accept)
|
||||
layout.addWidget(btn_box)
|
||||
|
||||
dialog.exec()
|
||||
|
||||
def open_pattern_puller(self) -> None:
|
||||
dialog = PatternPullerDialog(self.language, parent=self)
|
||||
dialog.exec()
|
||||
@@ -1432,15 +1567,41 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
hex_code = color.name()
|
||||
self._update_color_display(hex_code, self._t("dialog.choose_color_title"), update_range=True)
|
||||
|
||||
def _choose_overlay_color(self) -> None:
|
||||
color = QtWidgets.QColorDialog.getColor(parent=self, title=self._t("dialog.choose_color_title"))
|
||||
if not color.isValid():
|
||||
return
|
||||
hex_code = color.name()
|
||||
self.processor.set_overlay_color(hex_code)
|
||||
self.overlay_color_swatch.setStyleSheet(
|
||||
f"QPushButton {{ background-color: {hex_code}; border: 2px solid {THEMES[self.current_theme]['border']}; border-radius: 6px; }}"
|
||||
f"QPushButton:hover {{ border-color: {THEMES[self.current_theme]['accent']}; }}"
|
||||
)
|
||||
self.overlay_color_swatch.hex_code = hex_code
|
||||
self._refresh_overlay_only()
|
||||
|
||||
def save_overlay(self) -> None:
|
||||
pixmap = self.processor.overlay_pixmap()
|
||||
if pixmap.isNull():
|
||||
QtWidgets.QMessageBox.information(self, self._t("dialog.info_title"), self._t("dialog.no_preview_available"))
|
||||
return
|
||||
|
||||
skin_name = "image"
|
||||
if self._current_image_path:
|
||||
# Similar logic to export_settings to get the skin name
|
||||
if self._current_image_path.parent.name == "images":
|
||||
skin_name = self._current_image_path.parent.parent.name
|
||||
elif self._current_image_path.parent.name and self._current_image_path.parent.name != "images":
|
||||
skin_name = self._current_image_path.parent.name
|
||||
else:
|
||||
skin_name = self._current_image_path.stem
|
||||
|
||||
default_name = f"{skin_name}.png"
|
||||
|
||||
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
|
||||
self,
|
||||
self._t("dialog.save_overlay_title"),
|
||||
"overlay.png",
|
||||
default_name,
|
||||
"PNG (*.png)",
|
||||
)
|
||||
if not filename:
|
||||
@@ -1448,14 +1609,14 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
if not pixmap.save(filename, "PNG"):
|
||||
QtWidgets.QMessageBox.warning(self, self._t("dialog.error_title"), self._t("dialog.image_open_failed", error="Unable to save file"))
|
||||
return
|
||||
self.status_label.setText(self._t("dialog.overlay_saved", path=filename))
|
||||
self.set_status(self._t("dialog.overlay_saved", path=filename))
|
||||
|
||||
def toggle_free_draw(self) -> None:
|
||||
self.exclude_mode = "free" if self.exclude_mode == "rect" else "rect"
|
||||
self.image_view.set_mode(self.exclude_mode)
|
||||
self.free_draw_action.setChecked(self.exclude_mode == "free")
|
||||
message_key = "status.free_draw_enabled" if self.exclude_mode == "free" else "status.free_draw_disabled"
|
||||
self.status_label.setText(self._t(message_key))
|
||||
self.set_status(self._t(message_key))
|
||||
|
||||
def toggle_prefer_dark(self) -> None:
|
||||
self.processor.prefer_dark = not self.processor.prefer_dark
|
||||
@@ -1476,12 +1637,12 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
def clear_exclusions(self) -> None:
|
||||
self.image_view.clear_shapes()
|
||||
self.processor.set_exclusions([])
|
||||
self.status_label.setText(self._t("toolbar.clear_excludes"))
|
||||
self.set_status(self._t("toolbar.clear_excludes"))
|
||||
self._refresh_overlay_only()
|
||||
|
||||
def undo_exclusion(self) -> None:
|
||||
self.image_view.undo_last()
|
||||
self.status_label.setText(self._t("toolbar.undo_exclude"))
|
||||
self.set_status(self._t("toolbar.undo_exclude"))
|
||||
|
||||
def toggle_theme(self) -> None:
|
||||
self.current_theme = "light" if self.current_theme == "dark" else "dark"
|
||||
@@ -1504,6 +1665,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
|
||||
self.status_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;")
|
||||
self.current_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;")
|
||||
self.overlay_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;")
|
||||
self.current_color_label.setStyleSheet(f"color: {colors['text_dim']};")
|
||||
self.more_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;")
|
||||
self.filename_prefix_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;")
|
||||
@@ -1611,7 +1773,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin):
|
||||
dimensions = f"{width}×{height}"
|
||||
|
||||
# Status label for top right layout
|
||||
self.status_label.setText(
|
||||
self.set_status(
|
||||
self._t("status.loaded", name=self._current_image_path.name, dimensions=dimensions, position=position)
|
||||
)
|
||||
|
||||
|
||||
+26
-11
@@ -3,23 +3,38 @@
|
||||
language = "en"
|
||||
|
||||
[options]
|
||||
# Set to true to clear exclusion shapes whenever the image changes.
|
||||
# Set to true to clear exclusion shapes whenever the image changes in the preview viewer.
|
||||
reset_exclusions_on_image_change = false
|
||||
|
||||
# Hex color code for the match overlay (e.g. "#ff0000" for Red, "#00ff00" for Green)
|
||||
# This is the color that paints over pixels that successfully match your HSV ranges.
|
||||
overlay_color = "#ff0000"
|
||||
|
||||
# Hex color code for the background to be excluded (default #1f2937)
|
||||
# This is useful for automatically removing flat background colors from UI screenshots
|
||||
# or web scrapings before the analysis runs.
|
||||
exclude_bg_color = "#1f2937"
|
||||
|
||||
# Tolerance for background color matching (0-255, default 5)
|
||||
# A higher value will exclude pixels that are "close" to the hex color above, allowing
|
||||
# you to bypass slight compression artifacts or noise in the image background.
|
||||
exclude_bg_tolerance = 5
|
||||
|
||||
[defaults]
|
||||
# Override any of the following keys to tweak the initial slider values:
|
||||
# hue_min, hue_max, sat_min, val_min, val_max accept floating point numbers.
|
||||
# alpha accepts an integer between 0 and 255.
|
||||
hue_min = 250.0
|
||||
hue_max = 310.0
|
||||
sat_min = 15.0
|
||||
sat_max = 100.0
|
||||
val_min = 15.0
|
||||
val_max = 100.0
|
||||
alpha = 150
|
||||
# Override any of the following to tweak the initial slider values upon application start.
|
||||
hue_min = 250.0 # (0-360) Starting Hue for the target color range
|
||||
hue_max = 310.0 # (0-360) Ending Hue for the target color range
|
||||
sat_min = 15.0 # (0-100) Minimum Saturation percentage
|
||||
sat_max = 100.0 # (0-100) Maximum Saturation percentage
|
||||
val_min = 15.0 # (0-100) Minimum Value/Brightness percentage
|
||||
val_max = 100.0 # (0-100) Maximum Value/Brightness percentage
|
||||
alpha = 150 # (0-255) Opacity of the red overlay in the UI preview
|
||||
|
||||
[weights]
|
||||
# Contribution of each measurement to the final Composite Score (0-100%).
|
||||
match_all = 20 # % of the total visible image that matches
|
||||
match_keep = 30 # % of the non-excluded area that matches (the most important area)
|
||||
brightness = 10 # % Importance of Vibrance (or Darkness if "Prefer Darkness" is on)
|
||||
grouping = 10 # % Importance of pixel clustering (rewarding solid color blocks)
|
||||
continuity = 15 # % Quality of the largest connected surface area
|
||||
border = 15 # % Quality of the transition edges (penalizing dark/hard outlines)
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
# Base directory for language files
|
||||
LANG_DIR = Path(__file__).resolve().parent.parent / "app" / "lang"
|
||||
|
||||
def get_structure(file_path: Path):
|
||||
"""
|
||||
Returns a list of (line_number, key/header) for a TOML file.
|
||||
Only captures keys and section headers, ignoring the values.
|
||||
"""
|
||||
structure = []
|
||||
# Regex to capture "key" = or [header]
|
||||
key_pattern = re.compile(r'^\s*"?([^"\s=]+)"?\s*=')
|
||||
header_pattern = re.compile(r'^\s*\[([^\]]+)\]')
|
||||
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
for i, line in enumerate(f, 1):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
structure.append((i, "<empty>"))
|
||||
continue
|
||||
|
||||
# Check for header [section]
|
||||
header_match = header_pattern.match(line)
|
||||
if header_match:
|
||||
structure.append((i, f"[{header_match.group(1)}]"))
|
||||
continue
|
||||
|
||||
# Check for key "name" =
|
||||
key_match = key_pattern.match(line)
|
||||
if key_match:
|
||||
structure.append((i, key_match.group(1)))
|
||||
continue
|
||||
|
||||
# Comments or anything else
|
||||
structure.append((i, "<other/comment>"))
|
||||
|
||||
return structure
|
||||
|
||||
def test_i18n_files_exist():
|
||||
assert LANG_DIR.exists(), f"Language directory {LANG_DIR} not found"
|
||||
en_file = LANG_DIR / "en.toml"
|
||||
assert en_file.exists(), "English language file (en.toml) must exist as baseline"
|
||||
|
||||
def test_i18n_synchronization():
|
||||
"""
|
||||
Ensures all language files have the same keys/headers on the same lines
|
||||
as the baseline en.toml.
|
||||
"""
|
||||
en_path = LANG_DIR / "en.toml"
|
||||
en_structure = get_structure(en_path)
|
||||
|
||||
other_files = list(LANG_DIR.glob("*.toml"))
|
||||
other_files.remove(en_path)
|
||||
|
||||
for lang_file in other_files:
|
||||
lang_name = lang_file.name
|
||||
lang_structure = get_structure(lang_file)
|
||||
|
||||
# Check line count
|
||||
assert len(lang_structure) == len(en_structure), \
|
||||
f"{lang_name} length mismatch: expected {len(en_structure)} lines, got {len(lang_structure)}"
|
||||
|
||||
# Check line-by-line sync
|
||||
for (en_line, en_key), (lang_line, lang_key) in zip(en_structure, lang_structure):
|
||||
assert en_key == lang_key, \
|
||||
f"Sync error at {lang_name}:{lang_line}. Expected '{en_key}', found '{lang_key}'"
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Allow running directly as a script
|
||||
pytest.main([__file__])
|
||||
@@ -155,3 +155,20 @@ def test_calculate_grouping_score():
|
||||
# 1 center pixel = 80/80 = 1.0.
|
||||
# Overall it should be a healthy percentage.
|
||||
assert res_9x9 > 10.0 # significant grouping
|
||||
|
||||
def test_export_worker_error():
|
||||
from app.qt.image_processor import _export_worker
|
||||
|
||||
# 1. Provide a missing file to trigger an exception during Image.open()
|
||||
res1 = _export_worker(("missing_file.png", {
|
||||
"hue_min": 0, "hue_max": 360, "sat_min": 0, "sat_max": 100,
|
||||
"val_min": 0, "val_max": 100, "exclude_bg": False,
|
||||
"exclude_bg_rgb": (0, 0, 0), "exclude_bg_tolerance": 5,
|
||||
"prefer_dark": False, "exclude_shapes": [], "exclude_ref_size": None,
|
||||
"weights": {}
|
||||
}))
|
||||
assert res1 == ("missing_file.png", None, None, None, None, None)
|
||||
|
||||
# 2. Provide an empty params dict to trigger KeyError before opening image
|
||||
res2 = _export_worker(("dummy.png", {}))
|
||||
assert res2 == ("dummy.png", None, None, None, None, None)
|
||||
|
||||
Reference in New Issue
Block a user