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

Uploaded Python 3

File details

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

File metadata

  • Download URL: image_text_reader-0.1.2.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.1.2.tar.gz
Algorithm Hash digest
SHA256 ff1b75e485a549acac04b7863afc5efacd6a58af31ea35620bdde68cad839c64
MD5 cbe24a24149e556260d45345be7af74f
BLAKE2b-256 6a2ac26e78f213a14e19aec2957099ac6059d0a00e73d1bfb3bbe929789436c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for image_text_reader-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dbcff489f3324ace7435a9eca29d638e63e640a383467e6dc61998b0d0cc46ab
MD5 8d908bbe6619dc334623864633fbb353
BLAKE2b-256 eaa3b8e4e607601dd0770b463c3d4e47ce803ebefa4588d7854e15acbbd5ba27

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