Base64 Encoding

How you perform base64 encoding of image files depends on the way in which you send your requests.

The base64 command-line tool can encode a binary image into ASCII text data. Most development environments contain a native "base64" command-line tool. To encode an image file:


base64 input.jpg > output.txt


base64 -i input.jpg -o output.txt

You can use this output image data natively within the JSON request:

        "content": "base64-encoded data"
      "features": [

Each programming language has its own way of base64 encoding image files:


In Python, you can base64 encode image files as follows:

# Import the base64 encoding library.
import base64

# Pass the image data to an encoding function.
def encode_image(image):
  image_content =
  return base64.b64encode(image_content)


In Node.js, you can base64 encode image files as follows:

// Read the file into memory.
var fs = require('fs');
var imageFile = fs.readFileSync('/path/to/file');

// Covert the image data to a Buffer and base64 encode it.
var encoded = new Buffer(imageFile).toString('base64');


In Java, you can base64 encode image files as follows:

// Import the Base64 encoding library.
import org.apache.commons.codec.binary.Base64;

// Encode the image.
byte[] imageData = Base64.encodeBase64(imageFile.getBytes());

Send feedback about...

Cloud Vision API Documentation