img2txtio
v1.1.0
Published
An SDK for interacting with the Img2Txt.io API to extract text from images.
Maintainers
Readme
img2txt.io Javascript Package
A Javascript Package for the img2txt.io API. Easily upload images and convert them to plain text or structured output programmatically.
Installation
npm install img2txtioOr install directly from source:
git clone https://github.com/Astro-gram/img2txt-javascript-package.git
cd img2txt-javascript-package
pip install .Quick Start
import { Img2TxtClient } from "img2txtio";
// Initialize client with your API key
const client = new Img2TxtClient("YOUR_API_KEY_HERE");
(async () => {
try {
// Convert an image to plain text
const result = await client.process(
"path/to/image.jpg", // image_path
"raw", // output_type (default: raw)
"Receipt from grocery", // description (optional)
"" // outputStructure (optional JSON string)
);
// `result` is an object containing the API response
console.log(result);
} catch (error) {
console.error(error);
}
})();API Reference
Class: Img2TxtClient
__init__(api_key: string)
Create a new client instance.
api_key(string): Your img2txt.io API key. You can generate one from the img2txt.io dashboard.
process(imagePath: string, outputType: string = "raw", description: string = "", outputStructure: string = ""): Promise<Record<string, any>>
Uploads the image at image_path and converts it to text or structured output.
Parameters:
image_path(string): Local path to the image file. Must exist, otherwise raisesFileNotFoundError.output_type(string): The output format. Defaults toraw. Other valid types depend on the API (e.g.,json,structured).description(string): Optional description or context to improve text extraction.outputStructure(string): Optional JSON string defining a structure/schema for the output. Must be valid JSON or raisesValueError.
Returns:
dict: Parsed JSON response from the API. On failure, raisesRuntimeError.- Example response:
{ 'text_output': [ 'Los Angeles', 'Florida', '$235', 'Round trip', 'Economy', '1 passenger', 'Lowest total price' ], 'job_id': '16e2364a-e1e2-468e-9be9-e07b695b2afd', 'creditsLeft': 86.115, 'success': True, 'message': 'Image processed successfully.' }
Error Handling
- Network or HTTP errors raise
requests.exceptions.HTTPError. - Missing files raise
FileNotFoundError. - Invalid JSON in
outputStructureraisesValueError. - API failures raise
RuntimeError.
License
This project is licensed under the MIT License. See the LICENSE file for details.
