Created
February 13, 2026 18:34
-
-
Save me-suzy/a0060ca5e96911c5a989dc4bb11cdc99 to your computer and use it in GitHub Desktop.
465tgh.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import cv2 | |
| import numpy as np | |
| from pathlib import Path | |
| import sys | |
| try: | |
| if sys.platform == 'win32' and hasattr(sys.stdout, 'buffer'): | |
| import io | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') | |
| sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace') | |
| except: | |
| pass | |
| def detect_colored_lines(img): | |
| """Detecteaza liniile colorate: rosii, albastre, verzi, galbene""" | |
| hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
| # Rosu (doua intervale) | |
| mask_red1 = cv2.inRange(hsv, np.array([0, 50, 50]), np.array([10, 255, 255])) | |
| mask_red2 = cv2.inRange(hsv, np.array([160, 50, 50]), np.array([180, 255, 255])) | |
| mask_red = cv2.bitwise_or(mask_red1, mask_red2) | |
| # Albastru | |
| mask_blue = cv2.inRange(hsv, np.array([100, 50, 50]), np.array([130, 255, 255])) | |
| # Verde | |
| mask_green = cv2.inRange(hsv, np.array([35, 50, 50]), np.array([85, 255, 255])) | |
| # Galben | |
| mask_yellow = cv2.inRange(hsv, np.array([20, 50, 50]), np.array([35, 255, 255])) | |
| # Combinam toate culorile | |
| combined_mask = cv2.bitwise_or(mask_red, mask_blue) | |
| combined_mask = cv2.bitwise_or(combined_mask, mask_green) | |
| combined_mask = cv2.bitwise_or(combined_mask, mask_yellow) | |
| # Filtram dupa saturatie | |
| s = hsv[:, :, 1] | |
| saturation_mask = (s > 40).astype(np.uint8) * 255 | |
| combined_mask = cv2.bitwise_and(combined_mask, saturation_mask) | |
| # Detectam linii orizontale | |
| h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 1)) | |
| lines_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, h_kernel) | |
| # Detectam linii la unghiuri mici | |
| for angle in [-10, -7, -5, -3, 3, 5, 7, 10]: | |
| length = 30 | |
| rad = np.deg2rad(angle) | |
| dx = int(length * np.cos(rad)) | |
| dy = int(length * np.sin(rad)) | |
| kernel_size = max(abs(dx), abs(dy)) + 1 | |
| kernel = np.zeros((kernel_size * 2 + 1, kernel_size * 2 + 1), dtype=np.uint8) | |
| cv2.line(kernel, (kernel_size - dx, kernel_size - dy), | |
| (kernel_size + dx, kernel_size + dy), 1, 2) | |
| lines_angle = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, kernel) | |
| lines_mask = cv2.bitwise_or(lines_mask, lines_angle) | |
| # Dilatam pentru acoperire completa | |
| dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 7)) | |
| lines_mask = cv2.dilate(lines_mask, dilate_kernel, iterations=2) | |
| return lines_mask | |
| def remove_colored_lines(img, lines_mask): | |
| """Elimina liniile colorate prin inpainting""" | |
| result = cv2.inpaint(img, lines_mask, 3, cv2.INPAINT_TELEA) | |
| return result | |
| def enhance_text_preserve_details(img): | |
| """Face textul mai inchis PASTRAND detaliile literelor""" | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| # Adaptive threshold - pastreaza detaliile locale | |
| # blockSize mare = mai putine artefacte | |
| # C mic = pastreaza mai mult din text | |
| adaptive = cv2.adaptiveThreshold( | |
| gray, 255, | |
| cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
| cv2.THRESH_BINARY, | |
| blockSize=15, # marime bloc pentru calcul local | |
| C=8 # cat de mult sa albeasca fundalul (valoare mica = mai mult text pastrat) | |
| ) | |
| # Morphological closing - umple gaurile mici din litere | |
| close_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2)) | |
| closed = cv2.morphologyEx(adaptive, cv2.MORPH_CLOSE, close_kernel) | |
| # Optional: efect BOLD usor | |
| inverted = cv2.bitwise_not(closed) | |
| bold_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2)) | |
| dilated = cv2.dilate(inverted, bold_kernel, iterations=1) | |
| result = cv2.bitwise_not(dilated) | |
| return result | |
| def process_image(image_path, output_path): | |
| img = cv2.imread(str(image_path)) | |
| if img is None: | |
| raise ValueError(f"Nu s-a putut incarca: {image_path}") | |
| print(f" Procesare: {Path(image_path).name}") | |
| # Pasul 1: Eliminam liniile colorate | |
| lines_mask = detect_colored_lines(img) | |
| if np.any(lines_mask > 0): | |
| img_clean = remove_colored_lines(img, lines_mask) | |
| print(" -> Linii colorate eliminate") | |
| else: | |
| img_clean = img | |
| print(" -> Nu s-au detectat linii colorate") | |
| # Pasul 2: Text clar cu detalii pastrate | |
| result = enhance_text_preserve_details(img_clean) | |
| cv2.imwrite(str(output_path), result) | |
| print(f" [OK] Salvat: {output_path}") | |
| return result | |
| def process_folder(input_folder, output_folder): | |
| input_path = Path(input_folder) | |
| output_path = Path(output_folder) | |
| output_path.mkdir(parents=True, exist_ok=True) | |
| extensions = ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.tiff', '*.tif'] | |
| images = [] | |
| for ext in extensions: | |
| images.extend(input_path.glob(ext)) | |
| images.extend(input_path.glob(ext.upper())) | |
| images = sorted(set(images)) | |
| if not images: | |
| print(f"Nu s-au gasit imagini in: {input_folder}") | |
| return | |
| print(f"Gasite {len(images)} imagini de procesat\n") | |
| success = 0 | |
| failed = 0 | |
| for img_path in images: | |
| output_file = output_path / img_path.name | |
| try: | |
| process_image(img_path, output_file) | |
| success += 1 | |
| except Exception as e: | |
| print(f" [EROARE] {img_path.name}: {e}") | |
| failed += 1 | |
| print(f"\n{'='*60}") | |
| print(f"REZULTAT: {success} procesate, {failed} erori") | |
| print(f"{'='*60}") | |
| if __name__ == "__main__": | |
| input_folder = r"e:\Carte\BB\17 - Site Leadership\alte\Ionel Balauta\Aryeht\Task 1 - Traduce tot site-ul\Doar Google Web\Andreea\Meditatii\2023\Edit Text Images (Remove shadows + Remove red LInes)\TTT" | |
| output_folder = r"e:\Carte\BB\17 - Site Leadership\alte\Ionel Balauta\Aryeht\Task 1 - Traduce tot site-ul\Doar Google Web\Andreea\Meditatii\2023\Edit Text Images (Remove shadows + Remove red LInes)\Output" | |
| print("=" * 60) | |
| print("PROCESARE - Text clar + Eliminare linii colorate") | |
| print("=" * 60) | |
| print(f"Input: {input_folder}") | |
| print(f"Output: {output_folder}") | |
| print("=" * 60 + "\n") | |
| process_folder(input_folder, output_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment