Base64 encode and decode files

Base64 encode images

To make image generation requests you must send image data as Base64 encoded text.

Using the command line

Within a gRPC request, you can simply write binary data out directly; however, JSON is used when making a REST request. JSON is a text format that does not directly support binary data, so you will need to convert such binary data into text using Base64 encoding.

Most development environments contain a native base64 utility to encode a binary into ASCII text data. To encode a file:

Linux

Encode the file using the base64 command line tool, making sure to prevent line-wrapping by using the -w 0 flag:

base64 INPUT_FILE -w 0 > OUTPUT_FILE

macOS

Encode the file using the base64 command line tool:

base64 -i INPUT_FILE -o OUTPUT_FILE

Windows

Encode the file using the Base64.exe tool:

Base64.exe -e INPUT_FILE > OUTPUT_FILE

PowerShell

Encode the file using the Convert.ToBase64String method:

[Convert]::ToBase64String([IO.File]::ReadAllBytes("./INPUT_FILE"))> OUTPUT_FILE

Create a JSON request file, inlining the base64-encoded data:

JSON

{
"instances":[
{
"prompt":"TEXT_PROMPT",
"image":{
"bytes_base64_encoded":"B64_BASE_IMAGE"
}
}
]
}

Using client libraries

Embedding binary data into requests through text editors is neither desirable or practical. In practice, you will be embedding base64 encoded files within client code. All supported programming languages have built-in mechanisms for base64 encoding content.

Python

# Import the base64 encoding library.
importbase64
# Pass the image data to an encoding function.
defencode_image(image):
 with open(image, "rb") as image_file:
 encoded_string = base64.b64encode(image_file.read())
 return encoded_string

Node.js

// 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

// Import the Base64 encoding library.
importorg.apache.commons.codec.binary.Base64;
// Encode the image.
StringencodedString=Base64.getEncoder().encodeToString(imageFile.getBytes());

Go

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)

Base64 decode images

API requests return generated or edited images as base64-encoded strings. You can use the following client library samples to decode this data and save it locally as an image file.

Python

# Import the base64 encoding library.
importbase64
# Pass the base64 encoded image data to a decoding function and save image file.
defdecode_image(b64_encoded_string):
 with open("b64DecodedImage.png", "wb") as fh:
 fh.write(base64.decodebytes(b64_encoded_string))

Node.js

varfs=require('fs');
// Create buffer object, specifying base64 as encoding
varbuf=Buffer.from(base64str,'base64');
// Write buffer content to a file
fs.writeFile("b64DecodedImage.png",buf,function(error){
if(error){
throwerror;
}else{
console.log('File created from base64 string');
returntrue;
}
});

Java

// Import libraries
importorg.apache.commons.codec.binary.Base64;
importorg.apache.commons.io.FileUtils;
// Create new file
Filefile=newFile("./b64DecodedImage.png");
// Convert base64 encoded string to byte array
byte[]bytes=Base64.decodeBase64("base64");
// Write out file
FileUtils.writeByteArrayToFile(file,bytes);

Go

// Import packages
import(
"encoding/base64"
"io"
"os"
)
// Add encoded file string
varb64=`TWFuIGlz...Vhc3VyZS4=`
// Decode base64-encoded string
dec,err:=base64.StdEncoding.DecodeString(b64)
iferr!=nil{
panic(err)
}
// Create output file
f,err:=os.Create("b64DecodedImage.png")
iferr!=nil{
panic(err)
}
deferf.Close()
if_,err:=f.Write(dec);err!=nil{
panic(err)
}
iferr:=f.Sync();err!=nil{
panic(err)
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月31日 UTC.