Back to Puter

puter.ai.img2txt()

src/docs/src/AI/img2txt.md

26.073.0 KB
Original Source

Given an image, returns the text contained in the image. Also known as OCR (Optical Character Recognition), this API can be used to extract text from images of printed text, handwriting, or any other text-based content. You can choose between AWS Textract (default) or Mistral’s OCR service when you need multilingual or richer annotation output.

Syntax

js
puter.ai.img2txt(image, testMode = false)
puter.ai.img2txt(image, options = {})
puter.ai.img2txt({ source: image, ...options })

Parameters

image / source (String|File|Blob) (required)

A string containing the URL or Puter path, or a File/Blob object containing the source image or file. When calling with an options object, pass it as { source: ... }. Maximum input size at 10MB.

testMode (Boolean) (Optional)

A boolean indicating whether you want to use the test API. Defaults to false. This is useful for testing your code without using up API credits.

options (Object) (Optional)

Additional settings for the OCR request. Available options depend on the provider.

OptionTypeDescription
providerStringThe OCR backend to use. 'aws-textract' (default) | 'mistral'
modelStringOCR model to use (provider-specific)
testModeBooleanWhen true, returns a sample response without using credits. Defaults to false

AWS Textract Options

Available when provider: 'aws-textract' (default):

OptionTypeDescription
pagesArray<Number>Limit processing to specific page numbers (multi-page PDFs)

For more details about each option, see the AWS Textract documentation.

Mistral Options

Available when provider: 'mistral':

OptionTypeDescription
modelStringMistral OCR model to use
pagesArray<Number>Specific pages to process. Starts from 0
includeImageBase64BooleanInclude image URLs in response
imageLimitNumberMax images to extract
imageMinSizeNumberMinimum height and width of image to extract
bboxAnnotationFormatStringSpecify the format that the model must output for bounding-box annotations
documentAnnotationFormatStringSpecify the format that the model must output for document-level annotations

For more details about each option, see the Mistral OCR documentation.

Any properties not set fall back to provider defaults.

Return value

A Promise that will resolve to a string containing the text contained in the image.

In case of an error, the Promise will reject with an error message.

Examples

<strong class="example-title">Extract the text contained in an image</strong>

html;ai-img2txt
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.ai.img2txt('https://assets.puter.site/letter.png').then(puter.print);
    </script>
</body>
</html>