Base64 encode and decode files

Base64 encode images

To make image generation requests you must send image data asBase64 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. JSONis a text format that does not directly support binary data, so you will need toconvert such binary data into text usingBase64 encoding.

Most development environments contain a nativebase64 utility toencode a binary into ASCII text data. To encode a file:

Linux

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

base64INPUT_FILE -w 0 >OUTPUT_FILE

macOS

Encode the file using thebase64 command line tool:

base64 -iINPUT_FILE -oOUTPUT_FILE

Windows

Encode the file using theBase64.exe tool:

Base64.exe -eINPUT_FILE >OUTPUT_FILE

PowerShell

Encode the file using theConvert.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 neitherdesirable or practical. In practice, you will be embedding base64 encoded fileswithin client code. All supported programming languages have built-in mechanismsfor base64 encoding content.

Python

# Import the base64 encoding library.importbase64# Pass the image data to an encoding function.defencode_image(image):withopen(image,"rb")asimage_file:encoded_string=base64.b64encode(image_file.read())returnencoded_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. Youcan use the following client library samples to decode this data and save itlocally 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):withopen("b64DecodedImage.png","wb")asfh:fh.write(base64.decodebytes(b64_encoded_string))

Node.js

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

Java

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

Go

// Import packagesimport("encoding/base64""io""os")// Add encoded file stringvarb64=`TWFuIGlz...Vhc3VyZS4=`// Decode base64-encoded stringdec,err:=base64.StdEncoding.DecodeString(b64)iferr!=nil{panic(err)}// Create output filef,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 theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.