Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created May 10, 2026 14:38
Show Gist options
  • Select an option

  • Save me-suzy/9e19827ee81ebc8be50d46345e512933 to your computer and use it in GitHub Desktop.

Select an option

Save me-suzy/9e19827ee81ebc8be50d46345e512933 to your computer and use it in GitHub Desktop.
st4465.py
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_rocada_continuitate(img_path):
# 1. Citire si trecere in NEGATIV
img = cv2.imread(str(img_path), cv2.IMREAD_GRAYSCALE)
if img is None: return
# Facem fundalul negru si scrisul alb (sau gri deschis)
negativ = 255 - img
# 2. Eliminare "Zgomot" (Orice nu e destul de alb pe fundalul negru dispare)
# Tot ce e sub 140 (gri murdar) devine NEGRU pur (fundal)
_, mask = cv2.threshold(negativ, 140, 255, cv2.THRESH_BINARY)
# 3. FILTRU DE CONTINUITATE (Urmărim structura literelor)
# Stergem punctele albe care nu sunt legate vertical (minim 3 pixeli)
kernel_v = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 3))
text_curat_alb = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_v)
# 4. RECONSTRUCȚIE (Îngroșăm textul alb pe fundalul negru)
kernel_reconstructie = np.ones((2, 2), np.uint8)
text_dens_alb = cv2.dilate(text_curat_alb, kernel_reconstructie, iterations=1)
# 5. ROCADA FINALĂ: Tot ce e alb devine NEGRU, restul ALB PUR
# Invertim masca curatata pentru a obtine text negru pe alb
final = 255 - text_dens_alb
# 6. SHARPEN (Optional - forțează marginile să fie 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ă
files = [f for f in INPUT_DIR.glob("*.*") if f.suffix.lower() in ['.jpg', '.jpeg', '.png']]
print(f"🚀 Pornesc rocada pentru {len(files)} fișiere...")
for path in files:
print(f"🔄 Prelucrare: {path.name}")
rezultat = procesare_rocada_continuitate(path)
if rezultat is not None:
cv2.imwrite(str(OUTPUT_DIR / path.name), rezultat)
print("✅ Gata! Acum ai text negru dens pe fundal alb pur.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment