Skip to content

bokorarmin/image-text-reader-py-library

Repository files navigation

Image Text Reader

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

note: the current code only works if tessaract is set up for hungarian language pack

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages