[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7C7S4SSMW7RVJTA0NGAD66F":3,"skill-same:01M7C7S4SSMW7RVJTA0NGAD66F":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},"01M7C7S4SSMW7RVJTA0NGAD66F","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\u002Fskillhone__paratera-opus47-diagnosis-s0-20261031\u002Fskills\u002Fimage-ocr\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\r\nname: image-ocr\r\ndescription: Extract text content from images using Tesseract OCR via Python\r\n---\r\n\r\n# Image OCR Skill\r\n\r\n## Purpose\r\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.\r\n\r\n## When to Use\r\n- Extracting text from scanned documents or photos\r\n- Reading text from screenshots or image captures\r\n- Processing batch image files that contain textual information\r\n- Converting visual documents to machine-readable text\r\n- Extracting structured data from forms, receipts, or tables in images\r\n\r\n## Required Libraries\r\n\r\nThe following Python libraries are required:\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\n```\r\n\r\n## Input Requirements\r\n- **File formats**: JPG, JPEG, PNG, WEBP\r\n- **Image quality**: Minimum 300 DPI recommended for printed text; clear and legible text\r\n- **File size**: Under 5MB per image (resize if necessary)\r\n- **Text language**: Specify if non-English to improve accuracy\r\n\r\n## Output Schema\r\nAll extracted content must be returned as valid JSON conforming to this schema:\r\n\r\n```json\r\n{\r\n  \"success\": true,\r\n  \"filename\": \"example.jpg\",\r\n  \"extracted_text\": \"Full raw text extracted from the image...\",\r\n  \"confidence\": \"high|medium|low\",\r\n  \"metadata\": {\r\n    \"language_detected\": \"en\",\r\n    \"text_regions\": 3,\r\n    \"has_tables\": false,\r\n    \"has_handwriting\": false\r\n  },\r\n  \"warnings\": [\r\n    \"Text partially obscured in bottom-right corner\",\r\n    \"Low contrast detected in header section\"\r\n  ]\r\n}\r\n```\r\n\r\n\r\n### Field Descriptions\r\n\r\n- `success`: Boolean indicating whether text extraction completed\r\n- `filename`: Original image filename\r\n- `extracted_text`: Complete text content in reading order (top-to-bottom, left-to-right)\r\n- `confidence`: Overall OCR confidence level based on image quality and text clarity\r\n- `metadata.language_detected`: ISO 639-1 language code\r\n- `metadata.text_regions`: Number of distinct text blocks identified\r\n- `metadata.has_tables`: Whether tabular data structures were detected\r\n- `metadata.has_handwriting`: Whether handwritten text was detected\r\n- `warnings`: Array of quality issues or potential errors\r\n\r\n\r\n## Code Examples\r\n\r\n### Basic OCR Extraction\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\n\r\ndef extract_text_from_image(image_path):\r\n    \"\"\"Extract text from a single image using Tesseract OCR.\"\"\"\r\n    img = Image.open(image_path)\r\n    text = pytesseract.image_to_string(img)\r\n    return text.strip()\r\n```\r\n\r\n### OCR with Confidence Data\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\n\r\ndef extract_with_confidence(image_path):\r\n    \"\"\"Extract text with per-word confidence scores.\"\"\"\r\n    img = Image.open(image_path)\r\n\r\n    # Get detailed OCR data including confidence\r\n    data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\r\n\r\n    words = []\r\n    confidences = []\r\n\r\n    for i, word in enumerate(data['text']):\r\n        if word.strip():  # Skip empty strings\r\n            words.append(word)\r\n            confidences.append(data['conf'][i])\r\n\r\n    # Calculate average confidence\r\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\r\n\r\n    return {\r\n        'text': ' '.join(words),\r\n        'average_confidence': avg_confidence,\r\n        'word_count': len(words)\r\n    }\r\n```\r\n\r\n### Full OCR with JSON Output\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\n\r\ndef ocr_to_json(image_path):\r\n    \"\"\"Perform OCR and return results as JSON.\"\"\"\r\n    filename = os.path.basename(image_path)\r\n    warnings = []\r\n\r\n    try:\r\n        img = Image.open(image_path)\r\n\r\n        # Get detailed OCR data\r\n        data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\r\n\r\n        # Extract text preserving structure\r\n        text = pytesseract.image_to_string(img)\r\n\r\n        # Calculate confidence\r\n        confidences = [c for c in data['conf'] if c > 0]\r\n        avg_conf = sum(confidences) \u002F len(confidences) if confidences else 0\r\n\r\n        # Determine confidence level\r\n        if avg_conf >= 80:\r\n            confidence = \"high\"\r\n        elif avg_conf >= 50:\r\n            confidence = \"medium\"\r\n        else:\r\n            confidence = \"low\"\r\n            warnings.append(f\"Low OCR confidence: {avg_conf:.1f}%\")\r\n\r\n        # Count text regions (blocks)\r\n        block_nums = set(data['block_num'])\r\n        text_regions = len([b for b in block_nums if b > 0])\r\n\r\n        result = {\r\n            \"success\": True,\r\n            \"filename\": filename,\r\n            \"extracted_text\": text.strip(),\r\n            \"confidence\": confidence,\r\n            \"metadata\": {\r\n                \"language_detected\": \"en\",\r\n                \"text_regions\": text_regions,\r\n                \"has_tables\": False,\r\n                \"has_handwriting\": False\r\n            },\r\n            \"warnings\": warnings\r\n        }\r\n\r\n    except Exception as e:\r\n        result = {\r\n            \"success\": False,\r\n            \"filename\": filename,\r\n            \"extracted_text\": \"\",\r\n            \"confidence\": \"low\",\r\n            \"metadata\": {\r\n                \"language_detected\": \"unknown\",\r\n                \"text_regions\": 0,\r\n                \"has_tables\": False,\r\n                \"has_handwriting\": False\r\n            },\r\n            \"warnings\": [f\"OCR failed: {str(e)}\"]\r\n        }\r\n\r\n    return result\r\n\r\n# Usage\r\nresult = ocr_to_json(\"document.jpg\")\r\nprint(json.dumps(result, indent=2))\r\n```\r\n\r\n### Batch Processing Multiple Images\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\nfrom pathlib import Path\r\n\r\ndef process_image_directory(directory_path, output_file):\r\n    \"\"\"Process all images in a directory and save results.\"\"\"\r\n    image_extensions = {'.jpg', '.jpeg', '.png', '.webp'}\r\n    results = []\r\n\r\n    for file_path in sorted(Path(directory_path).iterdir()):\r\n        if file_path.suffix.lower() in image_extensions:\r\n            result = ocr_to_json(str(file_path))\r\n            results.append(result)\r\n            print(f\"Processed: {file_path.name}\")\r\n\r\n    # Save results\r\n    with open(output_file, 'w') as f:\r\n        json.dump(results, f, indent=2)\r\n\r\n    return results\r\n```\r\n\r\n\r\n## Tesseract Configuration Options\r\n\r\n### Language Selection\r\n\r\n```python\r\n# Specify language (default is English)\r\ntext = pytesseract.image_to_string(img, lang='eng')\r\n\r\n# Multiple languages\r\ntext = pytesseract.image_to_string(img, lang='eng+fra+deu')\r\n```\r\n\r\n### Page Segmentation Modes (PSM)\r\n\r\nUse `--psm` to control how Tesseract segments the image:\r\n\r\n```python\r\n# PSM 3: Fully automatic page segmentation (default)\r\ntext = pytesseract.image_to_string(img, config='--psm 3')\r\n\r\n# PSM 4: Assume single column of text\r\ntext = pytesseract.image_to_string(img, config='--psm 4')\r\n\r\n# PSM 6: Assume uniform block of text\r\ntext = pytesseract.image_to_string(img, config='--psm 6')\r\n\r\n# PSM 11: Sparse text - find as much text as possible\r\ntext = pytesseract.image_to_string(img, config='--psm 11')\r\n```\r\n\r\nCommon PSM values:\r\n- `0`: Orientation and script detection (OSD) only\r\n- `3`: Fully automatic page segmentation (default)\r\n- `4`: Single column of text of variable sizes\r\n- `6`: Uniform block of text\r\n- `7`: Single text line\r\n- `11`: Sparse text\r\n- `13`: Raw line\r\n\r\n\r\n## Image Preprocessing\r\n\r\nFor better OCR accuracy, preprocess images:\r\n\r\n```python\r\nfrom PIL import Image, ImageFilter, ImageOps\r\n\r\ndef preprocess_image(image_path):\r\n    \"\"\"Preprocess image for better OCR results.\"\"\"\r\n    img = Image.open(image_path)\r\n\r\n    # Convert to grayscale\r\n    img = img.convert('L')\r\n\r\n    # Increase contrast\r\n    img = ImageOps.autocontrast(img)\r\n\r\n    # Apply slight sharpening\r\n    img = img.filter(ImageFilter.SHARPEN)\r\n\r\n    return img\r\n\r\n# Use preprocessed image for OCR\r\nimg = preprocess_image(\"document.jpg\")\r\ntext = pytesseract.image_to_string(img)\r\n```\r\n\r\n### Advanced Preprocessing Strategies\r\n\r\nFor difficult images (low contrast, faded text, dark backgrounds), try multiple preprocessing approaches:\r\n\r\n1. **Grayscale + Autocontrast** - Basic enhancement for most images\r\n2. **Inverted** - Use `ImageOps.invert()` for dark backgrounds with light text\r\n3. **Scaling** - Upscale small images (e.g., 2x) before OCR to improve character recognition\r\n4. **Thresholding** - Convert to binary using `img.point(lambda p: 255 if p > threshold else 0)` with different threshold values (e.g., 100, 128)\r\n5. **Sharpening** - Apply `ImageFilter.SHARPEN` to improve edge clarity\r\n\r\n\r\n## Multi-Pass OCR Strategy\r\n\r\nFor challenging images, a single OCR pass may miss text. Use multiple passes with different configurations:\r\n\r\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)\r\n\r\n2. **Try multiple preprocessing variants** - Run OCR on several preprocessed versions of the same image\r\n\r\n3. **Combine results** - Aggregate text from all passes to maximize extraction coverage\r\n\r\n```python\r\ndef multi_pass_ocr(image_path):\r\n    \"\"\"Run OCR with multiple strategies and combine results.\"\"\"\r\n    img = Image.open(image_path)\r\n    gray = ImageOps.grayscale(img)\r\n\r\n    # Generate preprocessing variants\r\n    variants = [\r\n        ImageOps.autocontrast(gray),\r\n        ImageOps.invert(ImageOps.autocontrast(gray)),\r\n        gray.filter(ImageFilter.SHARPEN),\r\n    ]\r\n\r\n    # PSM modes to try\r\n    psm_modes = ['--psm 6', '--psm 4', '--psm 11']\r\n\r\n    all_text = []\r\n    for variant in variants:\r\n        for psm in psm_modes:\r\n            try:\r\n                text = pytesseract.image_to_string(variant, config=psm)\r\n                if text.strip():\r\n                    all_text.append(text)\r\n            except Exception:\r\n                pass\r\n\r\n    # Combine all extracted text\r\n    return \"\\n\".join(all_text)\r\n```\r\n\r\nThis approach improves extraction for receipts, faded documents, and images with varying quality.\r\n\r\n\r\n## Receipt Extraction Patterns\r\n\r\nPrinted receipts commonly need two canonical fields extracted: a date in ISO form (`YYYY-MM-DD`) and a monetary total as a string with exactly two decimals (e.g. `\"47.70\"`). The patterns below apply broadly whenever a scanned receipt is the input.\r\n\r\n### Robust OCR pass for receipts\r\n\r\nReceipts are usually a single narrow column of monospaced or dot-matrix text. Try `--psm 6` (uniform block) and `--psm 4` (single column) first, then widen to `--psm 11` (sparse) only as a fallback. Upscale small images (e.g. 2x–3x via `Image.resize(size, Image.Resampling.LANCZOS)`) **before** OCR; small print and faded thermal paper recognize dramatically better at higher resolution. Union the text of several (preprocessing × PSM) passes before pattern matching — a single pass frequently loses the TOTAL line.\r\n\r\n### Date extraction\r\n\r\nReceipts rarely use ISO dates, and the day\u002Fmonth order depends on locale (Malaysian, Singaporean, UK, EU, Australian receipts are typically `DD\u002FMM\u002FYYYY`; US receipts are typically `MM\u002FDD\u002FYYYY`). Rules of thumb:\r\n\r\n- Search near the first `Date` \u002F `Tarikh` \u002F `Dated` label. Accept the common forms `DD\u002FMM\u002FYYYY`, `DD-MM-YYYY`, `D MMM YYYY`, `YYYY-MM-DD`, and two-digit years `DD\u002FMM\u002FYY` (expand `YY` to `20YY` unless clearly historical).\r\n- When both halves of a `NN\u002FNN\u002FYYYY` token could be either day or month, let the receipt locale decide. If the address block names Malaysia, Kuala Lumpur, Selangor, Johor, Penang, Singapore, UK or EU, treat the **first** number as the day; otherwise fall back on an unambiguous signal (any component `>12` must be the day).\r\n- Before emitting, verify month ∈ 1..12 and day ∈ 1..31 and parse through `datetime.date(...)`; if OCR produced an impossible pair like `01\u002F13\u002F2018`, swap day\u002Fmonth and retry.\r\n- Always output `YYYY-MM-DD`.\r\n\r\n### Total amount: keyword hierarchy\r\n\r\nApply the keyword hierarchy strictly, most specific first, and skip exclusion keywords **on the same line**:\r\n\r\n1. `GRAND TOTAL`\r\n2. `TOTAL RM`, `TOTAL: RM`\r\n3. `TOTAL AMOUNT`\r\n4. `TOTAL`, `AMOUNT`, `TOTAL DUE`, `AMOUNT DUE`, `BALANCE DUE`, `NETT TOTAL`, `NET TOTAL`\r\n\r\nSkip any candidate line whose keyword is `SUBTOTAL`, `SUB TOTAL`, `TAX`, `GST`, `SST`, `DISCOUNT`, `CHANGE`, or `CASH TENDERED`. Pre-tax totals, change due, cash tendered, and standalone rounding-adjustment lines must never win.\r\n\r\n### Prefer the final rounded total\r\n\r\nMany Malaysian and SE-Asian receipts print **both** a pre-rounding \"inclusive of GST\" total and a final rounded total that is the actual amount paid:\r\n\r\n```\r\nTotal (Inclusive of GST) : 47.70\r\nRounding Adj             : -0.02\r\nTOTAL                    : 47.68\r\n```\r\n\r\nWhen more than one candidate survives the exclusion filter, choose the one that appears **after** any `Rounding Adj`, `Total Rounded`, `Rounded`, or final bold `TOTAL` line. The final rounded value is the paid amount; the earlier \"Inclusive of GST\" number is pre-rounding and will fail strict equality. Equivalently: among surviving matches, prefer a line whose keyword is exactly `TOTAL` and that appears after a rounding line, over an earlier `Total (Inclusive of GST)`.\r\n\r\n### Numeric parsing\r\n\r\n- Accept comma thousands separators (`1,234.56`) and strip them before formatting.\r\n- Within the extracted number only, repair common OCR confusions: `O\u002Fo → 0`, `l\u002FI → 1`, `S → 5`, `B → 8` when flanked by digits. Never apply these substitutions to the keyword text — doing so turns `GST` into `G5T` and misroutes the match.\r\n- If the keyword and its value are on different lines, take the **last** number on the next non-empty line (the amount column), not the first (which is usually quantity or unit price).\r\n- Final emission must be a string matching `^\\d+\\.\\d{2}$`; produce it with `f\"{value:.2f}\"` after parsing the receipt number as a `Decimal`.\r\n\r\n### Do not fabricate values\r\n\r\nEvery emitted date and amount must come from a token actually present in the combined OCR text (after the normalisations above). If no keyword candidate survives filtering, run one more preprocessing variant — invert for dark backgrounds, upscale 3x, or threshold at both ~110 and ~160 — before giving up. Only emit `null` when every pass still yields no plausible candidate; a null is explicitly preferable to a guess, but a guess that is not literally on the receipt (e.g. a 3-digit amount concatenated from unrelated digits on an obviously small-ticket receipt) is the worst outcome.\r\n\r\n### Batch sanity checks\r\n\r\nBefore writing results for the whole batch:\r\n\r\n- Cross-check each total against the line-item column: if the chosen amount is more than ~5x the sum of visible item amounts (or more than ~5x the plausible receipt-size range for that image size), re-OCR that single image with alternative preprocessing before accepting the value.\r\n- Flag any amount missing a decimal point (e.g. `\"2325\"` instead of `\"23.25\"`); such strings almost always indicate a stripped period that must be reinserted or the record rechecked.\r\n- Dates must land in a plausible calendar range (e.g. within the last ~15 years for retail receipts); a date like `2117-02-02` is almost certainly a misread `2017-02-02`.\r\n\r\n\r\n## Error Handling\r\n\r\n### Common Issues and Solutions\r\n\r\n**Issue**: Tesseract not found\r\n\r\n```python\r\n# Verify Tesseract is installed\r\ntry:\r\n    pytesseract.get_tesseract_version()\r\nexcept pytesseract.TesseractNotFoundError:\r\n    print(\"Tesseract is not installed or not in PATH\")\r\n```\r\n\r\n**Issue**: Poor OCR quality\r\n\r\n- Preprocess image (grayscale, contrast, sharpen)\r\n- Use appropriate PSM mode for the document type\r\n- Ensure image resolution is sufficient (300+ DPI)\r\n\r\n**Issue**: Empty or garbage output\r\n\r\n- Check if image contains actual text\r\n- Try different PSM modes\r\n- Verify image is not corrupted\r\n\r\n\r\n## Quality Self-Check\r\n\r\nBefore returning results, verify:\r\n\r\n- [ ] Output is valid JSON (use `json.loads()` to validate)\r\n- [ ] All required fields are present (`success`, `filename`, `extracted_text`, `confidence`, `metadata`)\r\n- [ ] Text preserves logical reading order\r\n- [ ] Confidence level reflects actual OCR quality\r\n- [ ] Warnings array includes all detected issues\r\n- [ ] Special characters are properly escaped in JSON\r\n\r\n\r\n## Limitations\r\n\r\n- Tesseract works best with printed text; handwriting recognition is limited\r\n- Accuracy decreases with decorative fonts, artistic text, or extreme stylization\r\n- Mathematical equations and special notation may not extract accurately\r\n- Redacted or watermarked text cannot be recovered\r\n- Severe image degradation (blur, noise, low resolution) reduces accuracy\r\n- Complex multi-column layouts may require custom PSM configuration\r\n\r\n\r\n## Version History\r\n\r\n- **1.0.0** (2026-01-13): Initial release with Tesseract\u002Fpytesseract OCR\r\n","# Image OCR Skill\r\n\r\n## Purpose\r\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.\r\n\r\n## When to Use\r\n- Extracting text from scanned documents or photos\r\n- Reading text from screenshots or image captures\r\n- Processing batch image files that contain textual information\r\n- Converting visual documents to machine-readable text\r\n- Extracting structured data from forms, receipts, or tables in images\r\n\r\n## Required Libraries\r\n\r\nThe following Python libraries are required:\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\n```\r\n\r\n## Input Requirements\r\n- **File formats**: JPG, JPEG, PNG, WEBP\r\n- **Image quality**: Minimum 300 DPI recommended for printed text; clear and legible text\r\n- **File size**: Under 5MB per image (resize if necessary)\r\n- **Text language**: Specify if non-English to improve accuracy\r\n\r\n## Output Schema\r\nAll extracted content must be returned as valid JSON conforming to this schema:\r\n\r\n```json\r\n{\r\n  \"success\": true,\r\n  \"filename\": \"example.jpg\",\r\n  \"extracted_text\": \"Full raw text extracted from the image...\",\r\n  \"confidence\": \"high|medium|low\",\r\n  \"metadata\": {\r\n    \"language_detected\": \"en\",\r\n    \"text_regions\": 3,\r\n    \"has_tables\": false,\r\n    \"has_handwriting\": false\r\n  },\r\n  \"warnings\": [\r\n    \"Text partially obscured in bottom-right corner\",\r\n    \"Low contrast detected in header section\"\r\n  ]\r\n}\r\n```\r\n\r\n\r\n### Field Descriptions\r\n\r\n- `success`: Boolean indicating whether text extraction completed\r\n- `filename`: Original image filename\r\n- `extracted_text`: Complete text content in reading order (top-to-bottom, left-to-right)\r\n- `confidence`: Overall OCR confidence level based on image quality and text clarity\r\n- `metadata.language_detected`: ISO 639-1 language code\r\n- `metadata.text_regions`: Number of distinct text blocks identified\r\n- `metadata.has_tables`: Whether tabular data structures were detected\r\n- `metadata.has_handwriting`: Whether handwritten text was detected\r\n- `warnings`: Array of quality issues or potential errors\r\n\r\n\r\n## Code Examples\r\n\r\n### Basic OCR Extraction\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\n\r\ndef extract_text_from_image(image_path):\r\n    \"\"\"Extract text from a single image using Tesseract OCR.\"\"\"\r\n    img = Image.open(image_path)\r\n    text = pytesseract.image_to_string(img)\r\n    return text.strip()\r\n```\r\n\r\n### OCR with Confidence Data\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\n\r\ndef extract_with_confidence(image_path):\r\n    \"\"\"Extract text with per-word confidence scores.\"\"\"\r\n    img = Image.open(image_path)\r\n\r\n    # Get detailed OCR data including confidence\r\n    data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\r\n\r\n    words = []\r\n    confidences = []\r\n\r\n    for i, word in enumerate(data['text']):\r\n        if word.strip():  # Skip empty strings\r\n            words.append(word)\r\n            confidences.append(data['conf'][i])\r\n\r\n    # Calculate average confidence\r\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\r\n\r\n    return {\r\n        'text': ' '.join(words),\r\n        'average_confidence': avg_confidence,\r\n        'word_count': len(words)\r\n    }\r\n```\r\n\r\n### Full OCR with JSON Output\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\n\r\ndef ocr_to_json(image_path):\r\n    \"\"\"Perform OCR and return results as JSON.\"\"\"\r\n    filename = os.path.basename(image_path)\r\n    warnings = []\r\n\r\n    try:\r\n        img = Image.open(image_path)\r\n\r\n        # Get detailed OCR data\r\n        data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)\r\n\r\n        # Extract text preserving structure\r\n        text = pytesseract.image_to_string(img)\r\n\r\n        # Calculate confidence\r\n        confidences = [c for c in data['conf'] if c > 0]\r\n        avg_conf = sum(confidences) \u002F len(confidences) if confidences else 0\r\n\r\n        # Determine confidence level\r\n        if avg_conf >= 80:\r\n            confidence = \"high\"\r\n        elif avg_conf >= 50:\r\n            confidence = \"medium\"\r\n        else:\r\n            confidence = \"low\"\r\n            warnings.append(f\"Low OCR confidence: {avg_conf:.1f}%\")\r\n\r\n        # Count text regions (blocks)\r\n        block_nums = set(data['block_num'])\r\n        text_regions = len([b for b in block_nums if b > 0])\r\n\r\n        result = {\r\n            \"success\": True,\r\n            \"filename\": filename,\r\n            \"extracted_text\": text.strip(),\r\n            \"confidence\": confidence,\r\n            \"metadata\": {\r\n                \"language_detected\": \"en\",\r\n                \"text_regions\": text_regions,\r\n                \"has_tables\": False,\r\n                \"has_handwriting\": False\r\n            },\r\n            \"warnings\": warnings\r\n        }\r\n\r\n    except Exception as e:\r\n        result = {\r\n            \"success\": False,\r\n            \"filename\": filename,\r\n            \"extracted_text\": \"\",\r\n            \"confidence\": \"low\",\r\n            \"metadata\": {\r\n                \"language_detected\": \"unknown\",\r\n                \"text_regions\": 0,\r\n                \"has_tables\": False,\r\n                \"has_handwriting\": False\r\n            },\r\n            \"warnings\": [f\"OCR failed: {str(e)}\"]\r\n        }\r\n\r\n    return result\r\n\r\n# Usage\r\nresult = ocr_to_json(\"document.jpg\")\r\nprint(json.dumps(result, indent=2))\r\n```\r\n\r\n### Batch Processing Multiple Images\r\n\r\n```python\r\nimport pytesseract\r\nfrom PIL import Image\r\nimport json\r\nimport os\r\nfrom pathlib import Path\r\n\r\ndef process_image_directory(directory_path, output_file):\r\n    \"\"\"Process all images in a directory and save results.\"\"\"\r\n    image_extensions = {'.jpg', '.jpeg', '.png', '.webp'}\r\n    results = []\r\n\r\n    for file_path in sorted(Path(directory_path).iterdir()):\r\n        if file_path.suffix.lower() in image_extensions:\r\n            result = ocr_to_json(str(file_path))\r\n            results.append(result)\r\n            print(f\"Processed: {file_path.name}\")\r\n\r\n    # Save results\r\n    with open(output_file, 'w') as f:\r\n        json.dump(results, f, indent=2)\r\n\r\n    return results\r\n```\r\n\r\n\r\n## Tesseract Configuration Options\r\n\r\n### Language Selection\r\n\r\n```python\r\n# Specify language (default is English)\r\ntext = pytesseract.image_to_string(img, lang='eng')\r\n\r\n# Multiple languages\r\ntext = pytesseract.image_to_string(img, lang='eng+fra+deu')\r\n```\r\n\r\n### Page Segmentation Modes (PSM)\r\n\r\nUse `--psm` to control how Tesseract segments the image:\r\n\r\n```python\r\n# PSM 3: Fully automatic page segmentation (default)\r\ntext = pytesseract.image_to_string(img, config='--psm 3')\r\n\r\n# PSM 4: Assume single column of text\r\ntext = pytesseract.image_to_string(img, config='--psm 4')\r\n\r\n# PSM 6: Assume uniform block of text\r\ntext = pytesseract.image_to_string(img, config='--psm 6')\r\n\r\n# PSM 11: Sparse text - find as much text as possible\r\ntext = pytesseract.image_to_string(img, config='--psm 11')\r\n```\r\n\r\nCommon PSM values:\r\n- `0`: Orientation and script detection (OSD) only\r\n- `3`: Fully automatic page segmentation (default)\r\n- `4`: Single column of text of variable sizes\r\n- `6`: Uniform block of text\r\n- `7`: Single text line\r\n- `11`: Sparse text\r\n- `13`: Raw line\r\n\r\n\r\n## Image Preprocessing\r\n\r\nFor better OCR accuracy, preprocess images:\r\n\r\n```python\r\nfrom PIL import Image, ImageFilter, ImageOps\r\n\r\ndef preprocess_image(image_path):\r\n    \"\"\"Preprocess image for better OCR results.\"\"\"\r\n    img = Image.open(image_path)\r\n\r\n    # Convert to grayscale\r\n    img = img.convert('L')\r\n\r\n    # Increase contrast\r\n    img = ImageOps.autocontrast(img)\r\n\r\n    # Apply slight sharpening\r\n    img = img.filter(ImageFilter.SHARPEN)\r\n\r\n    return img\r\n\r\n# Use preprocessed image for OCR\r\nimg = preprocess_image(\"document.jpg\")\r\ntext = pytesseract.image_to_string(img)\r\n```\r\n\r\n### Advanced Preprocessing Strategies\r\n\r\nFor difficult images (low contrast, faded text, dark backgrounds), try multiple preprocessing approaches:\r\n\r\n1. **Grayscale + Autocontrast** - Basic enhancement for most images\r\n2. **Inverted** - Use `ImageOps.invert()` for dark backgrounds with light text\r\n3. **Scaling** - Upscale small images (e.g., 2x) before OCR to improve character recognition\r\n4. **Thresholding** - Convert to binary using `img.point(lambda p: 255 if p > threshold else 0)` with different threshold values (e.g., 100, 128)\r\n5. **Sharpening** - Apply `ImageFilter.SHARPEN` to improve edge clarity\r\n\r\n\r\n## Multi-Pass OCR Strategy\r\n\r\nFor challenging images, a single OCR pass may miss text. Use multiple passes with different configurations:\r\n\r\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)\r\n\r\n2. **Try multiple preprocessing variants** - Run OCR on several preprocessed versions of the same image\r\n\r\n3. **Combine results** - Aggregate text from all passes to maximize extraction coverage\r\n\r\n```python\r\ndef multi_pass_ocr(image_path):\r\n    \"\"\"Run OCR with multiple strategies and combine results.\"\"\"\r\n    img = Image.open(image_path)\r\n    gray = ImageOps.grayscale(img)\r\n\r\n    # Generate preprocessing variants\r\n    variants = [\r\n        ImageOps.autocontrast(gray),\r\n        ImageOps.invert(ImageOps.autocontrast(gray)),\r\n        gray.filter(ImageFilter.SHARPEN),\r\n    ]\r\n\r\n    # PSM modes to try\r\n    psm_modes = ['--psm 6', '--psm 4', '--psm 11']\r\n\r\n    all_text = []\r\n    for variant in variants:\r\n        for psm in psm_modes:\r\n            try:\r\n                text = pytesseract.image_to_string(variant, config=psm)\r\n                if text.strip():\r\n                    all_text.append(text)\r\n            except Exception:\r\n                pass\r\n\r\n    # Combine all extracted text\r\n    return \"\\n\".join(all_text)\r\n```\r\n\r\nThis approach improves extraction for receipts, faded documents, and images with varying quality.\r\n\r\n\r\n## Receipt Extraction Patterns\r\n\r\nPrinted receipts commonly need two canonical fields extracted: a date in ISO form (`YYYY-MM-DD`) and a monetary total as a string with exactly two decimals (e.g. `\"47.70\"`). The patterns below apply broadly whenever a scanned receipt is the input.\r\n\r\n### Robust OCR pass for receipts\r\n\r\nReceipts are usually a single narrow column of monospaced or dot-matrix text. Try `--psm 6` (uniform block) and `--psm 4` (single column) first, then widen to `--psm 11` (sparse) only as a fallback. Upscale small images (e.g. 2x–3x via `Image.resize(size, Image.Resampling.LANCZOS)`) **before** OCR; small print and faded thermal paper recognize dramatically better at higher resolution. Union the text of several (preprocessing × PSM) passes before pattern matching — a single pass frequently loses the TOTAL line.\r\n\r\n### Date extraction\r\n\r\nReceipts rarely use ISO dates, and the day\u002Fmonth order depends on locale (Malaysian, Singaporean, UK, EU, Australian receipts are typically `DD\u002FMM\u002FYYYY`; US receipts are typically `MM\u002FDD\u002FYYYY`). Rules of thumb:\r\n\r\n- Search near the first `Date` \u002F `Tarikh` \u002F `Dated` label. Accept the common forms `DD\u002FMM\u002FYYYY`, `DD-MM-YYYY`, `D MMM YYYY`, `YYYY-MM-DD`, and two-digit years `DD\u002FMM\u002FYY` (expand `YY` to `20YY` unless clearly historical).\r\n- When both halves of a `NN\u002FNN\u002FYYYY` token could be either day or month, let the receipt locale decide. If the address block names Malaysia, Kuala Lumpur, Selangor, Johor, Penang, Singapore, UK or EU, treat the **first** number as the day; otherwise fall back on an unambiguous signal (any component `>12` must be the day).\r\n- Before emitting, verify month ∈ 1..12 and day ∈ 1..31 and parse through `datetime.date(...)`; if OCR produced an impossible pair like `01\u002F13\u002F2018`, swap day\u002Fmonth and retry.\r\n- Always output `YYYY-MM-DD`.\r\n\r\n### Total amount: keyword hierarchy\r\n\r\nApply the keyword hierarchy strictly, most specific first, and skip exclusion keywords **on the same line**:\r\n\r\n1. `GRAND TOTAL`\r\n2. `TOTAL RM`, `TOTAL: RM`\r\n3. `TOTAL AMOUNT`\r\n4. `TOTAL`, `AMOUNT`, `TOTAL DUE`, `AMOUNT DUE`, `BALANCE DUE`, `NETT TOTAL`, `NET TOTAL`\r\n\r\nSkip any candidate line whose keyword is `SUBTOTAL`, `SUB TOTAL`, `TAX`, `GST`, `SST`, `DISCOUNT`, `CHANGE`, or `CASH TENDERED`. Pre-tax totals, change due, cash tendered, and standalone rounding-adjustment lines must never win.\r\n\r\n### Prefer the final rounded total\r\n\r\nMany Malaysian and SE-Asian receipts print **both** a pre-rounding \"inclusive of GST\" total and a final rounded total that is the actual amount paid:\r\n\r\n```\r\nTotal (Inclusive of GST) : 47.70\r\nRounding Adj             : -0.02\r\nTOTAL                    : 47.68\r\n```\r\n\r\nWhen more than one candidate survives the exclusion filter, choose the one that appears **after** any `Rounding Adj`, `Total Rounded`, `Rounded`, or final bold `TOTAL` line. The final rounded value is the paid amount; the earlier \"Inclusive of GST\" number is pre-rounding and will fail strict equality. Equivalently: among surviving matches, prefer a line whose keyword is exactly `TOTAL` and that appears after a rounding line, over an earlier `Total (Inclusive of GST)`.\r\n\r\n### Numeric parsing\r\n\r\n- Accept comma thousands separators (`1,234.56`) and strip them before formatting.\r\n- Within the extracted number only, repair common OCR confusions: `O\u002Fo → 0`, `l\u002FI → 1`, `S → 5`, `B → 8` when flanked by digits. Never apply these substitutions to the keyword text — doing so turns `GST` into `G5T` and misroutes the match.\r\n- If the keyword and its value are on different lines, take the **last** number on the next non-empty line (the amount column), not the first (which is usually quantity or unit price).\r\n- Final emission must be a string matching `^\\d+\\.\\d{2}$`; produce it with `f\"{value:.2f}\"` after parsing the receipt number as a `Decimal`.\r\n\r\n### Do not fabricate values\r\n\r\nEvery emitted date and amount must come from a token actually present in the combined OCR text (after the normalisations above). If no keyword candidate survives filtering, run one more preprocessing variant — invert for dark backgrounds, upscale 3x, or threshold at both ~110 and ~160 — before giving up. Only emit `null` when every pass still yields no plausible candidate; a null is explicitly preferable to a guess, but a guess that is not literally on the receipt (e.g. a 3-digit amount concatenated from unrelated digits on an obviously small-ticket receipt) is the worst outcome.\r\n\r\n### Batch sanity checks\r\n\r\nBefore writing results for the whole batch:\r\n\r\n- Cross-check each total against the line-item column: if the chosen amount is more than ~5x the sum of visible item amounts (or more than ~5x the plausible receipt-size range for that image size), re-OCR that single image with alternative preprocessing before accepting the value.\r\n- Flag any amount missing a decimal point (e.g. `\"2325\"` instead of `\"23.25\"`); such strings almost always indicate a stripped period that must be reinserted or the record rechecked.\r\n- Dates must land in a plausible calendar range (e.g. within the last ~15 years for retail receipts); a date like `2117-02-02` is almost certainly a misread `2017-02-02`.\r\n\r\n\r\n## Error Handling\r\n\r\n### Common Issues and Solutions\r\n\r\n**Issue**: Tesseract not found\r\n\r\n```python\r\n# Verify Tesseract is installed\r\ntry:\r\n    pytesseract.get_tesseract_version()\r\nexcept pytesseract.TesseractNotFoundError:\r\n    print(\"Tesseract is not installed or not in PATH\")\r\n```\r\n\r\n**Issue**: Poor OCR quality\r\n\r\n- Preprocess image (grayscale, contrast, sharpen)\r\n- Use appropriate PSM mode for the document type\r\n- Ensure image resolution is sufficient (300+ DPI)\r\n\r\n**Issue**: Empty or garbage output\r\n\r\n- Check if image contains actual text\r\n- Try different PSM modes\r\n- Verify image is not corrupted\r\n\r\n\r\n## Quality Self-Check\r\n\r\nBefore returning results, verify:\r\n\r\n- [ ] Output is valid JSON (use `json.loads()` to validate)\r\n- [ ] All required fields are present (`success`, `filename`, `extracted_text`, `confidence`, `metadata`)\r\n- [ ] Text preserves logical reading order\r\n- [ ] Confidence level reflects actual OCR quality\r\n- [ ] Warnings array includes all detected issues\r\n- [ ] Special characters are properly escaped in JSON\r\n\r\n\r\n## Limitations\r\n\r\n- Tesseract works best with printed text; handwriting recognition is limited\r\n- Accuracy decreases with decorative fonts, artistic text, or extreme stylization\r\n- Mathematical equations and special notation may not extract accurately\r\n- Redacted or watermarked text cannot be recovered\r\n- Severe image degradation (blur, noise, low resolution) reduces accuracy\r\n- Complex multi-column layouts may require custom PSM configuration\r\n\r\n\r\n## Version History\r\n\r\n- **1.0.0** (2026-01-13): Initial release with Tesseract\u002Fpytesseract OCR\r\n",{"name":5,"description":7},"main","cdcb6d230f3b15c3c33f8a2bdd16439f43602cbe","jpg-ocr-stat\u002Fresult\u002Fskillhone__paratera-opus47-diagnosis-s0-20261031\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\u002Fskillhone__paratera-opus47-diagnosis-s0-20261031\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",[],[]]