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==0.1.2
    
  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-0.2.0.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-0.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: image_text_reader-0.2.0.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-0.2.0.tar.gz
Algorithm Hash digest
SHA256 46a5885e919d059e762e03b464124ef597ee1cc7b5a70700e07038b5bf029ac5
MD5 7ff94937b513c1ee38da6213ff6e2d1b
BLAKE2b-256 32c4a4b8ef730645cb8b6017348403f8db19c98fc126051ebec84ef4729d1b65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for image_text_reader-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e2e26f771345a23fb1414ed75115478db285f210f2da85718f77cdb808e4fb7
MD5 118f82bf8f4bf1bef5dcd781c7f1099f
BLAKE2b-256 a5ab3d03146063755de14e92905d51faf96f88f6d6068c6db4da5f7167e3ea0a

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