Stay organized with collections
Save and categorize content based on your preferences.
Base64 encoding
You can provide image data for optical character recognition 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:
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.importbase64# Pass the image data to an encoding function.defencode_image(image):image_content=image.read()returnbase64.b64encode(image_content)
Node.js
In Node.js, you can base64-encode image files as follows:
// Read the file into memory.varfs=require('fs');varimageFile=fs.readFileSync('/path/to/file');// Convert the image data to a Buffer and base64 encode it.varencoded=Buffer.from(imageFile).toString('base64');
Java
In Java, you can base64-encode image files as follows:
// Import the Base64 encoding library.importorg.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""os")// Open image file.f,_:=os.Open("image.jpg")// Read entire image into byte slice.reader:=bufio.NewReader(f)content,_:=io.ReadAll(reader)// Encode image as base64.base64.StdEncoding.EncodeToString(content)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eBase64 encoding allows you to send image data as text, which is useful for transmitting images in contexts where only text is supported.\u003c/p\u003e\n"],["\u003cp\u003eVarious operating systems like Linux, Mac OSX, and Windows, as well as their respective command lines, have native utilities or commands to perform base64 encoding on image files.\u003c/p\u003e\n"],["\u003cp\u003eMultiple programming languages including Python, Node.js, Java, and Go provide built-in libraries or functions for base64 encoding image data, each with its unique implementation.\u003c/p\u003e\n"],["\u003cp\u003eBase64-encoded image data can be directly embedded within a JSON request, specifically in the "content" field of an "image" object, for processing by applications requiring image input.\u003c/p\u003e\n"]]],[],null,["# Vertex AI services\n\nBase64 encoding\n---------------\n\nYou can provide image data for optical character recognition by specifying the URI path to the\nimage, or by sending the image data as base64-encoded text.\n\nMost development environments contain a native `base64` utility to\nencode a binary image into ASCII text data. To encode an image file: \n\n### Linux\n\n```\n base64 input.jpg \u003e output.txt\n```\n\n### Mac OSX\n\n```\n base64 -i input.jpg -o output.txt\n```\n\n### Windows\n\n```bash\n C:\u003e Base64.exe -e input.jpg \u003e output.txt\n```\n\n### PowerShell\n\n```bash\n [Convert]::ToBase64String([IO.File]::ReadAllBytes(\"./\u003cvar translate=\"no\"\u003einput.jpg\u003c/var\u003e\")) \u003e output.txt\n```\n\nYou can then use this output image data natively within the JSON request: \n\n```\n{\n \"requests\":[\n {\n \"image\":{\n \"content\": \"BASE64_ENCODED_DATA\"\n },\n \"features\": [\n {\n \"type\":\"TEXT_DETECTION\",\n \"maxResults\":1\n }\n ]\n }\n ]\n}\n```\n\nEach programming language has its own way of base64 encoding image files: \n\n### Python\n\nIn Python, you can base64-encode image files as follows: \n\n # Import the base64 encoding library.\n import base64\n\n # Pass the image data to an encoding function.\n def encode_image(image):\n image_content = image.read()\n return base64.b64encode(image_content)\n\n### Node.js\n\nIn Node.js, you can base64-encode image files as follows: \n\n // Read the file into memory.\n var fs = require('fs');\n var imageFile = fs.readFileSync('/path/to/file');\n\n // Convert the image data to a Buffer and base64 encode it.\n var encoded = Buffer.from(imageFile).toString('base64');\n\n### Java\n\nIn Java, you can base64-encode image files as follows: \n\n // Import the Base64 encoding library.\n import org.apache.commons.codec.binary.Base64;\n\n // Encode the image.\n byte[] imageData = Base64.encodeBase64(imageFile.getBytes());\n\n### Go\n\nIn Go, you can base64-encode image files as follows: \n\n```go\n import (\n \"bufio\"\n \"encoding/base64\"\n \"io\"\n \"os\"\n )\n\n // Open image file.\n f, _ := os.Open(\"image.jpg\")\n\n // Read entire image into byte slice.\n reader := bufio.NewReader(f)\n content, _ := io.ReadAll(reader)\n\n // Encode image as base64.\n base64.StdEncoding.EncodeToString(content)\n```"]]