Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Batch OCR

This python script takes in a specified folder with an image or multiple images and then extracts the text out of all the images and then outputs into 1 text file in the specified output directory.


What You Need Before Starting

You will need to install three things before this script will work:

  1. Python — the programming language the script runs on
  2. Tesseract — the OCR engine that actually reads text from images
  3. Two Python libraries — small add-ons that let the script talk to Tesseract

Step 1 — Install Python

  1. Go to https://www.python.org/downloads/
  2. Click the big yellow "Download Python" button
  3. Run the installer
  4. Important: On the first screen of the installer, check the box that says "Add Python to PATH" before clicking Install

To verify it worked, open Command Prompt (press Windows key, type cmd, press Enter) and type:

python --version

You should see something like Python 3.12.x. If you see an error, Python did not install correctly — try again and make sure to check "Add to PATH".


Step 2 — Install Tesseract

Tesseract is the engine that reads text from images. Python alone cannot do this without it.

  1. Go to https://github.com/UB-Mannheim/tesseract/wiki
  2. Under the "The latest installers" section, download the file ending in .exe (e.g. tesseract-ocr-w64-setup-5.x.x.exe)
  3. Run the installer and click through it — the default options are fine
  4. Important: On the install screen, make sure "Add to PATH" is checked (it usually is by default)
  5. The default install location will be C:\Program Files\Tesseract-OCR\ — leave it as-is

To verify it worked, open a new Command Prompt window and type:

tesseract --version

You should see version information printed out. If you get an error, Tesseract is not on your PATH — see the Troubleshooting section at the bottom.


Step 3 — Install the Python Libraries

Open Command Prompt and paste in this command, then press Enter:

pip install pytesseract pillow

You will see text scrolling as it downloads and installs. Wait for it to finish. You should see a message like Successfully installed ... at the end.


Step 4 — Configure the Script

Open the file batch_ocr.py in any text editor (Notepad works fine).

Near the top of the file you will see these two lines:

INPUT_DIR = r"C:\Users\Mihir\OneDrive\Desktop\Video transcribe test\Images"
OUTPUT_FILE = r"C:\Users\Mihir\OneDrive\Desktop\Video transcribe test\Images\output.txt"
  • Change INPUT_DIR to the full path of the folder that contains your images
  • Change OUTPUT_FILE to the full path of where you want the results saved (including a filename ending in .txt)

How to find a folder's full path on Windows:

  1. Open File Explorer and navigate to your folder
  2. Click on the address bar at the top — the full path will appear highlighted
  3. Copy and paste it into the script

Important: Always keep the r before the quote mark and use the exact format shown, for example:

INPUT_DIR = r"C:\Users\YourName\Desktop\MyImages"
OUTPUT_FILE = r"C:\Users\YourName\Desktop\results.txt"

Save the file when done.


Step 5 — Run the Script

  1. Open Command Prompt
  2. Type the following and press Enter (replace the path with wherever you saved the script):
python "C:\Users\YourName\Downloads\Batch-OCR\batch_ocr.py"

Or, you can navigate to the folder first:

cd "C:\Users\YourName\Downloads\Batch-OCR"
python batch_ocr.py

The script will print the name of each image as it processes it. When it finishes, it will tell you where the output file was saved.


Supported Image Formats

The script works with the following image types:

  • PNG
  • JPG / JPEG
  • TIFF / TIF
  • BMP
  • GIF
  • WebP

Output Format

The output text file will contain all extracted text from every image, with each image's text separated by a header like this:

=== photo1.png ===
(extracted text here)

=== photo2.jpg ===
(extracted text here)

Troubleshooting

"tesseract is not installed or it's not in your PATH"

Tesseract is installed but Windows cannot find it. Try the following:

  1. Press Windows key, search for "Edit the system environment variables", and open it
  2. Click "Environment Variables"
  3. Under "System variables", find the variable called Path and double-click it
  4. Click "New" and add: C:\Program Files\Tesseract-OCR
  5. Click OK on all windows, then open a new Command Prompt and try again

"pip is not recognized"

Python was not added to PATH during install. Re-run the Python installer, choose Modify, and make sure "Add Python to environment variables" is checked.

"No supported image files found"

The folder path in INPUT_DIR is wrong, or the images are in a format not listed above. Double-check the path and make sure the images are a supported type.

The output text looks garbled or has errors

OCR accuracy depends on image quality. For best results, use high-resolution, clearly lit images where the text is sharp and not blurry.

About

This python script takes in a specified folder with an image or multiple images and then extracts the text out of all the images and then outputs into 1 text file in the specified output directory.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages