Created
May 10, 2026 14:30
-
-
Save me-suzy/343c4491189c221addfed7b8aa3c8ca3 to your computer and use it in GitHub Desktop.
Negru pe alb.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
| import cv2 | |
| import numpy as np | |
| from pathlib import Path | |
| INPUT_DIR = Path(r"g:\Colectia EMINESCIANA") | |
| OUTPUT_DIR = Path(r"g:\Colectia EMINESCIANA\Output") | |
| OUTPUT_DIR.mkdir(parents=True, exist_ok=True) | |
| def procesare_structura_continua(img_path): | |
| # 1. Citire si Inversare (lucram cu text alb pe negru pentru procesare) | |
| img = cv2.imread(str(img_path), cv2.IMREAD_GRAYSCALE) | |
| if img is None: return | |
| img = 255 - img # Invert | |
| # 2. Eliminare fundal gri (Threshold agresiv pe negativ) | |
| # Orice e gri murdar sub 130 dispare, ramane doar ce e intens (litere + puncte mari) | |
| _, mask = cv2.threshold(img, 130, 255, cv2.THRESH_BINARY) | |
| # 3. METODA CONTINUITĂȚII: Filtru de formă verticală | |
| # Literele au linii verticale. Punctele aleatorii nu au. | |
| kernel_v = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 3)) | |
| # 'Open' sterge tot ce nu se potriveste structurii verticale de 3 pixeli inaltime | |
| text_struct = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_v) | |
| # 4. RECONSTRUCȚIE (Facem textul mai negru si plin) | |
| # Dilatam orizontal pentru a uni partile literelor mancate | |
| kernel_h = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1)) | |
| dilated = cv2.dilate(text_struct, kernel_h, iterations=1) | |
| # 5. INVERSARE FINALĂ (Text negru pe alb pur) | |
| final = 255 - dilated | |
| # 6. SHARPEN (Optional - pentru a face marginile literelor "tăioase") | |
| sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) | |
| final = cv2.filter2D(final, -1, sharpen_kernel) | |
| return final | |
| # Procesare masivă | |
| for path in INPUT_DIR.glob("*.*"): | |
| if path.suffix.lower() in ['.jpg', '.jpeg', '.png']: | |
| print(f"🔍 Urmărire continuitate text: {path.name}") | |
| rezultat = procesare_structura_continua(path) | |
| if rezultat is not None: | |
| cv2.imwrite(str(OUTPUT_DIR / path.name), rezultat) | |
| print("✅ Gata! Fundalul ar trebui să fie alb pur, iar textul dens.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment