Lesson

Batch Generate Nano Banana Variations with AI Studio and Scripting

Subscribe to the AI Dev Essentials newsletter!

Duration
10 min
Access
Free
Transcript
Needs source

Subscribe to the AI Dev Essentials newsletter!

https://egghead.io/newsletters/ai-dev-essentials

Github Repo

https://github.com/johnlindquist/expressions

Manually creating dozens of variations for a single image in a web UI is tedious and inefficient. This lesson demonstrates a powerful workflow for automating this process by leveraging Google AI Studio's "Get code" feature to build a flexible Node.js script. You'll learn how to take a simple image-to-image generation task and scale it up to create a large batch of customized images programmatically.

This approach is perfect for generating marketing assets, social media content, or character sheets where you need a consistent base character with different expressions or styles.

The Workflow

  1. From UI to Code: Start by performing a single image variation task in Google AI Studio's Gemini Native Image interface. Then, use the "Get code" button to export the entire interaction—including the Base64-encoded source image and text prompt—as a ready-to-run TypeScript snippet.
  2. Project Setup: Set up a new Node.js project using a modern runtime like Bun for speed and simplicity. This includes initializing the project, installing the necessary Google GenAI and MIME type packages, and configuring your environment.
  3. API Key Management: Securely manage your Google AI API key by creating a .env file. This keeps your credentials out of your source code and makes your script portable.
  4. Script Refactoring for Automation: Refactor the initial code snippet for scalability:
    • Replace the hardcoded Base64 image data with a dynamic file read operation, allowing you to easily swap the source image.
    • Create an array of text prompts to define all the variations you want to generate (e.g., different emotions, accessories, or styles).
    • Wrap the API call in a loop that iterates through your prompts.
  5. Adding Robustness: Enhance the script with essential features like:
    • Error Handling: Implement try...catch blocks to ensure that if one image generation fails, the entire script doesn't crash.
    • Descriptive Logging: Add extensive console.log statements to monitor the script's progress, see which image is currently being processed, and get final statistics.
    • Unique File Naming: Programmatically generate unique filenames for each output image, often including a timestamp and the specific prompt used, to keep your assets organized.
  6. Leveraging AI for Development (Bonus): This lesson also showcases how a modern, AI-powered editor like Cursor can accelerate the script-building process itself—from generating the list of expressions to refactoring code and fixing stubborn TypeScript errors.

By the end of this lesson, you'll have a powerful, reusable script and a streamlined workflow for generating image variations at scale, moving beyond the limitations of manual UI-based creation.

AI Prompts

Please install all the missing dependencies.
Please add extensive console logging. This is going to be a script and we want to see all of the progress and everything that's happening in the terminal.
Please generate an array of ten expressions covering the wide variety of human emotions.
Please insert a formatted date to the beginning of the file name.
Please add error handling so that if one generation fails it doesn't block the entire script, and please create a way to track and report in the terminal at the end all of the images that did fail in preparation to attempt to generate them in the future.

Terminal Commands

Terminal
take expressions
Terminal
c
Terminal
bun init
Terminal
bun i @google/genai mime
Terminal
bun run index.ts

Code Snippets

Environment Variables (.env)

To securely store your API key, create a .env file in your project root.

GEMINI_API_KEY=AIzaSyDt...

Reading the Source Image

Instead of a hardcoded Base64 string, dynamically read your source image and convert it.

import { readFile, writeFile } from 'fs/promises';

// ...

const buffer = await readFile('./ai-dev.jpg');
const data = buffer.toString('base64');

Defining Expressions to Generate

Create an array of prompts to loop through. This list was generated by an AI assistant.

const expressions = [
  'expression: joy, wide smile, sparkling eyes, raised eyebrows',
  'expression: sadness, downcast eyes, furrowed brows, slight frown',
  'expression: anger, narrowed eyes, clenched jaw, furrowed brows',
  'expression: fear, wide eyes, raised eyebrows, open mouth, tense expression',
  'expression: surprise, wide eyes, raised eyebrows, open mouth, shocked expression',
  'expression: disgust, wrinkled nose, narrowed eyes, curled upper lip',
  'expression: contempt, one raised eyebrow, slight smirk, sideways glance',
  'expression: love, soft eyes, gentle smile, relaxed facial muscles',
  'expression: confusion, furrowed brows, tilted head, puzzled expression',
  'expression: excitement, wide smile, bright eyes, flushed cheeks, energetic expression'
];