Skip to content

Commit e626e67

Browse files
committed
refactor: simplify contrast sampling
1 parent acec5b4 commit e626e67

2 files changed

Lines changed: 15 additions & 33 deletions

File tree

quickthumb/_diagnostic_rules.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ def worst_tile_contrast(
278278
tile_size: int,
279279
) -> TiledContrastMeasurement | None:
280280
"""Measure the lowest text-pixel contrast across tiled samples."""
281+
if tile_size <= 0:
282+
raise ValueError("tile_size must be positive")
283+
281284
foreground_alpha = foreground_image.getchannel("A").point(lambda value: 255 if value else 0)
282-
tile_count = _tile_count(region, tile_size)
285+
tile_count = ((region.width + tile_size - 1) // tile_size) * (
286+
(region.height + tile_size - 1) // tile_size
287+
)
283288

284289
worst: TiledContrastMeasurement | None = None
285290
for tile in _tiled_regions(region, tile_size):
@@ -288,7 +293,9 @@ def worst_tile_contrast(
288293
continue
289294

290295
background = average_visible_background(background_image, tile, mask=foreground_alpha)
291-
foreground = _average_visible_foreground(foreground_image, tile, foreground_alpha)
296+
foreground_crop = foreground_image.crop((tile.x, tile.y, tile.right, tile.bottom))
297+
foreground_mask = foreground_alpha.crop((tile.x, tile.y, tile.right, tile.bottom))
298+
foreground = tuple(ImageStat.Stat(foreground_crop, mask=foreground_mask).mean[:3])
292299
contrast = contrast_ratio(foreground, background)
293300
if worst is None or contrast < worst.contrast:
294301
worst = TiledContrastMeasurement(
@@ -301,27 +308,7 @@ def worst_tile_contrast(
301308
return worst
302309

303310

304-
def _average_visible_foreground(
305-
image: Image.Image, region: BBox, mask: Image.Image
306-
) -> tuple[float, float, float]:
307-
crop = image.crop((region.x, region.y, region.right, region.bottom))
308-
mask_crop = mask.crop((region.x, region.y, region.right, region.bottom))
309-
mean_r, mean_g, mean_b, *_ = ImageStat.Stat(crop, mask=mask_crop).mean
310-
return mean_r, mean_g, mean_b
311-
312-
313-
def _tile_count(region: BBox, tile_size: int) -> int:
314-
if tile_size <= 0:
315-
raise ValueError("tile_size must be positive")
316-
cols = (region.width + tile_size - 1) // tile_size
317-
rows = (region.height + tile_size - 1) // tile_size
318-
return cols * rows
319-
320-
321311
def _tiled_regions(region: BBox, tile_size: int) -> Iterable[BBox]:
322-
if tile_size <= 0:
323-
raise ValueError("tile_size must be positive")
324-
325312
for y in range(region.y, region.bottom, tile_size):
326313
for x in range(region.x, region.right, tile_size):
327314
yield BBox.from_points(

quickthumb/_diagnostics.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,15 @@ def _text_background_contrast(
426426
if clamped is None:
427427
return None
428428

429-
foreground = self._render_text_foreground(layer)
430-
return worst_tile_contrast(
431-
running,
432-
foreground,
433-
clamped,
434-
tile_size=CONTRAST_TILE_SIZE,
435-
)
436-
437-
def _render_text_foreground(self, layer: TextLayer) -> Image.Image:
438429
content = layer.content
439430
if isinstance(content, list):
440431
content = [part.model_copy(update={"effects": []}) for part in content]
441432
foreground_layer = layer.model_copy(update={"content": content, "effects": []})
442-
443433
foreground = Image.new("RGBA", (self._ctx.width, self._ctx.height), (0, 0, 0, 0))
444434
self._text.render_text_layer(foreground, foreground_layer)
445-
return foreground
435+
return worst_tile_contrast(
436+
running,
437+
foreground,
438+
clamped,
439+
tile_size=CONTRAST_TILE_SIZE,
440+
)

0 commit comments

Comments
 (0)