Nano Banana 🍌: Gemini 2.5 Flash Image Recipes

Source notebook

Repo path: gemini/nano-banana/nano_banana_recipes.ipynb · Open on GitHub · intermediate

Demonstrates Gemini 2.5 Flash image generation and editing recipes on Vertex AI with google-genai.

Summary

This notebook teaches practical Gemini image generation and editing workflows using the Google GenAI SDK on Vertex AI. It sets up a Vertex AI client, defines reusable image generation helpers, then walks through recipes for text-to-image, aspect ratio control, outpainting, editing, style transfer, restoration, references, try-on, product scenes, text rendering, character consistency, and perspective shifts.

Key code patterns

Vertex AI GenAI client

from google import genai
 
client = genai.Client(
    vertexai=True,
    project=PROJECT_ID,
    location="global",
)

Initializes google-genai for Vertex AI using a Google Cloud project and global location.

Image generation config

from google.genai import types
 
GENERATION_CONFIG = types.GenerateContentConfig(
    temperature=1,
    top_p=0.95,
    max_output_tokens=32768,
    response_modalities=["TEXT", "IMAGE"],
)

Requests both text and image modalities with explicit sampling and token settings.

Blank canvas aspect ratio

image = Image.new("RGB", (1280, 720), "white")
buffer = io.BytesIO()
image.save(buffer, format="PNG")
canvas = types.Part.from_bytes(
    data=buffer.getvalue(),
    mime_type="image/png",
)

Uses a provided canvas to guide generated output toward a target aspect ratio.

Generate and display image

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=contents,
    config=GENERATION_CONFIG,
)
for part in response.candidates[0].content.parts:
    if part.inline_data and part.inline_data.data:
        display(Image.open(io.BytesIO(part.inline_data.data)))

Extracts returned inline image bytes from the model response and displays them.

Reference image input

source_image = types.Part.from_uri(file_uri=image_url)
contents = [
    types.Content(
        role="user",
        parts=[source_image, types.Part.from_text(text=prompt)],
    )
]

Combines image URI parts with text prompts for editing, restoration, and perspective tasks.

Models & APIs used

When to use this

Use this pattern when building Vertex AI workflows for prompt-driven image generation, image editing, and reference-based visual transformations with Gemini.

Gotchas & caveats

  • Requires a Google Cloud project with the Vertex AI API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be supplied or available as GOOGLE_CLOUD_PROJECT.
  • The notebook uses LOCATION = “global”.
  • Custom canvas aspect ratio requires both width and height.

Best practices

  • Defines shared model and generation configuration before running recipes.
  • Uses helper functions to create canvases and display generated images consistently.
  • Checks response candidates and inline image data before displaying output.
  • Uses blank canvases to guide aspect ratio-sensitive outputs.
  • Uses source and reference images with text prompts for grounded image edits.