fitzee commented on code in PR #44034:
URL: https://github.com/apache/superset/pull/44034#discussion_r3963883231
##########
superset/utils/screenshot_utils.py:
##########
@@ -130,26 +136,77 @@ class ScreenshotCaptureTimeoutError(RuntimeError):
"""Raised when Chromium repeatedly times out while capturing a tile."""
-def is_screenshot_nearly_uniform(screenshot: bytes) -> tuple[bool, float]:
- """Return whether one color occupies nearly all sampled screenshot
pixels."""
+class ScreenshotBlankCaptureError(RuntimeError):
+ """Raised when Chromium repeatedly returns a perceptually blank capture."""
+
+
+@dataclass(frozen=True)
+class ScreenshotBlanknessMetrics:
+ """Metrics used to decide whether a screenshot is perceptually blank."""
+
+ is_blank: bool
+ dominant_pixel_ratio: float
+ near_white_pixel_ratio: float
+ mean_luminance: float
+ luminance_stddev: float
+ entropy: float
+
+
+def get_screenshot_blankness_metrics(screenshot: bytes) ->
ScreenshotBlanknessMetrics:
+ """Measure exact-color and perceptual blankness on a sampled screenshot."""
try:
with Image.open(io.BytesIO(screenshot)) as image:
sample = image.convert("RGB")
sample.thumbnail((256, 256))
- colors = sample.getcolors(maxcolors=256)
- if not colors:
- return False, 0.0
- dominant_pixels = max(count for count, _color in colors)
- dominant_ratio = dominant_pixels / (sample.width * sample.height)
- return (
- dominant_ratio >= TILED_SCREENSHOT_BLANK_DOMINANT_PIXEL_RATIO,
- dominant_ratio,
+ pixel_count = sample.width * sample.height
+ colors = sample.getcolors(maxcolors=pixel_count) or []
+ dominant_pixels = max(
+ (count for count, _color in colors),
+ default=0,
+ )
+ dominant_ratio = dominant_pixels / pixel_count
+
+ grayscale = sample.convert("L")
+ histogram = grayscale.histogram()
+ near_white_ratio = (
+ sum(histogram[SCREENSHOT_BLANK_MIN_LUMINANCE:]) / pixel_count
+ )
+ statistics = ImageStat.Stat(grayscale)
+ mean_luminance = float(statistics.mean[0])
+ luminance_stddev = float(statistics.stddev[0])
+ entropy = -sum(
+ (count / pixel_count) * math.log2(count / pixel_count)
+ for count in histogram
+ if count
+ )
+ exact_uniform = (
+ dominant_ratio >= TILED_SCREENSHOT_BLANK_DOMINANT_PIXEL_RATIO
+ )
+ perceptually_blank = (
+ near_white_ratio >= SCREENSHOT_BLANK_NEAR_WHITE_PIXEL_RATIO
+ and luminance_stddev <= SCREENSHOT_BLANK_MAX_LUMINANCE_STDDEV
+ and entropy <= SCREENSHOT_BLANK_MAX_ENTROPY
+ )
Review Comment:
Addressed in 6a9011bbbf. Blankness is no longer decided by near-white
coverage alone: the revised detector requires low luminance variance and low
entropy at both 256px and 1024px sampling scales, plus either high mean
luminance or exact uniformity. Capture rejection is also DOM-state-aware, so
terminal empty/error states are accepted. Added realistic readable empty-state,
sparse-content, tall-report, and RGB 239 background regression cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]