Skip to main content

A library to read text from images

Project description

Image Text Reader

The image-text-reader library allows you to extract text from images using Optical Character Recognition (OCR) with the help of the pytesseract library and Pillow for image processing.

Table of Contents

Prerequisites

  • Python 3.x
  • Tesseract-OCR

Installation

  1. Install the required Python libraries:

    pip install image-text-reader
    
  2. Install Tesseract-OCR:

    • Windows: Download and install from here.

    • macOS: Use Homebrew to install:

      brew install tesseract
      
    • Linux: Use your package manager, for example:

      sudo apt-get install tesseract-ocr
      

Usage

  1. Create a Python script (e.g., test_script.py) and import the ocr_image function from the image_text_reader library:

    from image_text_reader import ocr_image
    
  2. Set the path to your image and Tesseract-OCR executable:

    # Update these paths for your system
    image_path = 'C:/path_to_your_image.jpg'  # Replace with the path to your test image
    tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'  # Path to Tesseract executable
    
    extracted_text = ocr_image(image_path, tesseract_cmd=tesseract_cmd)
    print("Extracted Text:")
    print(extracted_text)
    
  3. Run your script:

    python test_script.py
    

Code Explanation

  • Preprocessing Function:

    The preprocess_image function prepares the image for OCR by converting it to grayscale, sharpening it, and enhancing its contrast:

    def preprocess_image(image_path):
        image = Image.open(image_path).convert('L')
        image = image.filter(ImageFilter.SHARPEN)
        enhancer = ImageEnhance.Contrast(image)
        image = enhancer.enhance(2)
        return image
    
  • OCR Function:

    The ocr_image function processes the image and then extracts the text using pytesseract:

    def ocr_image(image_path, tesseract_cmd=None):
        if tesseract_cmd:
            pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
        image = preprocess_image(image_path)
        text = pytesseract.image_to_string(image, lang='eng')
        return text
    

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any changes.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

For more information, visit the image-text-reader library page on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

image_text_reader-1.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

image_text_reader-1.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file image_text_reader-1.0.1.tar.gz.

File metadata

  • Download URL: image_text_reader-1.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for image_text_reader-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a2f8e09c638c630796193aa9a9905a0873c11682c5fa63d60fdeab9212f5394a
MD5 74004d0f99df33ff08dacf0b16335639
BLAKE2b-256 e79a26653c540f6dad2dc2e5615e2c5b6ef2db46b02c1d532e39fb662f47c504

See more details on using hashes here.

File details

Details for the file image_text_reader-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for image_text_reader-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d270cc7dc9e411480a8dcf42826fad8efaac19a1c33e2bf882edf8e4534f442
MD5 3cb95c75f755c28dd17fd50242f61bb7
BLAKE2b-256 85d89f89488ddec86b3d29528a792ef6a7383f2cf039b0db81d4f08b1e55118f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page