Encoding a file in Base64

The Vision OCR API and OCR API support images encoded in Base64. Encode your image or PDF file in Base64:

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 Base64 encoding.
        import base64
        
        # Create a function that encodes a file and returns the encoded result.
        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 into memory.
        var fs = require('fs');
        var file = fs.readFileSync('/path/to/file');
        
        // Get the file contents encoded in Base64
        var encoded = Buffer.from(file).toString('base64');
        
// Import a library for Base64 encoding.
        import org.apache.commons.codec.binary.Base64;
        
        // Get the file contents encoded in Base64.
        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 encoded in Base64.
        base64.StdEncoding.EncodeToString(content)
        

In the request body, include the contents you got after encoding an image or PDF file in Base64:

{
            "folderId": "b1gvmob95yys********",
            "analyze_specs": [{
                "content": "iVBORw0KGgo...",
                ...
            }]
        }
        
        

Where:

  • folderId: ID of the folder for which your account has the ai.vision.user role or higher.
  • content: Base64-encoded image or PDF file contents.

Useful links

Folder: Space containing the Yandex Cloud resources. To authenticate to AI Studio, you need a folder ID. To get it, in the AI Studio interface, hover over the folder name at the top of the screen and click image. The ID will be copied to the clipboard.

image