Processing Images with Mathpix Python SDK
This section explains how to send an image to the Mathpix API using
mpxpy
and retrieve structured OCR results.Example: Processing an Image from URL
You can pass an image URL to the
image_new()
method to receive structured data output such as Mathpix Markdown (MMD) and individual OCR lines.from mpxpy.mathpix_client import MathpixClient
client = MathpixClient(
app_id="your-app-id",
app_key="your-app-key"
)
# Process an image file
image = client.image_new(
file_url="https://mathpix-ocr-examples.s3.amazonaws.com/cases_hw.jpg"
)
# Get the Mathpix Markdown (MMD) representation
mmd = image.mmd()
print(mmd)
# Get line-by-line OCR data
lines = image.lines_json()
print(lines)
The
The
mmd()
method returns a single string of LaTeX-style Mathpix Markdown.The
lines_json()
method returns a list of objects, each representing a line of text or math, along with metadata such as coordinates, confidence scores, and handwriting detection.Visual Example: From Image to Mathpix JSON
This is a real example that demonstrates how a handwritten equation is processed into structured output:

Example Output
[
{
"type": "math",
"cnt": [[49, 332], [49, 0], [774, 0], [774, 332]],
"included": true,
"conversion_output": true,
"is_printed": false,
"is_handwritten": true,
"id": "090b9d3a52e24421846eb864288ea20b",
"text": "\\( f(x)=\\left\\{\\begin{array}{ll}x^{2} & \\text { if } x<0 \\\\ 2 x & \\text { if } x \\geq 0\\end{array}\\right. \\)",
"confidence": 1,
"confidence_rate": 1
}
]
This data includes:
- The type of line (math, text, etc.)
- Bounding box (cnt)
- Whether the line is handwritten or printed
- The extracted LaTeX expression (text)
- A confidence score from the OCR engine
- You can also pass
file_path
for local images instead offile_url
. lines_json()
is especially useful for building interfaces or performing advanced analysis of each recognized element.