Encoding a file as Base64
Written by
Updated at August 29, 2024
The Vision OCR API and OCR API support images in Base64Base64
:
UNIX
Windows
PowerShell
Python
Node.js
Java
Go
base64 -i input.jpg > output.txt
C:> Base64.exe -e input.jpg > output.txt
[Convert]::ToBase64String([IO.File]::ReadAllBytes("./input.jpg")) > output.txt
# Import a library for encoding files in Base64
import base64
# Create a function that will encode a file and return results.
def encode_file(file_path):
with open(file_path, "rb") as fid:
file_content = fid.read()
return base64.b64encode(file_content).decode("utf-8")
// Read the file contents to memory.
var fs = require('fs');
var file = fs.readFileSync('/path/to/file');
// Get the file contents in Base64 format.
var encoded = Buffer.from(file).toString('base64');
// Import a library for encoding files in Base64.
import org.apache.commons.codec.binary.Base64;
// Get the file contents in Base64 format.
byte[] fileData = Base64.encodeBase64(yourFile.getBytes());
import (
"bufio"
"encoding/base64"
"io/ioutil"
"os"
)
// Open the file.
f, _ := os.Open("/path/to/file")
// Read the file contents.
reader := bufio.NewReader(f)
content, _ := ioutil.ReadAll(reader)
// Get the file contents in Base64 format.
base64.StdEncoding.EncodeToString(content)
In the request body, provide the image or PDF file contents encoded as Base64:
{
"folderId": "b1gvmob95yys********",
"analyze_specs": [{
"content": "iVBORw0KGgo...",
...
}]
}
Where:
folderId
: ID of any folder for which your account has theai.vision.user
role or higher.content
:Base64
-encoded image or PDF file contents.