How to Extract Text from Images with Azure AI Vision (OCR)
Scanning invoices, receipts, or paper documents stops being a problem once the text can be read automatically. With Azure AI Vision you can extract text from images (OCR) in just a few minutes, using the Read feature of Image Analysis. The result is searchable text, ready to store in a database or push into a report.
Prerequisites
- An active Azure subscription.
- An Azure AI Services (or Computer Vision) resource created in the portal, in a region that supports Image Analysis 4.0.
- Python 3.8 or later installed.
- The resource key and endpoint at hand.
- A test image containing text (a photo of a receipt, for example).
Step 1: Create the resource and store the credentials
In the Azure portal, create an Azure AI Services resource. Once created, open the Keys and Endpoint section and copy KEY 1 and the Endpoint. Never put the key in your code: store it in environment variables.
setx VISION_KEY "your-key"
setx VISION_ENDPOINT "https://your-resource.cognitiveservices.azure.com/"
On macOS or Linux, use export VISION_KEY="..." and export VISION_ENDPOINT="...". Close and reopen the terminal so the variables take effect.
Step 2: Install the Image Analysis SDK
The official package handles all communication with the service, so you do not have to build HTTP requests by hand.
pip install azure-ai-vision-imageanalysis
Step 3: Extract text from a local image
This is the minimal OCR example. You open the image in binary mode, request the READ feature, and walk the result. The service returns the text organised into blocks and lines.
import os
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.ai.vision.imageanalysis.models import VisualFeatures
from azure.core.credentials import AzureKeyCredential
client = ImageAnalysisClient(
endpoint=os.environ["VISION_ENDPOINT"],
credential=AzureKeyCredential(os.environ["VISION_KEY"]),
)
with open("receipt.png", "rb") as f:
image_data = f.read()
result = client.analyze(
image_data=image_data,
visual_features=[VisualFeatures.READ],
)
if result.read is not None:
for block in result.read.blocks:
for line in block.lines:
print(line.text)
If everything works, you will see each line of text from the image in the terminal, in the order it appears.
Step 4: Read text from a URL
When the image is already online (in a public blob, for instance), there is no need to download it. Just swap the method.
result = client.analyze_from_url(
image_url="https://public-example/image.png",
visual_features=[VisualFeatures.READ],
)
text = "\n".join(
line.text
for block in result.read.blocks
for line in block.lines
)
print(text)
Notice that we joined everything into a single string. That is usually how you store OCR output in a text column.
Step 5: Check the confidence of each word
OCR is never perfect. Each word comes with a confidence value between 0 and 1, which lets you decide what to accept automatically and what to send for human review.
THRESHOLD = 0.8
for block in result.read.blocks:
for line in block.lines:
for word in line.words:
if word.confidence < THRESHOLD:
print("Review:", word.text, round(word.confidence, 2))
A simple and effective rule: accept anything above 0.8 and flag the rest for validation.
Verify the result
Compare the printed text with the original image: numbers and dates should match. Common errors and how to fix them:
- 401 / Access denied: the key or the endpoint are swapped, or the resource is in a different region.
- 400 InvalidImageSize: the image must be at least 50x50 pixels and must not exceed the service size limits (very large images should be resized).
- Empty result: the photo is blurry or poorly lit. Shoot the document straight on, with good contrast, and try again.
Tip: before investing in image pre-processing, simply try a better photo. It usually solves most cases.
Conclusion
You now have working OCR in Python with Azure AI Vision, reading both local files and URLs, plus a confidence filter to control quality. The natural next step is to write the extracted text into a table (SQL Server, Lakehouse, or Fabric) and search over it. And if what you need is not loose text but structured fields from invoices and forms? In that case, Azure AI Document Intelligence is the right service — which of the two fits the documents you are dealing with?