You can provide image data to the Vision API by specifying the URI path to the image, or by sending the image data as base64-encoded text.
Most development environments contain a native "base64" utility to encode a binary image into ASCII text data. To encode an image file:
Linux
base64 input.jpg > output.txt
Mac OSX
base64 -i input.jpg -o output.txt
Windows
C:> Base64.exe -e input.jpg > output.txt
PowerShell
[Convert]::ToBase64String([IO.File]::ReadAllBytes("./input.jpg")) > output.txt
You can then use this output image data natively within the JSON request:
{ "requests":[ { "image":{ "content": "BASE64_ENCODED_DATA" }, "features": [ { "type":"LABEL_DETECTION", "maxResults":1 } ] } ] }
Each programming language has its own way of base64 encoding image files:
Python
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 = image.read()
return base64.b64encode(image_content)
Node.js
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');
// Convert the image data to a Buffer and base64 encode it.
var encoded = Buffer.from(imageFile).toString('base64');
Java
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());
Go
In Go, you can base64-encode image files as follows:
import ( "bufio" "encoding/base64" "io/ioutil" "os" ) // Open image file. f, _ := os.Open("image.jpg") // Read entire image into byte slice. reader := bufio.NewReader(f) content, _ := ioutil.ReadAll(reader) // Encode image as base64. base64.StdEncoding.EncodeToString(content)