from pathlib import Path from PIL import Image, ImageEnhance, ImageOps src = Path(r"C:\Apache24\htdocs\mikrotik\admin\assets\id_cards") out = Path(r"C:\Apache24\htdocs\.codex_tmp\ocr_crops") out.mkdir(parents=True, exist_ok=True) for path in src.glob("bulk_*_both_*.jpg"): img = Image.open(path).convert("L") w, h = img.size crop = img.crop((int(w * .12), 0, int(w * .88), max(1, int(h * .13)))) crop = ImageOps.autocontrast(crop) crop = crop.resize((crop.width * 4, crop.height * 4)) crop = ImageEnhance.Contrast(crop).enhance(2) crop.save(out / f"{path.stem}.png") print(sum(1 for _ in out.glob('*.png')))