Spaces:
Sleeping
Sleeping
Update app.py
Browse files293 unmodified lines2942952962972982993003013023033043053 unmodified lines3093103113123133143153163173183193203213223233243253261 unmodified line32832933033133233333415 unmodified lines350351352353354355356357358359254 unmodified lines614615616617618619620293 unmodified linesreturn (lighter + 0.05) / (darker + 0.05)def contrast_severity_label(ratio):if ratio < 1.5:return "could be much clearer"elif ratio < 2.5:return "could be clearer"elif ratio < 3.5:return "slightly less clear than ideal"else:return "close to the recommended level"3 unmodified linespad = max(2, int(h * 0.3))y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)x0, x1 = max(0, x - pad), min(img_arr.shape[1], x + w + pad)region = img_arr[y0:y1, x0:x1]gray = cv2.cvtColor(region, cv2.COLOR_RGB2GRAY)thresh = np.median(gray)dark_mask = gray < threshlight_mask = ~dark_maskdark_px = region[dark_mask]light_px = region[light_mask]if len(dark_px) == 0 or len(light_px) == 0:return None, Nonetext_color = tuple(np.median(dark_px, axis=0).astype(int))bg_color = tuple(np.median(light_px, axis=0).astype(int))return text_color, bg_colordef analyze_readability(pil_img):1 unmodified lineimg_h = img_arr.shape[0]gray = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 10)ocr_data = pytesseract.image_to_data(bw, output_type=pytesseract.Output.DICT)words_checked = 015 unmodified linescontinueratio = contrast_ratio(text_color, bg_color)size_pct = (h / img_h) * 100if ratio < 4.5:contrast_issues += 1if len(issue_lines) < 5:severity = contrast_severity_label(ratio)issue_lines.append(f"- \"{word}\" {severity} against its background")issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})if size_pct < 1.2:254 unmodified linessummary += ("\n\n---\n""**This tool flags what's off, not what you have to do.** I'm Haan - ""a brand identity and visual designer with 12+ years turning insights like ""these into designs that actually convert, stay true to your brand, and ""look intentional (not just \"fixed\").\n\n"293 unmodified lines2942952962972982993003013023033043053063073083093103 unmodified lines3143153163173183193203213223233243253263273283293301 unmodified line33233333433533633733833934034134234334434534634734815 unmodified lines364365366367368369370371372373374254 unmodified lines629630631632633634635293 unmodified linesreturn (lighter + 0.05) / (darker + 0.05)def required_contrast_ratio(size_pct):return 3.0 if size_pct >= 3.0 else 4.5def contrast_severity_label(ratio, required_ratio):deficit = (required_ratio - ratio) / required_ratioif deficit > 0.6:return "could be much clearer"elif deficit > 0.35:return "could be clearer"elif deficit > 0.15:return "slightly less clear than ideal"else:return "close to the recommended level"3 unmodified linespad = max(2, int(h * 0.3))y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)x0, x1 = max(0, x - pad), min(img_arr.shape[1], x + w + pad)region = img_arr[y0:y1, x0:x1].reshape(-1, 3).astype(np.float32)if len(region) < 10:return None, Nonecriteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)try:_, _, centers = cv2.kmeans(region, 2, None, criteria, 3, cv2.KMEANS_PP_CENTERS)except cv2.error:return None, Nonecolor1 = tuple(centers[0].astype(int))color2 = tuple(centers[1].astype(int))return color1, color2def analyze_readability(pil_img):1 unmodified lineimg_h = img_arr.shape[0]gray = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))gray_enhanced = clahe.apply(gray)gray_denoised = cv2.medianBlur(gray_enhanced, 3)bw = cv2.adaptiveThreshold(gray_denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 10)cleanup_kernel = np.ones((2, 2), np.uint8)bw = cv2.morphologyEx(bw, cv2.MORPH_OPEN, cleanup_kernel)ocr_data = pytesseract.image_to_data(bw, output_type=pytesseract.Output.DICT)words_checked = 015 unmodified linescontinueratio = contrast_ratio(text_color, bg_color)size_pct = (h / img_h) * 100required_ratio = required_contrast_ratio(size_pct)if ratio < required_ratio:contrast_issues += 1if len(issue_lines) < 5:severity = contrast_severity_label(ratio, required_ratio)issue_lines.append(f"- \"{word}\" {severity} against its background")issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})if size_pct < 1.2:254 unmodified linessummary += ("\n\n---\n""**This tool flags what's off. A designer knows what's right.** I'm Haan - ""a brand identity and visual designer with 12+ years turning insights like ""these into designs that actually convert, stay true to your brand, and ""look intentional (not just \"fixed\").\n\n"
app.py+34-19