Convert Multiple Images to Text using OCR using Python

You will find multiple free online services to convert OCR text. But those offers limited number of conversion. You have to pay if you want unlimited conversion. There are some free applications to convert OCR text. Again those applications have limited capability.

So, in this article, I will show you how you can convert multiple images into OCR text using Python. With the Python code you can convert as many as images into texts in one go.

Convert Images to Text using Python

1. Install Python

Download & install python on your computer. You can download python on this official website.

2. Install Tesseract

With Python you need a library named Tesseract. This library will convert OCR images into text. Download and install Tesseract from here.

3. Install ‘pytesseract’ library

Copy the below code and paste it to command prompt window to install the library using PIP.

pip install pytesseract

4. Save the code using ‘convert.py’

import pytesseract
from PIL import Image
import os

input_folder = 'images/'
output_folder = 'text/'
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

for image_name in os.listdir(input_folder):
    print(f"Processing {image_name}") 
    
    image_path = os.path.join(input_folder, image_name)
    image = Image.open(image_path)

    text = pytesseract.image_to_string(image)
    
    output_path = os.path.join(output_folder, image_name.replace(".jpg", ".txt"))
    
    with open(output_path, "w") as text_file:
        text_file.write(text)
        

print("OCR extraction completed!")

5. Define Pytesseract path

On the line 7, replace the pytesseract installation location with your version.

6. Save the images

Now paste all the images into the ‘images’ folder and create another folder ‘text’ to save all the converted text.

7. Run the code

Finally, run the Python program, and it will convert all images into text. You will see the progress. You will see the converted text files in the ‘text’ folder when done.

Nur Islam

Leave a Reply