This project shows how to extract text from any image using Python and Tesseract OCR. In just a few steps, you can convert images into editable text!
π Project Requirements
pytesseract
– Python wrapper for TesseractPillow
– For image processing- Tesseract-OCR – The OCR engine (must be installed separately)
π Download Tesseract-OCR
π Download the latest stable version from this direct link:
π Download Tesseract-OCR for Windows
After downloading:
- Run the setup file to install Tesseract-OCR
- By default, it's installed at:
C:\Program Files\Tesseract-OCR\tesseract.exe
- If you want to change the path, make sure to set it during installation
π How to Find the Path to tesseract.exe
If you're unsure where Tesseract was installed, here’s how to find the path:
- Open the folder:
C:\Program Files\Tesseract-OCR
- Look for the file:
tesseract.exe
- Copy the full path from the address bar
Example path to use in your Python script:
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
π¦ Install Required Python Libraries
pip install pytesseract
pip install pillow
π Python Code to Extract Text from Image
Make sure the image (e.g. quote1.png
) is in the same folder as your script.
import pytesseract
from PIL import Image
# ✅ Set the full path to tesseract.exe
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Load and process image
image = Image.open("quote1.png")
text = pytesseract.image_to_string(image)
# Display the extracted text
print(text)
⚡ Pro Tips & Hacks
- π Use high-quality images with clear text for best results
- π§½ Preprocess image: grayscale, resize, sharpen etc. for better OCR accuracy
- π For different languages, use
lang='your_lang_code'
inimage_to_string
- π Automate multiple images by using a loop with a folder
✅ Example Output
The script will print the extracted text directly to the terminal. You can also save it to a file if needed.
with open("output.txt", "w", encoding="utf-8") as file:
file.write(text)
π₯ If you have any problem related to Code then - Comment Below
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment