HaanIlango commited on
Commit
e82e34e
·
verified ·
1 Parent(s): 09ceb58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -294,12 +294,17 @@ def contrast_ratio(rgb1, rgb2):
294
  return (lighter + 0.05) / (darker + 0.05)
295
 
296
 
297
- def contrast_severity_label(ratio):
298
- if ratio < 1.5:
 
 
 
 
 
299
  return "could be much clearer"
300
- elif ratio < 2.5:
301
  return "could be clearer"
302
- elif ratio < 3.5:
303
  return "slightly less clear than ideal"
304
  else:
305
  return "close to the recommended level"
@@ -309,18 +314,17 @@ def sample_text_and_bg_color(img_arr, x, y, w, h):
309
  pad = max(2, int(h * 0.3))
310
  y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)
311
  x0, x1 = max(0, x - pad), min(img_arr.shape[1], x + w + pad)
312
- region = img_arr[y0:y1, x0:x1]
313
- gray = cv2.cvtColor(region, cv2.COLOR_RGB2GRAY)
314
- thresh = np.median(gray)
315
- dark_mask = gray < thresh
316
- light_mask = ~dark_mask
317
- dark_px = region[dark_mask]
318
- light_px = region[light_mask]
319
- if len(dark_px) == 0 or len(light_px) == 0:
320
  return None, None
321
- text_color = tuple(np.median(dark_px, axis=0).astype(int))
322
- bg_color = tuple(np.median(light_px, axis=0).astype(int))
323
- return text_color, bg_color
 
 
 
 
 
324
 
325
 
326
  def analyze_readability(pil_img):
@@ -328,7 +332,17 @@ def analyze_readability(pil_img):
328
  img_h = img_arr.shape[0]
329
 
330
  gray = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)
331
- bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 10)
 
 
 
 
 
 
 
 
 
 
332
  ocr_data = pytesseract.image_to_data(bw, output_type=pytesseract.Output.DICT)
333
 
334
  words_checked = 0
@@ -350,10 +364,11 @@ def analyze_readability(pil_img):
350
  continue
351
  ratio = contrast_ratio(text_color, bg_color)
352
  size_pct = (h / img_h) * 100
353
- if ratio < 4.5:
 
354
  contrast_issues += 1
355
  if len(issue_lines) < 5:
356
- severity = contrast_severity_label(ratio)
357
  issue_lines.append(f"- \"{word}\" {severity} against its background")
358
  issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})
359
  if size_pct < 1.2:
@@ -614,7 +629,7 @@ def analyze(pil_image, overlay_strength):
614
 
615
  summary += (
616
  "\n\n---\n"
617
- "**This tool flags what's off, not what you have to do.** I'm Haan - "
618
  "a brand identity and visual designer with 12+ years turning insights like "
619
  "these into designs that actually convert, stay true to your brand, and "
620
  "look intentional (not just \"fixed\").\n\n"
 
294
  return (lighter + 0.05) / (darker + 0.05)
295
 
296
 
297
+ def required_contrast_ratio(size_pct):
298
+ return 3.0 if size_pct >= 3.0 else 4.5
299
+
300
+
301
+ def contrast_severity_label(ratio, required_ratio):
302
+ deficit = (required_ratio - ratio) / required_ratio
303
+ if deficit > 0.6:
304
  return "could be much clearer"
305
+ elif deficit > 0.35:
306
  return "could be clearer"
307
+ elif deficit > 0.15:
308
  return "slightly less clear than ideal"
309
  else:
310
  return "close to the recommended level"
 
314
  pad = max(2, int(h * 0.3))
315
  y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)
316
  x0, x1 = max(0, x - pad), min(img_arr.shape[1], x + w + pad)
317
+ region = img_arr[y0:y1, x0:x1].reshape(-1, 3).astype(np.float32)
318
+ if len(region) < 10:
 
 
 
 
 
 
319
  return None, None
320
+ criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
321
+ try:
322
+ _, _, centers = cv2.kmeans(region, 2, None, criteria, 3, cv2.KMEANS_PP_CENTERS)
323
+ except cv2.error:
324
+ return None, None
325
+ color1 = tuple(centers[0].astype(int))
326
+ color2 = tuple(centers[1].astype(int))
327
+ return color1, color2
328
 
329
 
330
  def analyze_readability(pil_img):
 
332
  img_h = img_arr.shape[0]
333
 
334
  gray = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)
335
+
336
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
337
+ gray_enhanced = clahe.apply(gray)
338
+
339
+ gray_denoised = cv2.medianBlur(gray_enhanced, 3)
340
+
341
+ bw = cv2.adaptiveThreshold(gray_denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 10)
342
+
343
+ cleanup_kernel = np.ones((2, 2), np.uint8)
344
+ bw = cv2.morphologyEx(bw, cv2.MORPH_OPEN, cleanup_kernel)
345
+
346
  ocr_data = pytesseract.image_to_data(bw, output_type=pytesseract.Output.DICT)
347
 
348
  words_checked = 0
 
364
  continue
365
  ratio = contrast_ratio(text_color, bg_color)
366
  size_pct = (h / img_h) * 100
367
+ required_ratio = required_contrast_ratio(size_pct)
368
+ if ratio < required_ratio:
369
  contrast_issues += 1
370
  if len(issue_lines) < 5:
371
+ severity = contrast_severity_label(ratio, required_ratio)
372
  issue_lines.append(f"- \"{word}\" {severity} against its background")
373
  issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})
374
  if size_pct < 1.2:
 
629
 
630
  summary += (
631
  "\n\n---\n"
632
+ "**This tool flags what's off. A designer knows what's right.** I'm Haan - "
633
  "a brand identity and visual designer with 12+ years turning insights like "
634
  "these into designs that actually convert, stay true to your brand, and "
635
  "look intentional (not just \"fixed\").\n\n"