[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7C7V4XW19THR7ZKCDMJ3PAN":3,"skill-same:01M7C7V4XW19THR7ZKCDMJ3PAN":45},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":6,"ownerHtmlUrl":29,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":30,"install":31,"resources":44},"01M7C7V4XW19THR7ZKCDMJ3PAN","image-ocr",null,"Extract text content from images using Tesseract OCR via Python","linyouxue\u002Fskill-repair-benchmark","linyouxue","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F216151464?v=4","01M7BJEVCXKD74KH6FG9CBVSQB","林宥学","01M7BJEVE02NP217V93BKZFC0J",0,"Apache-2.0","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskillopt__opus47-tatu-r2-image-aware\u002Fevidence\u002Ffinal_runs\u002Frun_03\u002Finputs\u002Fskills\u002Fimage-ocr\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: image-ocr\ndescription: Extract text content from images using Tesseract OCR via Python\n---\n\n# Image OCR Skill\n\n## Purpose\nThis skill enables accurate text extraction from image files (JPG, PNG, etc.) using Tesseract OCR via the `pytesseract` Python library. It is suitable for scanned documents, screenshots, photos of text, receipts, forms, and other visual content containing text.\n\n## When to Use\n- Extracting text from scanned documents or photos\n- Reading text from screenshots or image captures\n- Processing batch image files that contain textual information\n- Converting visual documents to machine-readable text\n- Extracting structured data from forms, receipts, or tables in images\n\n\n\n## Receipt Field Extraction (Date + Total)\n\nThis section is a domain-specific recipe for extracting `date` (ISO `YYYY-MM-DD`) and `total_amount` (two-decimal string) from scanned receipt images (Malaysian\u002FSEA style). Use this recipe when the task asks for receipt totals and dates; the generic OCR sections below are inadequate on their own.\n\n### Priority Tiers and Exclusions\n\n```python\nTIERS = [\n    ['GRAND TOTAL'],\n    ['TOTAL RM', 'TOTAL: RM'],\n    ['TOTAL AMOUNT'],\n    ['TOTAL', 'AMOUNT', 'TOTAL DUE', 'AMOUNT DUE', 'BALANCE DUE',\n     'NETT TOTAL', 'NET TOTAL', 'ROUNDED TOTAL', 'TOTAL ROUNDED',\n     'TOTAL AFTER ROUNDING', 'TOTAL INCL GST', 'TOTAL RM INCLUDING GST'],\n]\n\nEXCLUSIONS = [\n    'SUBTOTAL', 'SUB TOTAL', 'SUB-TOTAL',\n    'TAX', 'GST', 'SST',\n    'DISCOUNT', 'CHANGE',\n    'CASH TENDERED', 'CASH RECEIVED', 'CASH PAYMENT',\n    'VISA', 'MASTERCARD', 'DEBIT', 'CREDIT', 'CARD', 'PAYMENT',\n    'ROUNDING ADJ', 'ROUNDING ADJUSTMENT',\n    'TOTAL GST', 'TOTAL TAX', 'TOTAL SST', 'GST TOTAL', 'TAX TOTAL',\n    'TOTAL ITEMS', 'TOTAL ITEM(S)', 'ITEM COUNT', 'TOTAL QTY',\n    'TOTAL QUANTITY', 'TOTAL SAVINGS',\n    'EXCLUDING', 'EXCL GST', 'AMT EXCL',\n    'DESCRIPTION', 'QTY PRICE AMOUNT', 'PRICE AMOUNT',\n    'AVERAGE', 'PAX',\n]\n```\n\nIMPORTANT nuances:\n- Distinguish `TOTAL GST` \u002F `GST TOTAL` (EXCLUDE) from `TOTAL INCL GST` \u002F `TOTAL (INCLUSIVE OF GST)` (INCLUDE). Check INCLUDE patterns before applying EXCLUSIONS.\n- Once any line contains `GST SUMMARY`, `TAX SUMMARY`, or `TAX CODE`, mark all subsequent lines as excluded for total selection (summary tables often repeat a 'Total' row).\n\n### Amount Regex and Normalization\n\n```python\nimport re\nAMOUNT_RE = re.compile(r'(?\u003C![\\w.])(\\d{1,3}(?:,\\d{3})+\\.\\d{2}|\\d+\\.\\d{2})(?![\\w.\\d])')\n\ndef to_float(tok):\n    return float(tok.replace(',', ''))\n\ndef fmt_amount(v):\n    return f'{v:.2f}'\n\ndef normalize_numeric_tokens(line):\n    # Apply only inside tokens that look numeric-ish (contain a digit).\n    def fix_token(tok):\n        if not re.search(r'\\d', tok):\n            return tok\n        t = tok\n        t = t.replace('O', '0').replace('o', '0').replace('Q', '0').replace('u', '0')\n        t = t.replace('l', '1').replace('I', '1').replace('|', '1')\n        t = t.replace('S', '5').replace('B', '8')\n        t = t.replace(':', '.')\n        return t\n    line = re.sub(r'\\bRN\\b', 'RM', line)\n    # Collapse 'NN NN' at end of a keyword line into 'NN.NN'\n    line = re.sub(r'(\\d{1,4})\\s+(\\d{2})\\s*$', r'\\1.\\2', line)\n    return ' '.join(fix_token(t) for t in line.split(' '))\n```\n\n### Multi-Variant OCR\n\nGenerate at least four variants and collect text from each:\n1. Original, `--psm 6`\n2. Grayscale + autocontrast, `--psm 6`\n3. 2x LANCZOS upscale of #2, `--psm 6`\n4. 3x LANCZOS upscale of #2, `--psm 4`\n5. (optional) Binarized\u002Finverted for faded scans, `--psm 11`\n\n```python\nfrom PIL import Image, ImageOps\nimport pytesseract\n\ndef ocr_variants(image_path):\n    img = Image.open(image_path)\n    gray = ImageOps.autocontrast(ImageOps.grayscale(img))\n    w, h = gray.size\n    up2 = gray.resize((w*2, h*2), Image.LANCZOS)\n    up3 = gray.resize((w*3, h*3), Image.LANCZOS)\n    specs = [(img, '--psm 6'), (gray, '--psm 6'),\n             (up2, '--psm 6'), (up3, '--psm 4')]\n    texts = []\n    for im, cfg in specs:\n        try:\n            texts.append(pytesseract.image_to_string(im, config=cfg))\n        except Exception:\n            pass\n    return texts\n```\n\n### Selection Algorithm (Total)\n\nFor each OCR variant text, split into lines; for each line:\n1. Apply `normalize_numeric_tokens`.\n2. If a prior line matched `GST SUMMARY`\u002F`TAX SUMMARY`\u002F`TAX CODE`, treat this line as excluded.\n3. If the uppercased line contains any EXCLUSION keyword (and NOT an INCLUDE pattern like `TOTAL INCL GST` \u002F `INCLUSIVE OF GST`), skip.\n4. For each tier (lowest index wins), if the line contains a tier keyword, extract 2-decimal amounts via `AMOUNT_RE`.\n   - If amounts found: record `(tier_index, value, line_index, variant_index)` for the LAST amount on that line.\n   - If no amounts AND the line contains no digits at all: look ahead up to 2 non-empty, non-excluded lines; take the LAST 2-decimal number found. Record as candidate.\n   - If the keyword line has partial digits but no valid 2-decimal match: do NOT fall through (prevents Visa\u002FCash leakage).\n\nFinal pick across all variants:\n1. Choose the lowest `tier_index` that has any candidate.\n2. Within that tier, prefer values corroborated by (a) appearing on Subtotal\u002FPayment\u002FCash\u002FRounded lines on the same receipt, or (b) appearing in multiple variants.\n3. Tiebreak by cross-variant frequency, then by LAST line_index (so a final `TOTAL: 23.25` beats an earlier `Total (Inclusive of GST): 23.26` when a `Rounding Adj: -0.01` sits between them).\n4. Output `fmt_amount(value)`.\n\nWorked example:\n```\nTotal (Inclusive of GST):   23.26\nRounding Adj:               -0.01\nTOTAL:                      23.25\n```\nBoth `23.26` and `23.25` are tier-3 candidates. The later line wins → `total_amount = '23.25'`.\n\n### Date Extraction\n\n```python\nimport re\nfrom datetime import date\n\nDATE_PATTERNS = [\n    (re.compile(r'(\\d{1,2})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{4})'), 'dmy4'),\n    (re.compile(r'(\\d{4})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{1,2})'), 'ymd'),\n    (re.compile(r'(\\d{1,2})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{2})\\b'), 'dmy2'),\n    (re.compile(r'(\\d{1,2})\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\\s+(\\d{4})', re.I), 'dmony'),\n]\nMONTHS = {m:i+1 for i,m in enumerate(['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'])}\n\ndef _valid(y, mo, d):\n    try:\n        date(y, mo, d); return 2000 \u003C= y \u003C= 2099\n    except Exception:\n        return False\n\ndef _try_swaps(y, mo, d):\n    swaps = [('6','0'),('0','6'),('8','3'),('3','8'),('1','7'),('7','1'),('5','6'),('6','5')]\n    for field in ('mo','d'):\n        s = str(mo if field=='mo' else d)\n        for a,b in swaps:\n            if a in s:\n                cand = int(s.replace(a,b,1))\n                nm, nd = (cand, d) if field=='mo' else (mo, cand)\n                if _valid(y, nm, nd):\n                    return y, nm, nd\n    return None\n\ndef extract_date(text):\n    lines = text.splitlines()\n    # Prefer lines near Date\u002FTarikh\u002FINV labels first\n    scored = []\n    for i, ln in enumerate(lines):\n        boost = 1 if re.search(r'date|tarikh|inv', ln, re.I) else 0\n        scored.append((boost, i, ln))\n    scored.sort(key=lambda x: (-x[0], x[1]))\n    for _, _, ln in scored:\n        for rx, kind in DATE_PATTERNS:\n            m = rx.search(ln)\n            if not m: continue\n            if kind == 'dmy4':\n                d, mo, y = int(m.group(1)), int(m.group(2)), int(m.group(3))\n                if mo > 12 and d \u003C= 12:  # fallback to MM\u002FDD\n                    d, mo = mo, d\n            elif kind == 'ymd':\n                y, mo, d = int(m.group(1)), int(m.group(2)), int(m.group(3))\n            elif kind == 'dmy2':\n                d, mo, yy = int(m.group(1)), int(m.group(2)), int(m.group(3))\n                y = 2000 + yy if yy \u003C= 49 else 1900 + yy\n            elif kind == 'dmony':\n                d = int(m.group(1)); mo = MONTHS[m.group(2).lower()[:3]]; y = int(m.group(3))\n            if _valid(y, mo, d):\n                return f'{y:04d}-{mo:02d}-{d:02d}'\n            fix = _try_swaps(y, mo, d)\n            if fix:\n                y, mo, d = fix\n                return f'{y:04d}-{mo:02d}-{d:02d}'\n    return None\n```\n\nFor ambiguous `DD\u002FMM` vs `MM\u002FDD`, default to `DD\u002FMM\u002FYYYY` (Malaysian\u002FSEA convention). Example: `09\u002F03\u002F2018` → `2018-03-09` (NOT `2018-08-09`).\n\n### Reference Function and Batch Driver\n\n```python\ndef extract_receipt_fields(image_path):\n    variant_texts = ocr_variants(image_path)\n    # --- total selection ---\n    include_pat = re.compile(r'TOTAL\\s*(\\(?\\s*INCL(?:USIVE)?(?:\\s*OF)?\\s*GST|INCL\\s*GST)', re.I)\n    candidates = []  # (tier, value, line_idx, variant_idx, line_text)\n    corroborators = set()\n    for vi, raw in enumerate(variant_texts):\n        in_summary = False\n        lines = [normalize_numeric_tokens(l) for l in raw.splitlines()]\n        for li, line in enumerate(lines):\n            U = line.upper()\n            if any(k in U for k in ('GST SUMMARY','TAX SUMMARY','TAX CODE')):\n                in_summary = True\n            # Corroborators\n            if any(k in U for k in ('SUBTOTAL','SUB TOTAL','CASH','PAYMENT','ROUNDED','TENDERED','CHANGE')):\n                for m in AMOUNT_RE.finditer(line):\n                    corroborators.add(to_float(m.group(1)))\n            is_include = bool(include_pat.search(U))\n            is_excluded = in_summary or (not is_include and any(k in U for k in EXCLUSIONS))\n            if is_excluded:\n                continue\n            for ti, kws in enumerate(TIERS):\n                if any(k in U for k in kws):\n                    amounts = [to_float(m.group(1)) for m in AMOUNT_RE.finditer(line)]\n                    if amounts:\n                        candidates.append((ti, amounts[-1], li, vi, line))\n                    elif not re.search(r'\\d', line):\n                        # split-line fallback: look ahead up to 2 non-excluded lines\n                        for look in range(li+1, min(li+3, len(lines))):\n                            nxt = lines[look]\n                            if not nxt.strip(): continue\n                            NU = nxt.upper()\n                            if any(k in NU for k in EXCLUSIONS): continue\n                            nx = [to_float(m.group(1)) for m in AMOUNT_RE.finditer(nxt)]\n                            if nx:\n                                candidates.append((ti, nx[-1], look, vi, nxt))\n                                break\n                    break  # only match the strongest tier per line\n    total_str = None\n    if candidates:\n        best_tier = min(c[0] for c in candidates)\n        pool = [c for c in candidates if c[0] == best_tier]\n        from collections import Counter\n        freq = Counter(round(c[1], 2) for c in pool)\n        def score(c):\n            v = round(c[1], 2)\n            return (freq[v], 1 if v in {round(x,2) for x in corroborators} else 0, c[2])\n        pool.sort(key=score, reverse=True)\n        total_str = fmt_amount(pool[0][1])\n    # --- date ---\n    date_iso = None\n    for raw in variant_texts:\n        date_iso = extract_date(raw)\n        if date_iso: break\n    return {'date': date_iso, 'total_amount': total_str}\n\n# Batch driver → xlsx\ndef build_xlsx(img_dir, out_path):\n    import os, openpyxl\n    from pathlib import Path\n    wb = openpyxl.Workbook()\n    ws = wb.active\n    ws.title = 'results'\n    ws.append(['filename', 'date', 'total_amount'])\n    files = sorted(p.name for p in Path(img_dir).iterdir()\n                   if p.suffix.lower() in {'.jpg','.jpeg','.png','.webp'})\n    for name in files:\n        res = extract_receipt_fields(os.path.join(img_dir, name))\n        ws.append([name, res['date'], res['total_amount']])\n    wb.save(out_path)\n```\n\n### Output Contract\n- `total_amount`: two-decimal string (e.g., `'47.70'`), or `None` on failure.\n- `date`: ISO `YYYY-MM-DD` string, or `None` on failure.\n- xlsx contains exactly one sheet `results`, header row + sorted-by-filename data rows, no extra columns\u002Fsheets.\n\n## Required Libraries\n\nThe following Python libraries are required:\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\n```\n\n## Input Requirements\n- **File formats**: JPG, JPEG, PNG, WEBP\n- **Image quality**: Minimum 300 DPI recommended for printed text; clear and legible text\n- **File size**: Under 5MB per image (resize if necessary)\n- **Text language**: Specify if non-English to improve accuracy\n\n## Output Schema\nAll extracted content must be returned as valid JSON conforming to this schema:\n\n```json\n{\n  \"success\": true,\n  \"filename\": \"example.jpg\",\n  \"extracted_text\": \"Full raw text extracted from the image...\",\n  \"confidence\": \"high|medium|low\",\n  \"metadata\": {\n    \"language_detected\": \"en\",\n    \"text_regions\": 3,\n    \"has_tables\": false,\n    \"has_handwriting\": false\n  },\n  \"warnings\": [\n    \"Text partially obscured in bottom-right corner\",\n    \"Low contrast detected in header section\"\n  ]\n}\n```\n\n\n### Field Descriptions\n\n- `success`: Boolean indicating whether text extraction completed\n- `filename`: Original image filename\n- `extracted_text`: Complete text content in reading order (top-to-bottom, left-to-right)\n- `confidence`: Overall OCR confidence level based on image quality and text clarity\n- `metadata.language_detected`: ISO 639-1 language code\n- `metadata.text_regions`: Number of distinct text blocks identified\n- `metadata.has_tables`: Whether tabular data structures were detected\n- `metadata.has_handwriting`: Whether handwritten text was detected\n- `warnings`: Array of quality issues or potential errors\n\n\n## Code Examples\n\n### Basic OCR Extraction\n\n```python\nimport pytesseract\nfrom PIL import Image\n\ndef extract_text_from_image(image_path):\n    \"\"\"Extract text from a single image using Tesseract OCR.\"\"\"\n    img = Image.open(image_path)\n    text = pytesseract.image_to_string(img)\n    return text.strip()\n```\n\n### OCR with Confidence Data\n\n```python\nimport pytesseract\nfrom PIL import Image\n\ndef extract_with_confidence(image_path):\n    \"\"\"Extract text with per-word confidence scores.\"\"\"\n    img = Image.open(image_path)\n\n    # Get detailed OCR data including confidence\n    data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\n\n    words = []\n    confidences = []\n\n    for i, word in enumerate(data['text']):\n        if word.strip():  # Skip empty strings\n            words.append(word)\n            confidences.append(data['conf'][i])\n\n    # Calculate average confidence\n    avg_confidence = sum(c for c in confidences if c > 0) \u002F len([c for c in confidences if c > 0]) if confidences else 0\n\n    return {\n        'text': ' '.join(words),\n        'average_confidence': avg_confidence,\n        'word_count': len(words)\n    }\n```\n\n### Full OCR with JSON Output\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\n\ndef ocr_to_json(image_path):\n    \"\"\"Perform OCR and return results as JSON.\"\"\"\n    filename = os.path.basename(image_path)\n    warnings = []\n\n    try:\n        img = Image.open(image_path)\n\n        # Get detailed OCR data\n        data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\n\n        # Extract text preserving structure\n        text = pytesseract.image_to_string(img)\n\n        # Calculate confidence\n        confidences = [c for c in data['conf'] if c > 0]\n        avg_conf = sum(confidences) \u002F len(confidences) if confidences else 0\n\n        # Determine confidence level\n        if avg_conf >= 80:\n            confidence = \"high\"\n        elif avg_conf >= 50:\n            confidence = \"medium\"\n        else:\n            confidence = \"low\"\n            warnings.append(f\"Low OCR confidence: {avg_conf:.1f}%\")\n\n        # Count text regions (blocks)\n        block_nums = set(data['block_num'])\n        text_regions = len([b for b in block_nums if b > 0])\n\n        result = {\n            \"success\": True,\n            \"filename\": filename,\n            \"extracted_text\": text.strip(),\n            \"confidence\": confidence,\n            \"metadata\": {\n                \"language_detected\": \"en\",\n                \"text_regions\": text_regions,\n                \"has_tables\": False,\n                \"has_handwriting\": False\n            },\n            \"warnings\": warnings\n        }\n\n    except Exception as e:\n        result = {\n            \"success\": False,\n            \"filename\": filename,\n            \"extracted_text\": \"\",\n            \"confidence\": \"low\",\n            \"metadata\": {\n                \"language_detected\": \"unknown\",\n                \"text_regions\": 0,\n                \"has_tables\": False,\n                \"has_handwriting\": False\n            },\n            \"warnings\": [f\"OCR failed: {str(e)}\"]\n        }\n\n    return result\n\n# Usage\nresult = ocr_to_json(\"document.jpg\")\nprint(json.dumps(result, indent=2))\n```\n\n### Batch Processing Multiple Images\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\nfrom pathlib import Path\n\ndef process_image_directory(directory_path, output_file):\n    \"\"\"Process all images in a directory and save results.\"\"\"\n    image_extensions = {'.jpg', '.jpeg', '.png', '.webp'}\n    results = []\n\n    for file_path in sorted(Path(directory_path).iterdir()):\n        if file_path.suffix.lower() in image_extensions:\n            result = ocr_to_json(str(file_path))\n            results.append(result)\n            print(f\"Processed: {file_path.name}\")\n\n    # Save results\n    with open(output_file, 'w') as f:\n        json.dump(results, f, indent=2)\n\n    return results\n```\n\n\n## Tesseract Configuration Options\n\n### Language Selection\n\n```python\n# Specify language (default is English)\ntext = pytesseract.image_to_string(img, lang='eng')\n\n# Multiple languages\ntext = pytesseract.image_to_string(img, lang='eng+fra+deu')\n```\n\n### Page Segmentation Modes (PSM)\n\nUse `--psm` to control how Tesseract segments the image:\n\n```python\n# PSM 3: Fully automatic page segmentation (default)\ntext = pytesseract.image_to_string(img, config='--psm 3')\n\n# PSM 4: Assume single column of text\ntext = pytesseract.image_to_string(img, config='--psm 4')\n\n# PSM 6: Assume uniform block of text\ntext = pytesseract.image_to_string(img, config='--psm 6')\n\n# PSM 11: Sparse text - find as much text as possible\ntext = pytesseract.image_to_string(img, config='--psm 11')\n```\n\nCommon PSM values:\n- `0`: Orientation and script detection (OSD) only\n- `3`: Fully automatic page segmentation (default)\n- `4`: Single column of text of variable sizes\n- `6`: Uniform block of text\n- `7`: Single text line\n- `11`: Sparse text\n- `13`: Raw line\n\n\n## Image Preprocessing\n\nFor better OCR accuracy, preprocess images:\n\n```python\nfrom PIL import Image, ImageFilter, ImageOps\n\ndef preprocess_image(image_path):\n    \"\"\"Preprocess image for better OCR results.\"\"\"\n    img = Image.open(image_path)\n\n    # Convert to grayscale\n    img = img.convert('L')\n\n    # Increase contrast\n    img = ImageOps.autocontrast(img)\n\n    # Apply slight sharpening\n    img = img.filter(ImageFilter.SHARPEN)\n\n    return img\n\n# Use preprocessed image for OCR\nimg = preprocess_image(\"document.jpg\")\ntext = pytesseract.image_to_string(img)\n```\n\n### Advanced Preprocessing Strategies\n\nFor difficult images (low contrast, faded text, dark backgrounds), try multiple preprocessing approaches:\n\n1. **Grayscale + Autocontrast** - Basic enhancement for most images\n2. **Inverted** - Use `ImageOps.invert()` for dark backgrounds with light text\n3. **Scaling** - Upscale small images (e.g., 2x) before OCR to improve character recognition\n4. **Thresholding** - Convert to binary using `img.point(lambda p: 255 if p > threshold else 0)` with different threshold values (e.g., 100, 128)\n5. **Sharpening** - Apply `ImageFilter.SHARPEN` to improve edge clarity\n\n\n## Multi-Pass OCR Strategy\n\nFor challenging images, a single OCR pass may miss text. Use multiple passes with different configurations:\n\n1. **Try multiple PSM modes** - Different page segmentation modes work better for different layouts (e.g., `--psm 6` for blocks, `--psm 4` for columns, `--psm 11` for sparse text)\n\n2. **Try multiple preprocessing variants** - Run OCR on several preprocessed versions of the same image\n\n3. **Combine results** - Aggregate text from all passes to maximize extraction coverage\n\n```python\ndef multi_pass_ocr(image_path):\n    \"\"\"Run OCR with multiple strategies and combine results.\"\"\"\n    img = Image.open(image_path)\n    gray = ImageOps.grayscale(img)\n\n    # Generate preprocessing variants\n    variants = [\n        ImageOps.autocontrast(gray),\n        ImageOps.invert(ImageOps.autocontrast(gray)),\n        gray.filter(ImageFilter.SHARPEN),\n    ]\n\n    # PSM modes to try\n    psm_modes = ['--psm 6', '--psm 4', '--psm 11']\n\n    all_text = []\n    for variant in variants:\n        for psm in psm_modes:\n            try:\n                text = pytesseract.image_to_string(variant, config=psm)\n                if text.strip():\n                    all_text.append(text)\n            except Exception:\n                pass\n\n    # Combine all extracted text\n    return \"\\n\".join(all_text)\n```\n\nThis approach improves extraction for receipts, faded documents, and images with varying quality.\n\n\n## Error Handling\n\n### Common Issues and Solutions\n\n**Issue**: Tesseract not found\n\n```python\n# Verify Tesseract is installed\ntry:\n    pytesseract.get_tesseract_version()\nexcept pytesseract.TesseractNotFoundError:\n    print(\"Tesseract is not installed or not in PATH\")\n```\n\n**Issue**: Poor OCR quality\n\n- Preprocess image (grayscale, contrast, sharpen)\n- Use appropriate PSM mode for the document type\n- Ensure image resolution is sufficient (300+ DPI)\n\n**Issue**: Empty or garbage output\n\n- Check if image contains actual text\n- Try different PSM modes\n- Verify image is not corrupted\n\n\n## Quality Self-Check\n\nBefore returning results, verify:\n\n- [ ] Output is valid JSON (use `json.loads()` to validate)\n- [ ] All required fields are present (`success`, `filename`, `extracted_text`, `confidence`, `metadata`)\n- [ ] Text preserves logical reading order\n- [ ] Confidence level reflects actual OCR quality\n- [ ] Warnings array includes all detected issues\n- [ ] Special characters are properly escaped in JSON\n\n\n## Limitations\n\n- Tesseract works best with printed text; handwriting recognition is limited\n- Accuracy decreases with decorative fonts, artistic text, or extreme stylization\n- Mathematical equations and special notation may not extract accurately\n- Redacted or watermarked text cannot be recovered\n- Severe image degradation (blur, noise, low resolution) reduces accuracy\n- Complex multi-column layouts may require custom PSM configuration\n\n\n## Version History\n\n- **1.0.0** (2026-01-13): Initial release with Tesseract\u002Fpytesseract OCR\n","# Image OCR Skill\n\n## Purpose\nThis skill enables accurate text extraction from image files (JPG, PNG, etc.) using Tesseract OCR via the `pytesseract` Python library. It is suitable for scanned documents, screenshots, photos of text, receipts, forms, and other visual content containing text.\n\n## When to Use\n- Extracting text from scanned documents or photos\n- Reading text from screenshots or image captures\n- Processing batch image files that contain textual information\n- Converting visual documents to machine-readable text\n- Extracting structured data from forms, receipts, or tables in images\n\n\n\n## Receipt Field Extraction (Date + Total)\n\nThis section is a domain-specific recipe for extracting `date` (ISO `YYYY-MM-DD`) and `total_amount` (two-decimal string) from scanned receipt images (Malaysian\u002FSEA style). Use this recipe when the task asks for receipt totals and dates; the generic OCR sections below are inadequate on their own.\n\n### Priority Tiers and Exclusions\n\n```python\nTIERS = [\n    ['GRAND TOTAL'],\n    ['TOTAL RM', 'TOTAL: RM'],\n    ['TOTAL AMOUNT'],\n    ['TOTAL', 'AMOUNT', 'TOTAL DUE', 'AMOUNT DUE', 'BALANCE DUE',\n     'NETT TOTAL', 'NET TOTAL', 'ROUNDED TOTAL', 'TOTAL ROUNDED',\n     'TOTAL AFTER ROUNDING', 'TOTAL INCL GST', 'TOTAL RM INCLUDING GST'],\n]\n\nEXCLUSIONS = [\n    'SUBTOTAL', 'SUB TOTAL', 'SUB-TOTAL',\n    'TAX', 'GST', 'SST',\n    'DISCOUNT', 'CHANGE',\n    'CASH TENDERED', 'CASH RECEIVED', 'CASH PAYMENT',\n    'VISA', 'MASTERCARD', 'DEBIT', 'CREDIT', 'CARD', 'PAYMENT',\n    'ROUNDING ADJ', 'ROUNDING ADJUSTMENT',\n    'TOTAL GST', 'TOTAL TAX', 'TOTAL SST', 'GST TOTAL', 'TAX TOTAL',\n    'TOTAL ITEMS', 'TOTAL ITEM(S)', 'ITEM COUNT', 'TOTAL QTY',\n    'TOTAL QUANTITY', 'TOTAL SAVINGS',\n    'EXCLUDING', 'EXCL GST', 'AMT EXCL',\n    'DESCRIPTION', 'QTY PRICE AMOUNT', 'PRICE AMOUNT',\n    'AVERAGE', 'PAX',\n]\n```\n\nIMPORTANT nuances:\n- Distinguish `TOTAL GST` \u002F `GST TOTAL` (EXCLUDE) from `TOTAL INCL GST` \u002F `TOTAL (INCLUSIVE OF GST)` (INCLUDE). Check INCLUDE patterns before applying EXCLUSIONS.\n- Once any line contains `GST SUMMARY`, `TAX SUMMARY`, or `TAX CODE`, mark all subsequent lines as excluded for total selection (summary tables often repeat a 'Total' row).\n\n### Amount Regex and Normalization\n\n```python\nimport re\nAMOUNT_RE = re.compile(r'(?\u003C![\\w.])(\\d{1,3}(?:,\\d{3})+\\.\\d{2}|\\d+\\.\\d{2})(?![\\w.\\d])')\n\ndef to_float(tok):\n    return float(tok.replace(',', ''))\n\ndef fmt_amount(v):\n    return f'{v:.2f}'\n\ndef normalize_numeric_tokens(line):\n    # Apply only inside tokens that look numeric-ish (contain a digit).\n    def fix_token(tok):\n        if not re.search(r'\\d', tok):\n            return tok\n        t = tok\n        t = t.replace('O', '0').replace('o', '0').replace('Q', '0').replace('u', '0')\n        t = t.replace('l', '1').replace('I', '1').replace('|', '1')\n        t = t.replace('S', '5').replace('B', '8')\n        t = t.replace(':', '.')\n        return t\n    line = re.sub(r'\\bRN\\b', 'RM', line)\n    # Collapse 'NN NN' at end of a keyword line into 'NN.NN'\n    line = re.sub(r'(\\d{1,4})\\s+(\\d{2})\\s*$', r'\\1.\\2', line)\n    return ' '.join(fix_token(t) for t in line.split(' '))\n```\n\n### Multi-Variant OCR\n\nGenerate at least four variants and collect text from each:\n1. Original, `--psm 6`\n2. Grayscale + autocontrast, `--psm 6`\n3. 2x LANCZOS upscale of #2, `--psm 6`\n4. 3x LANCZOS upscale of #2, `--psm 4`\n5. (optional) Binarized\u002Finverted for faded scans, `--psm 11`\n\n```python\nfrom PIL import Image, ImageOps\nimport pytesseract\n\ndef ocr_variants(image_path):\n    img = Image.open(image_path)\n    gray = ImageOps.autocontrast(ImageOps.grayscale(img))\n    w, h = gray.size\n    up2 = gray.resize((w*2, h*2), Image.LANCZOS)\n    up3 = gray.resize((w*3, h*3), Image.LANCZOS)\n    specs = [(img, '--psm 6'), (gray, '--psm 6'),\n             (up2, '--psm 6'), (up3, '--psm 4')]\n    texts = []\n    for im, cfg in specs:\n        try:\n            texts.append(pytesseract.image_to_string(im, config=cfg))\n        except Exception:\n            pass\n    return texts\n```\n\n### Selection Algorithm (Total)\n\nFor each OCR variant text, split into lines; for each line:\n1. Apply `normalize_numeric_tokens`.\n2. If a prior line matched `GST SUMMARY`\u002F`TAX SUMMARY`\u002F`TAX CODE`, treat this line as excluded.\n3. If the uppercased line contains any EXCLUSION keyword (and NOT an INCLUDE pattern like `TOTAL INCL GST` \u002F `INCLUSIVE OF GST`), skip.\n4. For each tier (lowest index wins), if the line contains a tier keyword, extract 2-decimal amounts via `AMOUNT_RE`.\n   - If amounts found: record `(tier_index, value, line_index, variant_index)` for the LAST amount on that line.\n   - If no amounts AND the line contains no digits at all: look ahead up to 2 non-empty, non-excluded lines; take the LAST 2-decimal number found. Record as candidate.\n   - If the keyword line has partial digits but no valid 2-decimal match: do NOT fall through (prevents Visa\u002FCash leakage).\n\nFinal pick across all variants:\n1. Choose the lowest `tier_index` that has any candidate.\n2. Within that tier, prefer values corroborated by (a) appearing on Subtotal\u002FPayment\u002FCash\u002FRounded lines on the same receipt, or (b) appearing in multiple variants.\n3. Tiebreak by cross-variant frequency, then by LAST line_index (so a final `TOTAL: 23.25` beats an earlier `Total (Inclusive of GST): 23.26` when a `Rounding Adj: -0.01` sits between them).\n4. Output `fmt_amount(value)`.\n\nWorked example:\n```\nTotal (Inclusive of GST):   23.26\nRounding Adj:               -0.01\nTOTAL:                      23.25\n```\nBoth `23.26` and `23.25` are tier-3 candidates. The later line wins → `total_amount = '23.25'`.\n\n### Date Extraction\n\n```python\nimport re\nfrom datetime import date\n\nDATE_PATTERNS = [\n    (re.compile(r'(\\d{1,2})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{4})'), 'dmy4'),\n    (re.compile(r'(\\d{4})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{1,2})'), 'ymd'),\n    (re.compile(r'(\\d{1,2})[\u002F\\-.](\\d{1,2})[\u002F\\-.](\\d{2})\\b'), 'dmy2'),\n    (re.compile(r'(\\d{1,2})\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\\s+(\\d{4})', re.I), 'dmony'),\n]\nMONTHS = {m:i+1 for i,m in enumerate(['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'])}\n\ndef _valid(y, mo, d):\n    try:\n        date(y, mo, d); return 2000 \u003C= y \u003C= 2099\n    except Exception:\n        return False\n\ndef _try_swaps(y, mo, d):\n    swaps = [('6','0'),('0','6'),('8','3'),('3','8'),('1','7'),('7','1'),('5','6'),('6','5')]\n    for field in ('mo','d'):\n        s = str(mo if field=='mo' else d)\n        for a,b in swaps:\n            if a in s:\n                cand = int(s.replace(a,b,1))\n                nm, nd = (cand, d) if field=='mo' else (mo, cand)\n                if _valid(y, nm, nd):\n                    return y, nm, nd\n    return None\n\ndef extract_date(text):\n    lines = text.splitlines()\n    # Prefer lines near Date\u002FTarikh\u002FINV labels first\n    scored = []\n    for i, ln in enumerate(lines):\n        boost = 1 if re.search(r'date|tarikh|inv', ln, re.I) else 0\n        scored.append((boost, i, ln))\n    scored.sort(key=lambda x: (-x[0], x[1]))\n    for _, _, ln in scored:\n        for rx, kind in DATE_PATTERNS:\n            m = rx.search(ln)\n            if not m: continue\n            if kind == 'dmy4':\n                d, mo, y = int(m.group(1)), int(m.group(2)), int(m.group(3))\n                if mo > 12 and d \u003C= 12:  # fallback to MM\u002FDD\n                    d, mo = mo, d\n            elif kind == 'ymd':\n                y, mo, d = int(m.group(1)), int(m.group(2)), int(m.group(3))\n            elif kind == 'dmy2':\n                d, mo, yy = int(m.group(1)), int(m.group(2)), int(m.group(3))\n                y = 2000 + yy if yy \u003C= 49 else 1900 + yy\n            elif kind == 'dmony':\n                d = int(m.group(1)); mo = MONTHS[m.group(2).lower()[:3]]; y = int(m.group(3))\n            if _valid(y, mo, d):\n                return f'{y:04d}-{mo:02d}-{d:02d}'\n            fix = _try_swaps(y, mo, d)\n            if fix:\n                y, mo, d = fix\n                return f'{y:04d}-{mo:02d}-{d:02d}'\n    return None\n```\n\nFor ambiguous `DD\u002FMM` vs `MM\u002FDD`, default to `DD\u002FMM\u002FYYYY` (Malaysian\u002FSEA convention). Example: `09\u002F03\u002F2018` → `2018-03-09` (NOT `2018-08-09`).\n\n### Reference Function and Batch Driver\n\n```python\ndef extract_receipt_fields(image_path):\n    variant_texts = ocr_variants(image_path)\n    # --- total selection ---\n    include_pat = re.compile(r'TOTAL\\s*(\\(?\\s*INCL(?:USIVE)?(?:\\s*OF)?\\s*GST|INCL\\s*GST)', re.I)\n    candidates = []  # (tier, value, line_idx, variant_idx, line_text)\n    corroborators = set()\n    for vi, raw in enumerate(variant_texts):\n        in_summary = False\n        lines = [normalize_numeric_tokens(l) for l in raw.splitlines()]\n        for li, line in enumerate(lines):\n            U = line.upper()\n            if any(k in U for k in ('GST SUMMARY','TAX SUMMARY','TAX CODE')):\n                in_summary = True\n            # Corroborators\n            if any(k in U for k in ('SUBTOTAL','SUB TOTAL','CASH','PAYMENT','ROUNDED','TENDERED','CHANGE')):\n                for m in AMOUNT_RE.finditer(line):\n                    corroborators.add(to_float(m.group(1)))\n            is_include = bool(include_pat.search(U))\n            is_excluded = in_summary or (not is_include and any(k in U for k in EXCLUSIONS))\n            if is_excluded:\n                continue\n            for ti, kws in enumerate(TIERS):\n                if any(k in U for k in kws):\n                    amounts = [to_float(m.group(1)) for m in AMOUNT_RE.finditer(line)]\n                    if amounts:\n                        candidates.append((ti, amounts[-1], li, vi, line))\n                    elif not re.search(r'\\d', line):\n                        # split-line fallback: look ahead up to 2 non-excluded lines\n                        for look in range(li+1, min(li+3, len(lines))):\n                            nxt = lines[look]\n                            if not nxt.strip(): continue\n                            NU = nxt.upper()\n                            if any(k in NU for k in EXCLUSIONS): continue\n                            nx = [to_float(m.group(1)) for m in AMOUNT_RE.finditer(nxt)]\n                            if nx:\n                                candidates.append((ti, nx[-1], look, vi, nxt))\n                                break\n                    break  # only match the strongest tier per line\n    total_str = None\n    if candidates:\n        best_tier = min(c[0] for c in candidates)\n        pool = [c for c in candidates if c[0] == best_tier]\n        from collections import Counter\n        freq = Counter(round(c[1], 2) for c in pool)\n        def score(c):\n            v = round(c[1], 2)\n            return (freq[v], 1 if v in {round(x,2) for x in corroborators} else 0, c[2])\n        pool.sort(key=score, reverse=True)\n        total_str = fmt_amount(pool[0][1])\n    # --- date ---\n    date_iso = None\n    for raw in variant_texts:\n        date_iso = extract_date(raw)\n        if date_iso: break\n    return {'date': date_iso, 'total_amount': total_str}\n\n# Batch driver → xlsx\ndef build_xlsx(img_dir, out_path):\n    import os, openpyxl\n    from pathlib import Path\n    wb = openpyxl.Workbook()\n    ws = wb.active\n    ws.title = 'results'\n    ws.append(['filename', 'date', 'total_amount'])\n    files = sorted(p.name for p in Path(img_dir).iterdir()\n                   if p.suffix.lower() in {'.jpg','.jpeg','.png','.webp'})\n    for name in files:\n        res = extract_receipt_fields(os.path.join(img_dir, name))\n        ws.append([name, res['date'], res['total_amount']])\n    wb.save(out_path)\n```\n\n### Output Contract\n- `total_amount`: two-decimal string (e.g., `'47.70'`), or `None` on failure.\n- `date`: ISO `YYYY-MM-DD` string, or `None` on failure.\n- xlsx contains exactly one sheet `results`, header row + sorted-by-filename data rows, no extra columns\u002Fsheets.\n\n## Required Libraries\n\nThe following Python libraries are required:\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\n```\n\n## Input Requirements\n- **File formats**: JPG, JPEG, PNG, WEBP\n- **Image quality**: Minimum 300 DPI recommended for printed text; clear and legible text\n- **File size**: Under 5MB per image (resize if necessary)\n- **Text language**: Specify if non-English to improve accuracy\n\n## Output Schema\nAll extracted content must be returned as valid JSON conforming to this schema:\n\n```json\n{\n  \"success\": true,\n  \"filename\": \"example.jpg\",\n  \"extracted_text\": \"Full raw text extracted from the image...\",\n  \"confidence\": \"high|medium|low\",\n  \"metadata\": {\n    \"language_detected\": \"en\",\n    \"text_regions\": 3,\n    \"has_tables\": false,\n    \"has_handwriting\": false\n  },\n  \"warnings\": [\n    \"Text partially obscured in bottom-right corner\",\n    \"Low contrast detected in header section\"\n  ]\n}\n```\n\n\n### Field Descriptions\n\n- `success`: Boolean indicating whether text extraction completed\n- `filename`: Original image filename\n- `extracted_text`: Complete text content in reading order (top-to-bottom, left-to-right)\n- `confidence`: Overall OCR confidence level based on image quality and text clarity\n- `metadata.language_detected`: ISO 639-1 language code\n- `metadata.text_regions`: Number of distinct text blocks identified\n- `metadata.has_tables`: Whether tabular data structures were detected\n- `metadata.has_handwriting`: Whether handwritten text was detected\n- `warnings`: Array of quality issues or potential errors\n\n\n## Code Examples\n\n### Basic OCR Extraction\n\n```python\nimport pytesseract\nfrom PIL import Image\n\ndef extract_text_from_image(image_path):\n    \"\"\"Extract text from a single image using Tesseract OCR.\"\"\"\n    img = Image.open(image_path)\n    text = pytesseract.image_to_string(img)\n    return text.strip()\n```\n\n### OCR with Confidence Data\n\n```python\nimport pytesseract\nfrom PIL import Image\n\ndef extract_with_confidence(image_path):\n    \"\"\"Extract text with per-word confidence scores.\"\"\"\n    img = Image.open(image_path)\n\n    # Get detailed OCR data including confidence\n    data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\n\n    words = []\n    confidences = []\n\n    for i, word in enumerate(data['text']):\n        if word.strip():  # Skip empty strings\n            words.append(word)\n            confidences.append(data['conf'][i])\n\n    # Calculate average confidence\n    avg_confidence = sum(c for c in confidences if c > 0) \u002F len([c for c in confidences if c > 0]) if confidences else 0\n\n    return {\n        'text': ' '.join(words),\n        'average_confidence': avg_confidence,\n        'word_count': len(words)\n    }\n```\n\n### Full OCR with JSON Output\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\n\ndef ocr_to_json(image_path):\n    \"\"\"Perform OCR and return results as JSON.\"\"\"\n    filename = os.path.basename(image_path)\n    warnings = []\n\n    try:\n        img = Image.open(image_path)\n\n        # Get detailed OCR data\n        data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\n\n        # Extract text preserving structure\n        text = pytesseract.image_to_string(img)\n\n        # Calculate confidence\n        confidences = [c for c in data['conf'] if c > 0]\n        avg_conf = sum(confidences) \u002F len(confidences) if confidences else 0\n\n        # Determine confidence level\n        if avg_conf >= 80:\n            confidence = \"high\"\n        elif avg_conf >= 50:\n            confidence = \"medium\"\n        else:\n            confidence = \"low\"\n            warnings.append(f\"Low OCR confidence: {avg_conf:.1f}%\")\n\n        # Count text regions (blocks)\n        block_nums = set(data['block_num'])\n        text_regions = len([b for b in block_nums if b > 0])\n\n        result = {\n            \"success\": True,\n            \"filename\": filename,\n            \"extracted_text\": text.strip(),\n            \"confidence\": confidence,\n            \"metadata\": {\n                \"language_detected\": \"en\",\n                \"text_regions\": text_regions,\n                \"has_tables\": False,\n                \"has_handwriting\": False\n            },\n            \"warnings\": warnings\n        }\n\n    except Exception as e:\n        result = {\n            \"success\": False,\n            \"filename\": filename,\n            \"extracted_text\": \"\",\n            \"confidence\": \"low\",\n            \"metadata\": {\n                \"language_detected\": \"unknown\",\n                \"text_regions\": 0,\n                \"has_tables\": False,\n                \"has_handwriting\": False\n            },\n            \"warnings\": [f\"OCR failed: {str(e)}\"]\n        }\n\n    return result\n\n# Usage\nresult = ocr_to_json(\"document.jpg\")\nprint(json.dumps(result, indent=2))\n```\n\n### Batch Processing Multiple Images\n\n```python\nimport pytesseract\nfrom PIL import Image\nimport json\nimport os\nfrom pathlib import Path\n\ndef process_image_directory(directory_path, output_file):\n    \"\"\"Process all images in a directory and save results.\"\"\"\n    image_extensions = {'.jpg', '.jpeg', '.png', '.webp'}\n    results = []\n\n    for file_path in sorted(Path(directory_path).iterdir()):\n        if file_path.suffix.lower() in image_extensions:\n            result = ocr_to_json(str(file_path))\n            results.append(result)\n            print(f\"Processed: {file_path.name}\")\n\n    # Save results\n    with open(output_file, 'w') as f:\n        json.dump(results, f, indent=2)\n\n    return results\n```\n\n\n## Tesseract Configuration Options\n\n### Language Selection\n\n```python\n# Specify language (default is English)\ntext = pytesseract.image_to_string(img, lang='eng')\n\n# Multiple languages\ntext = pytesseract.image_to_string(img, lang='eng+fra+deu')\n```\n\n### Page Segmentation Modes (PSM)\n\nUse `--psm` to control how Tesseract segments the image:\n\n```python\n# PSM 3: Fully automatic page segmentation (default)\ntext = pytesseract.image_to_string(img, config='--psm 3')\n\n# PSM 4: Assume single column of text\ntext = pytesseract.image_to_string(img, config='--psm 4')\n\n# PSM 6: Assume uniform block of text\ntext = pytesseract.image_to_string(img, config='--psm 6')\n\n# PSM 11: Sparse text - find as much text as possible\ntext = pytesseract.image_to_string(img, config='--psm 11')\n```\n\nCommon PSM values:\n- `0`: Orientation and script detection (OSD) only\n- `3`: Fully automatic page segmentation (default)\n- `4`: Single column of text of variable sizes\n- `6`: Uniform block of text\n- `7`: Single text line\n- `11`: Sparse text\n- `13`: Raw line\n\n\n## Image Preprocessing\n\nFor better OCR accuracy, preprocess images:\n\n```python\nfrom PIL import Image, ImageFilter, ImageOps\n\ndef preprocess_image(image_path):\n    \"\"\"Preprocess image for better OCR results.\"\"\"\n    img = Image.open(image_path)\n\n    # Convert to grayscale\n    img = img.convert('L')\n\n    # Increase contrast\n    img = ImageOps.autocontrast(img)\n\n    # Apply slight sharpening\n    img = img.filter(ImageFilter.SHARPEN)\n\n    return img\n\n# Use preprocessed image for OCR\nimg = preprocess_image(\"document.jpg\")\ntext = pytesseract.image_to_string(img)\n```\n\n### Advanced Preprocessing Strategies\n\nFor difficult images (low contrast, faded text, dark backgrounds), try multiple preprocessing approaches:\n\n1. **Grayscale + Autocontrast** - Basic enhancement for most images\n2. **Inverted** - Use `ImageOps.invert()` for dark backgrounds with light text\n3. **Scaling** - Upscale small images (e.g., 2x) before OCR to improve character recognition\n4. **Thresholding** - Convert to binary using `img.point(lambda p: 255 if p > threshold else 0)` with different threshold values (e.g., 100, 128)\n5. **Sharpening** - Apply `ImageFilter.SHARPEN` to improve edge clarity\n\n\n## Multi-Pass OCR Strategy\n\nFor challenging images, a single OCR pass may miss text. Use multiple passes with different configurations:\n\n1. **Try multiple PSM modes** - Different page segmentation modes work better for different layouts (e.g., `--psm 6` for blocks, `--psm 4` for columns, `--psm 11` for sparse text)\n\n2. **Try multiple preprocessing variants** - Run OCR on several preprocessed versions of the same image\n\n3. **Combine results** - Aggregate text from all passes to maximize extraction coverage\n\n```python\ndef multi_pass_ocr(image_path):\n    \"\"\"Run OCR with multiple strategies and combine results.\"\"\"\n    img = Image.open(image_path)\n    gray = ImageOps.grayscale(img)\n\n    # Generate preprocessing variants\n    variants = [\n        ImageOps.autocontrast(gray),\n        ImageOps.invert(ImageOps.autocontrast(gray)),\n        gray.filter(ImageFilter.SHARPEN),\n    ]\n\n    # PSM modes to try\n    psm_modes = ['--psm 6', '--psm 4', '--psm 11']\n\n    all_text = []\n    for variant in variants:\n        for psm in psm_modes:\n            try:\n                text = pytesseract.image_to_string(variant, config=psm)\n                if text.strip():\n                    all_text.append(text)\n            except Exception:\n                pass\n\n    # Combine all extracted text\n    return \"\\n\".join(all_text)\n```\n\nThis approach improves extraction for receipts, faded documents, and images with varying quality.\n\n\n## Error Handling\n\n### Common Issues and Solutions\n\n**Issue**: Tesseract not found\n\n```python\n# Verify Tesseract is installed\ntry:\n    pytesseract.get_tesseract_version()\nexcept pytesseract.TesseractNotFoundError:\n    print(\"Tesseract is not installed or not in PATH\")\n```\n\n**Issue**: Poor OCR quality\n\n- Preprocess image (grayscale, contrast, sharpen)\n- Use appropriate PSM mode for the document type\n- Ensure image resolution is sufficient (300+ DPI)\n\n**Issue**: Empty or garbage output\n\n- Check if image contains actual text\n- Try different PSM modes\n- Verify image is not corrupted\n\n\n## Quality Self-Check\n\nBefore returning results, verify:\n\n- [ ] Output is valid JSON (use `json.loads()` to validate)\n- [ ] All required fields are present (`success`, `filename`, `extracted_text`, `confidence`, `metadata`)\n- [ ] Text preserves logical reading order\n- [ ] Confidence level reflects actual OCR quality\n- [ ] Warnings array includes all detected issues\n- [ ] Special characters are properly escaped in JSON\n\n\n## Limitations\n\n- Tesseract works best with printed text; handwriting recognition is limited\n- Accuracy decreases with decorative fonts, artistic text, or extreme stylization\n- Mathematical equations and special notation may not extract accurately\n- Redacted or watermarked text cannot be recovered\n- Severe image degradation (blur, noise, low resolution) reduces accuracy\n- Complex multi-column layouts may require custom PSM configuration\n\n\n## Version History\n\n- **1.0.0** (2026-01-13): Initial release with Tesseract\u002Fpytesseract OCR\n",{"name":5,"description":7},"main","cdcb6d230f3b15c3c33f8a2bdd16439f43602cbe","jpg-ocr-stat\u002Fresult\u002Fskillopt__opus47-tatu-r2-image-aware\u002Fevidence\u002Ffinal_runs\u002Frun_03\u002Finputs\u002Fskills\u002Fimage-ocr",1,"https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark","https:\u002F\u002Fgithub.com\u002Flinyouxue","2026-11-14 20:28:35",{"source":8,"skillName":5,"commands":32,"prompt":42,"zipAvailable":43},[33,36,39],{"tool":34,"command":35},"npx","npx skills add linyouxue\u002Fskill-repair-benchmark --skill image-ocr",{"tool":37,"command":38},"bunx","bunx skills add linyouxue\u002Fskill-repair-benchmark --skill image-ocr",{"tool":40,"command":41},"pnpm","pnpm dlx skills add linyouxue\u002Fskill-repair-benchmark --skill image-ocr","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskillopt__opus47-tatu-r2-image-aware\u002Fevidence\u002Ffinal_runs\u002Frun_03\u002Finputs\u002Fskills\u002Fimage-ocr\u002FSKILL.md",true,[],[46,53,60,66,71,76],{"skillNo":47,"name":48,"nameZh":6,"description":49,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":50,"categories":51,"keywords":52,"publishedAt":19},"01M7E1HGVRY73NBJ022CJG13X6","video-frame-extraction","Extract frames from video files and save them as images using OpenCV","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_03\u002Finputs\u002Fskills\u002Fvideo-frame-extraction\u002FSKILL.md",[],[],{"skillNo":54,"name":55,"nameZh":6,"description":56,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":57,"categories":58,"keywords":59,"publishedAt":19},"01M7E1HAG5R7CJ9C8NYJS771MB","openai-vision","Analyze images and multi-frame sequences using OpenAI GPT vision models","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_03\u002Finputs\u002Fskills\u002Fopenai-vision\u002FSKILL.md",[],[],{"skillNo":61,"name":5,"nameZh":6,"description":62,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":63,"categories":64,"keywords":65,"publishedAt":19},"01M7E1H7DADKB6QBWKC2KQ63SZ","Extract text content from images using Tesseract OCR via Python, including structured field extraction from receipts and forms","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_03\u002Finputs\u002Fskills\u002Fimage-ocr\u002FSKILL.md",[],[],{"skillNo":67,"name":48,"nameZh":6,"description":49,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":68,"categories":69,"keywords":70,"publishedAt":19},"01M7E1H0WBNT43TENGQY7AVZHD","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_02\u002Finputs\u002Fskills\u002Fvideo-frame-extraction\u002FSKILL.md",[],[],{"skillNo":72,"name":55,"nameZh":6,"description":56,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":73,"categories":74,"keywords":75,"publishedAt":19},"01M7E1GTC7D4N4Q2EF03875J9E","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_02\u002Finputs\u002Fskills\u002Fopenai-vision\u002FSKILL.md",[],[],{"skillNo":77,"name":5,"nameZh":6,"description":62,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":78,"categories":79,"keywords":80,"publishedAt":19},"01M7E1GQ92CWFMGH53KSSE4WER","https:\u002F\u002Fgithub.com\u002Flinyouxue\u002Fskill-repair-benchmark\u002Fblob\u002Fmain\u002Fjpg-ocr-stat\u002Fresult\u002Fskilltta-claude4.7\u002Fgroup_03\u002Frun_02\u002Finputs\u002Fskills\u002Fimage-ocr\u002FSKILL.md",[],[]]