Extract Text from an Image with Python: Tesseract vs GLM-OCR
Two practical ways to extract text from images in Python — local OCR with pytesseract, and the GLM-OCR API for structured Markdown with tables and formulas.
There are two common ways to extract text from an image in Python:
- Run OCR locally with Tesseract through the
pytesseractwrapper. Free, offline, good for clean printed text. - Call an AI OCR API such as GLM-OCR. Better on complex layouts, and returns structured Markdown with tables and LaTeX formulas.
If you only need the result once and don’t want to write code, you can also paste the image into ExtractFromIMG, which runs GLM-OCR in the browser.
Option 1: pytesseract (local Tesseract OCR)
Tesseract is an open-source OCR engine (Apache 2.0) with an LSTM-based recognizer and support for more than 100 languages. pytesseract is a Python wrapper around the Tesseract command-line program, so you need to install Tesseract itself first.
Install Tesseract:
# macOS
brew install tesseract
# Ubuntu / Debian
sudo apt install tesseract-ocr
# Windows: install from the official installer, then add it to PATH
Install the Python packages:
pip install pytesseract pillow
Extract text:
from PIL import Image
import pytesseract
image = Image.open("screenshot.png")
text = pytesseract.image_to_string(image, lang="eng")
print(text)
For other languages, install the language data and pass it with lang, for example lang="eng+chi_sim".
Limitations. Tesseract returns plain text. Tables lose their structure, multi-column layouts can be read in the wrong order, and math formulas are not converted to LaTeX. Accuracy also drops on photos with uneven lighting and on handwriting, so you often need preprocessing (grayscale, thresholding, deskewing) with OpenCV or Pillow.
Option 2: the GLM-OCR API (structured Markdown)
GLM-OCR is an open-source multimodal OCR model from Z.ai for complex documents. The hosted API analyzes the page layout and returns Markdown, so tables stay tables and formulas come back as LaTeX. According to the official docs, it accepts JPG, PNG and PDF, with images up to 10MB.
Get an API key from the Zhipu open platform, then send the image as a base64 data URI:
import base64
import requests
API_KEY = "your-api-key"
with open("table.png", "rb") as f:
b64 = base64.b64encode(f.read()).decode()
resp = requests.post(
"https://open.bigmodel.cn/api/paas/v4/layout_parsing",
headers={"Authorization": f"Bearer {API_KEY}"},
json={"model": "glm-ocr", "file": f"data:image/png;base64,{b64}"},
timeout=60,
)
resp.raise_for_status()
print(resp.json()["md_results"])
The md_results field contains the Markdown. You can also pass a public image URL in file instead of base64.
The official Python SDK wraps the same API:
pip install glmocr
See the GLM-OCR README for SDK configuration and for self-hosting the model with vLLM, SGLang or Ollama.
Which one should you choose?
| pytesseract | GLM-OCR API | |
|---|---|---|
| Runs offline | Yes | No (hosted API), unless you self-host the model |
| Cost | Free | API usage is billed by the provider |
| Output | Plain text (also hOCR, TSV, PDF) | Markdown with tables and LaTeX |
| Complex layouts and tables | Weak | Strong |
| Math formulas | No | Yes, as LaTeX |
| Setup | Install Tesseract binary | API key |
Rule of thumb: use pytesseract for simple, clean printed text where you need offline processing. Use GLM-OCR when you care about tables, formulas, reading order or photos of real-world documents.
No-code alternative
To test GLM-OCR on your own images before writing any code, upload a screenshot to ExtractFromIMG — it runs the same document model and returns Markdown you can copy. When you need a file back, use Markdown to Word or Markdown to Excel.