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.
You will need to install three things before this script will work:
- Python — the programming language the script runs on
- Tesseract — the OCR engine that actually reads text from images
- Two Python libraries — small add-ons that let the script talk to Tesseract
- Go to https://www.python.org/downloads/
- Click the big yellow "Download Python" button
- Run the installer
- 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".
Tesseract is the engine that reads text from images. Python alone cannot do this without it.
- Go to https://github.com/UB-Mannheim/tesseract/wiki
- Under the "The latest installers" section, download the file ending in
.exe(e.g.tesseract-ocr-w64-setup-5.x.x.exe) - Run the installer and click through it — the default options are fine
- Important: On the install screen, make sure "Add to PATH" is checked (it usually is by default)
- 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.
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.
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_DIRto the full path of the folder that contains your images - Change
OUTPUT_FILEto 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:
- Open File Explorer and navigate to your folder
- Click on the address bar at the top — the full path will appear highlighted
- 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.
- Open Command Prompt
- 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.
The script works with the following image types:
- PNG
- JPG / JPEG
- TIFF / TIF
- BMP
- GIF
- WebP
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)
"tesseract is not installed or it's not in your PATH"
Tesseract is installed but Windows cannot find it. Try the following:
- Press
Windows key, search for "Edit the system environment variables", and open it - Click "Environment Variables"
- Under "System variables", find the variable called Path and double-click it
- Click "New" and add:
C:\Program Files\Tesseract-OCR - 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.