Skip to content

Add scale_detect: infer display scale / visual DPI from a template#407

Merged
JE-Chen merged 1 commit into
devfrom
feat/scale-detect-batch
Jun 24, 2026
Merged

Add scale_detect: infer display scale / visual DPI from a template#407
JE-Chen merged 1 commit into
devfrom
feat/scale-detect-batch

Conversation

@JE-Chen

@JE-Chen JE-Chen commented Jun 24, 2026

Copy link
Copy Markdown
Member

Why

A template cropped at 100% display scale won't match pixel-for-pixel on a machine running at 150% DPI — everything is 1.5x bigger. visual_match.match_template can sweep scales, but it returns only the single best match's location and throws the per-scale scores away. scale_detect keeps the whole profile:

  • scale_sweep — the per-scale score profile (every scale's best match)
  • detect_scale — the winning scale as a DPI inference (scale_percent) with a confidence margin (how far it beats the runner-up)

So an automation can infer the effective UI scale / DPI and how confident that inference is — the per-scale scores match_template discards.

Design

  • Reuses visual_match._score_map (the full matchTemplate surface, oriented higher = better) for each scale; source is any ndarray / path / PIL image, or the live screen. scales defaults to the common Windows values (1.0, 1.25, 1.5, 1.75, 2.0). Scales at which the template exceeds the haystack are skipped; detect_scale returns None when none fit. cv2/numpy lazily imported.
  • 5 layers wired: core → facade __all__AC_detect_scale / AC_scale_sweep → read-only ac_* MCP tools → Script Builder (Image). Qt-free verified.

Tests

test/unit_test/headless/test_scale_detect_batch.py (cv2 via importorskip) — embeds a template resized by a known factor into a canvas and asserts detect_scale recovers that scale (1.5x→150%, 1.0x→100%, 2.0x via the executor path), the full sweep profile + keys, the margin confidence, and the too-big None/[] cases. Uses pytest.approx (no float ==). 15 passed with the image_quality sibling.

A template cropped at 100% scale won't match on a 150%-DPI machine, and
match_template returns only the single best match, discarding the
per-scale scores. scale_sweep keeps the whole profile (every scale's
best match) and detect_scale reports the winning scale as a DPI
inference with a confidence margin (how far it beats the runner-up).
Reuses visual_match._score_map per scale; cv2/numpy lazily imported.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 25 complexity · 0 duplication

Metric Results
Complexity 25
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@JE-Chen JE-Chen merged commit 538a6b4 into dev Jun 24, 2026
16 checks passed
@JE-Chen JE-Chen deleted the feat/scale-detect-batch branch June 24, 2026 06:41
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant