Use Gemini AI Flash 2. 0 Experimental to create on the fly images /icons in your app

In March 2025, Google announced the availability of Google Flash 2.0 Experimental AI model capable of image generation based on text prompt.

This tip is about using Gemini Flash 2 to create images or icons in your app. If the app has a requirement of displaying a product image or icon in the app in say product inventory table,  one can use Gemini Flash 2.0 AI model to create such images on the fly when the record is being added to the app.

The app view when the record is being added looks like follows. 

Once the record is added in the app, after around 7 to 12 seconds, the image is created and populated in the record.

Gemini Flash 2 Image Creation - 3 April 2025 | Loom

The workflow for the functionality of this tip is as below 

 

Show More

 

Suvrutt_Gurjar_0-1743663790490.png

 


 

 

Product Record fields in the AppSheet app used in image generation are mentioned below

 

Show More

The user fills in the following highlighted three fields related to the image generation when adding a new products record.

Suvrutt_Gurjar_0-1743654648203.png

 

The prompt code in the GAS, based on the three AppSheet Product record fields is as follows

 

 const prompt = "Create an image with image type of " +imgType + " of " + productName + " on a" + imgBackground;
 
 
The GAS code is as follows
 
Show More
function productImagesGeminiFlash2(productName, imgBackground, imgType) {
   // The function gets as input parameters A) product name being added in the AppSheet Products table
   //B) The background color the image or icon should have and C) The image type -photo or icon

  //Please replace with your Gemini API Key
  const apiKey = "Insert you Gemini API Key";

  //This is the folder ID of the Google drive folder where the images created by the Gemini Flash 2 are stored.
  //This folder is in the root folder structure of the Appsheet app as usual with most AppSheet apps

  const folderId= "Insert the AppSheet app's Image folder ID in G Drive";
  // Retrieve the Googld edrive folder
  const imageFolder = DriveApp.getFolderById(folderId);

  //Create a prompot for the Gemini 2.0 Flash Experimental model to creat the photo or icon based on the prompt

  const prompt = "Create an image with image type of " +imgType + " of " + productName + " on a" + imgBackground;
 

  const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=${apiKey}`;
  const payload = {
    contents: [{ parts: [{ text: prompt }], role: "user" }],
    generationConfig: { responseModalities: ["TEXT", "IMAGE"]}
  };

  const options = {
    payload: JSON.stringify(payload),
    contentType: "application/json",
    muteHttpExceptions: true,
  };
  const res = UrlFetchApp.fetch(url, options);

  const obj = JSON.parse(res.getContentText());
  const imageObj = obj.candidates[0].content.parts.find((e) => e.inlineData);
  const imageBlob = Utilities.newBlob(
    Utilities.base64Decode(imageObj.inlineData.data),
    imageObj.inlineData.mimeType
  );
// Save the file created by the Gemini Flash onto the images folder of the AppSheet app.
//Give the name of the product to the image
var imageFile=imageFolder.createFile(imageBlob.setName(productName+".png"));

//The images created by the Gemini Flash model are very large (Generally 1024 X 1024 pixels or similar sized)
//The images created by Gemini Flash are also very large in memory size , many times close to or even more than 1 MB
//So we need to reduce the image dimensions in pixels as well as in memory size.
//The code below does that
//Here we have used "ImgApp" library by Google Apps Script expert Kanshi Tanaike
//The "ImgApp" library is availavle at GitHib
const imageFileID = imageFile.getId();
var width= 400;

 var res2 = ImgApp.doResize(imageFileID, width);
 Logger.log(res2)
// The AppSheet app will use this small size file with reduced dimensions and memory size
//The file name has a suffix of small

imageFolder.createFile(res2.blob.setName(productName +"_small.png"));

//Since the dimensions of the image file created by Gemini Flash AI have been reduced and a new small dimensions file is created,
//we can delete the original large dimensions image file created by Gemini Flash AI

Drive.Files.remove(imageFileID);
}


 

Technical points to note are as follows

 

Show More

1) Image Storage Size and Dimensions: Most of the times Gemini creates nice images.The images created by Gemini Flash 2 by default are large in storage size ( app 1 MB) and typically with large dimensions ( 1024 X 1024 pixels or similar). I tried a lot to reduce the image dimensions and storage size within the image generation process of Gemini Flash itself but could not do so. But maybe more experimentation is required.

As a result I used the doresize() method within the  ImgApp " library by the Google Apps Script expert Kanshi Tanaike . With this method the images are converted to 400 pixels width with height at same aspect ratio as the original image . The image size reduces by around 80 %  to 180 K or so.

2) More definitive prompt: At times the image generation may not be exactly as intended. We would then need to give a more definitive prompt to the AI model.
3) Time to execute the GAS  and Gemini Flash: It takes around 7  to 12 seconds for the GAS and the underlying Gemini Flash mode to create the image once the Appsheet record is added.

 

 

References and Acknowledgements are mentioned below

 

Show More

1) I have referenced the GAS code in the article Generate Images with Gemini API using Google Apps Script

I have of course modified it to accept inputs from an AppSheet app and other requirements of the this tip. However the image generation related code is mainly from the GAS code in the article.

2) I have reference the ImgApp " library by Kanshi Tanaike and its  doResize() method to reduce the image dimensions and storage size.

 

Disclaimer and other important points to note are mentioned below

 

Show More

1) This tip is only to demonstrate a technical option of integrating Google Gemini AI model and AppSheet through Google Apps Script.  It does not in anyway suggest direct adaption in production level apps.

2) Please evaluate all the necessary information and abide by all applicable  legal and usage terms mentioned on Google AI models sites  while using it in your AppSheet 

 

4 1 766
1 REPLY 1

Aurelien
Google Developer Expert
Google Developer Expert

Neat! Super great job @Suvrutt_Gurjar !

Top Labels in this Space