Next 2026 - Image Generation with Gemini - Nano Banana

Lab 10 Minuten universal_currency_alt Keine Kosten show_chart Einsteiger
info Dieses Lab kann KI-Tools enthalten, die den Lernprozess unterstützen.
Dieser Inhalt ist noch nicht für Mobilgeräte optimiert.
Die Lernumgebung funktioniert am besten, wenn Sie auf einem Computer über einen per E‑Mail gesendeten Link darauf zugreifen.
Note: To ensure a consistent and high-performance experience, this lab may provide cached responses for some model requests. Google Cloud self-paced labs

Overview

In this lab, you explore Gemini 2.5 Flash Image (Nano Banana), Google’s state-of-the-art model for high-speed image generation, prompt-based editing, and visual reasoning.

In this lab, you write a Python script that prompts the model to generate a creative image: a cat eating a nano-banana in a fancy restaurant under the Gemini constellation.

Objectives

Google’s Generative AI, accessed through the google-genai Python SDK, gives you direct access to Gemini models so you can generate text, images, and multimodal outputs in your AI-powered applications.

In this lab, you learn how to perform the following tasks:

  • Connect to Google’s Generative AI services using the genai.Client to interact with Gemini models.
  • Load a pre-trained Image Generation Model gemini-2.5-flash-image to generate images without training your own ML model.
  • Send text prompts to the model and see how Gemini interprets natural language instructions.
  • Extract and save the generated image produced by the model.
  • Understand the basics of building AI applications using the new GenAI SDK and Python.
  • Labs are timed and cannot be paused. The timer starts when you click Start.
  • The included IDE is preconfigured with the gcloud SDK.
  • Use the terminal to execute commands and then click Check my progress to verify your work.

Working with Generative AI

After starting the lab, you will get a split pane view consisting of the Code Editor and the lab instructions. Follow these steps to interact with the Generative AI APIs using genai Python SDK.

  1. Click Explorer to access the pre-created workspace file.
Explorer Icon
  1. Select GenerateImage.py to open the file in the Code Editor.

  2. To initialize the Generative AI client and send a text prompt to the model to generate an image, copy and paste the following code into your file:

import time from google import genai from google.genai import types from PIL import Image from google.genai.types import HttpOptions, ModelContent, Part, UserContent from google.cloud import logging as gcp_logging from google.genai.errors import ClientError # ------ Below cloud logging code is for Qwiklab's internal use, do not edit/remove it. -------- # Initialize Google Cloud logging gcp_logging_client = gcp_logging.Client() gcp_logging_client.setup_logging() client = genai.Client( vertexai=True, project='{{{ project_0.project_id | "project-id" }}}', location='{{{ project_0.default_region | "REGION" }}}', http_options=HttpOptions(api_version="v1") ) prompt = ( "Create a picture of my cat eating a nano-banana in a " "fancy restaurant under the Gemini constellation", ) # Configuration for retry logic MAX_RETRIES = 3 INITIAL_DELAY = 2 for attempt in range(MAX_RETRIES + 1): try: response = client.models.generate_content( model="gemini-2.5-flash-image", contents=[prompt], ) for part in response.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: image = part.as_image() image.save("image.png") break except ClientError as e: if "429" in str(e) or "RESOURCE_EXHAUSTED" in str(e): if attempt < MAX_RETRIES: delay = INITIAL_DELAY * (2 ** attempt) print(f"Warning: Resource exhausted (429). Retrying in {delay} seconds... (Attempt {attempt + 1}/{MAX_RETRIES})") time.sleep(delay) else: print("We are experiencing high demand right now. To stay on track, please skip this step for now and continue with the next part of the lab. You can try this prompt again in a few minutes.") finally: # Verify if the process concluded without a response object being created if 'response' not in locals() and attempt == MAX_RETRIES: print("Final Status: Process terminated unsuccessfully.")
  1. Click File > Save to store your script.

  2. To send the prompt to the model and generate an image file named image.png, click the triangle icon or run the following command in the terminal:

python3 GenerateImage.py

Sample output:

Success message Note: You can ignore any warnings related to Python version dependencies.
  1. In the Explorer, click image.png to view the generated image output.
Success Image

Code Explanation

  • genai.Client: Initializes the connection to Agent Platform.
  • gemini-2.5-flash-image: This is the technical ID for the Nano Banana model.
  • generate_content: Sends your text prompt to the model's neural network.
  • Inline_data: The model returns the image as raw data, which the script converts into a viewable .png file.

Click Check my progress to verify the objective and obtain the passcode for the Skills Challenge.

Send a text prompt to Gen AI and receive an image response

Congratulations!

You have successfully connected to Google’s Generative AI services, initialized the Nano Banana model, and transformed a creative text prompt into a high-quality digital image.

Manual Last Updated April 24, 2026

Lab Last Tested April 24, 2026

Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Vorbereitung

  1. Labs erstellen ein Google Cloud-Projekt und Ressourcen für einen bestimmten Zeitraum
  2. Labs haben ein Zeitlimit und keine Pausenfunktion. Wenn Sie das Lab beenden, müssen Sie von vorne beginnen.
  3. Klicken Sie links oben auf dem Bildschirm auf Lab starten, um zu beginnen

Privates Surfen verwenden

  1. Kopieren Sie den bereitgestellten Nutzernamen und das Passwort für das Lab
  2. Klicken Sie im privaten Modus auf Konsole öffnen

In der Konsole anmelden

  1. Melden Sie sich mit Ihren Lab-Anmeldedaten an. Wenn Sie andere Anmeldedaten verwenden, kann dies zu Fehlern führen oder es fallen Kosten an.
  2. Akzeptieren Sie die Nutzungsbedingungen und überspringen Sie die Seite zur Wiederherstellung der Ressourcen
  3. Klicken Sie erst auf Lab beenden, wenn Sie das Lab abgeschlossen haben oder es neu starten möchten. Andernfalls werden Ihre bisherige Arbeit und das Projekt gelöscht.

Diese Inhalte sind derzeit nicht verfügbar

Bei Verfügbarkeit des Labs benachrichtigen wir Sie per E-Mail

Sehr gut!

Bei Verfügbarkeit kontaktieren wir Sie per E-Mail

Es ist immer nur ein Lab möglich

Bestätigen Sie, dass Sie alle vorhandenen Labs beenden und dieses Lab starten möchten

Privates Surfen für das Lab verwenden

Am besten führen Sie dieses Lab in einem Inkognito- oder privaten Browserfenster aus. So vermeiden Sie Konflikte zwischen Ihrem privaten Konto und dem Teilnehmerkonto, die zusätzliche Kosten für Ihr privates Konto verursachen könnten.