OCR

OCR reads the text in an image and gives it back as plain text. Use it on scans, screenshots, receipts, forms and photos of pages.

Read text from an image

Send the image in a chat message, the same way as with vision, and ask for the text.

from pendra import Pendra

client = Pendra(api_key="pdr_sk_...")

response = client.chat.completions.create(
    model="glm-ocr",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Extract all text from this image."},
            {"type": "image_url",
             "image_url": {"url": "data:image/png;base64,iVBORw0..."}},
        ],
    }],
)
print(response.choices[0].message.content)
A scanned passage of text sent to the model
Response

Magna Carta (Medieval Latin for "Great Charter"), sometimes spelled Magna Charta, is a royal charter of rights agreed to by King John of England at Runnymede, near Windsor, on 15 June 1215. First drafted by the Archbishop of Canterbury, Cardinal Stephen Langton, to make peace between the unpopular king and a group of rebel barons…

You can also send the image with no text at all, and you'll get the transcription back. To get something else, such as one field or a table as JSON, ask for it in the same message as the image.

Tips

  • Send one page per request. Earlier pages in a conversation only slow it down.
  • Leave temperature unset. Pendra uses the setting each OCR model reads best with.
  • For general questions about a picture, such as "what colour is this?", use a vision model. An OCR model may return a 422 instead.

Models

glm-ocr is a good place to start. See every OCR model in the models list.

For the full message format, see the Chat completions API reference.