Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created May 11, 2026 05:00
Show Gist options
  • Select an option

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

Select an option

Save me-suzy/9e04cde802b590f120812c4dd00bd8a9 to your computer and use it in GitHub Desktop.
inchis.py
"""Script standalone "Inchis" - face textul gri / slab contrast mai NEGRU.
Pipeline minimalist (fara CLAHE/unsharp -> fara umbre/halouri):
1. Median 3x3 - praf fin
2. LEVELS Photoshop: BP=80, WP=240, gamma=0.65
-> bg >240 devine alb pur, text gri (100) -> dark (~12)
PASTREAZA anti-aliasing (italice, serife) - NU binarizeaza.
"""
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)
BP = 80 # black point - inputs below devin negru
WP = 240 # white point - inputs above devin alb
GAMMA = 0.65 # < 1 darkens midtones
JPEG_QUALITY = 80
def proc_inchis(img):
"""Returneaza grayscale cu textul mult mai negru, fundal alb pur, fara umbre."""
if img.ndim == 3:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
gray = img
# 1. Median 3x3 - praf fin
g = cv2.medianBlur(gray, 3)
# 2. Photoshop-style Levels
f = g.astype(np.float32)
norm = np.clip((f - BP) / max(1, WP - BP), 0, 1)
leveled = np.power(norm, 1.0 / GAMMA) * 255
return np.clip(leveled, 0, 255).astype(np.uint8)
def main():
extensions = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".webp"}
files = sorted(
f for f in INPUT_DIR.iterdir()
if f.is_file() and f.suffix.lower() in extensions
)
if not files:
print(f"Nu am gasit imagini in {INPUT_DIR}")
return
print(f"Procesez {len(files)} imagini din {INPUT_DIR}")
for i, p in enumerate(files, 1):
print(f" [{i}/{len(files)}] {p.name}")
# Citire (suport pentru caractere non-ASCII pe Windows)
data = np.fromfile(str(p), dtype=np.uint8)
img = cv2.imdecode(data, cv2.IMREAD_UNCHANGED)
if img is None:
print(f" SKIP - nu pot citi")
continue
out = proc_inchis(img)
out_path = OUTPUT_DIR / (p.stem + ".jpg")
# Salvare (suport pentru caractere non-ASCII pe Windows)
ok, buf = cv2.imencode(".jpg", out, [cv2.IMWRITE_JPEG_QUALITY, JPEG_QUALITY])
if ok:
buf.tofile(str(out_path))
print(f"Gata! Iesire: {OUTPUT_DIR}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment