Optical Character Recognition (OCR) is a popular technology designed to convert different types of documents into editable and searchable data.
From scanned documents to photos of documents, OCR can be an effective tool for extracting text and helping automate the data extraction process. Python is one of the most versatile languages and has various libraries for performing OCR.
In this article, we will explore a Python script that uses the easyocr library to perform OCR on local image files.
pace
Import necessary libraries
Set up OCR reader using easyocr
Search text on images in folder
Set up main function using cli tool
Now let us unveil and understand each part of this script in detail.
1. Import necessary libraries
import os
from PIL import Image
import argparse
from typing import List import easyocr
The script first imports the necessary Python libraries.
os is used to interact with the operating system, allowing scripts to traverse directories in the system.
PIL (Python Image Library) is used to open, manipulate and save different image file formats.
argparse is used to write user-friendly command line interfaces that process commands given by users.
typing promotes clearer type hints, improving code readability and debugging.
easyocr is a library that allows us to perform OCR tasks efficiently.
2. Set up OCR reader using easyocr
reader = easyocr.Reader(['en'])
This line creates the easyocr reader for English ('en'). This reader will later be used to recognize text in images.
def ocr_scan(image_path: str) -> str: