How to Extract Text from Images Using Python

By hientd, at: April 5, 2024, 8:17 p.m.

Estimated Reading Time: 4 min read

How to Extract Text from Images Using Python
How to Extract Text from Images Using Python

How to Extract Text from Images Using Python

Extracting text from images—a process known as Optical Character Recognition (OCR)—has numerous applications, from digitizing printed documents to processing street signs in real-time. Python, with its rich ecosystem of libraries and APIs, offers several solutions for OCR tasks. This article explores four popular Python libraries and four cloud APIs for text extraction from images.

 

Python Libraries for OCR


1. pytesseract

  • Description: A wrapper for Google's Tesseract-OCR Engine.
  • Code Snippet: you need to run the installation first pip install pytesseract
     
    from PIL import Image
    import pytesseract

    text = pytesseract.image_to_string(Image.open('image.jpg'))
    print(text)

     

  • Pros: Free and open-source, supports multiple languages.
     
  • Cons: Can struggle with images containing complex layouts.

 

2. easyOCR

  • Description: A more recent library that supports over 40 languages and is designed for simplicity.
     
  • Code Snippet: installation vai pip install easyocr
     
    import easyocr

    reader = easyocr.Reader(['en'])
    results = reader.readtext('image.jpg')
    print(results)

 

  • Pros: Easy to use, good performance on various image types.
     
  • Cons: Larger size due to its deep learning models.


3. OCRopus

  • Description: An OCR suite written in Python, focusing on historical document recognition.
     
  • Code Snippet:
    # Pseudocode as OCRopus uses command line
    ocropus-rpred 'image.jpg'

 

  • Pros: Good for historical documents, open-source.
     
  • Cons: Less effective for modern text layouts, command-line based.

 

Cloud APIs for OCR


1. Microsoft OCR


2. Amazon Textract


3. Google Cloud Vision API

 

Conclusion

In conclusion, Python OCR offers a versatile range of tools and cloud APIs, each with its own strengths and weaknesses, catering to a wide array of use cases from simple text extraction to complex document analysis. Whether you're working with historical manuscripts or modern documents, there’s a FIT solution. However, choosing the right tool or API depends on your specific needs, including accuracy, language support, cost, and ease of integration. 

Since ChatGPT is available in Azure Services, Microsoft Document Intelligence or OCR seems to be the best now


Subscribe

Subscribe to our newsletter and never miss out lastest news.