ocrmac

1.0.1 · active · verified Thu Apr 16

ocrmac is a Python wrapper designed to extract text from images specifically on macOS systems. It leverages Apple's Vision framework to provide fast and accurate Optical Character Recognition (OCR) capabilities. The library is currently at version 1.0.1 and maintains an active development and release cadence, with updates addressing features and bug fixes. It requires macOS 10.15 (Catalina) or newer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ocrmac` to extract text from an image. It covers both the class-based `ocrmac.OCR` approach and the functional `ocrmac.text_from_image` method, including setting language preferences. A dummy image is created for a runnable example.

from ocrmac import ocrmac
from PIL import Image
import os

# Create a dummy image for demonstration purposes
dummy_image_path = 'test_image.png'
img = Image.new('RGB', (60, 30), color = 'red')
from PIL import ImageDraw, ImageFont
d = ImageDraw.Draw(img)
try:
    # Use a system font path for broader compatibility
    font = ImageFont.truetype("/System/Library/Fonts/Supplemental/Arial.ttf", 12)
except IOError:
    font = ImageFont.load_default() # Fallback
d.text((10,10), "Hello OCR!", fill=(0,0,0), font=font)
img.save(dummy_image_path)

# Perform OCR using the OCR class
try:
    annotations = ocrmac.OCR(dummy_image_path, language_preference=['en-US']).recognize()
    print("Detected annotations:")
    for text, confidence, bounding_box in annotations:
        print(f"  Text: '{text}', Confidence: {confidence:.2f}, Bounding Box: {bounding_box}")

    # Alternatively, use the functional interface
    text_output = ocrmac.text_from_image(dummy_image_path, language_preference=['en-US'])
    print("\nDetected text (functional interface):")
    print(text_output)

finally:
    # Clean up the dummy image
    if os.path.exists(dummy_image_path):
        os.remove(dummy_image_path)

view raw JSON →