Generating a Simple Image with Gemini's Nano Banana and Python

Introduction to Gemini's Image Generation and Python

Welcome to the first lesson of our course, "Creating Images with Gemini's Nano Banana and Python". In this course, you will explore the fascinating world of AI-driven image generation using Google's Gemini API and its native image generation models, known as Nano Banana. Our journey begins with understanding how to set up the environment and generate a simple image. We'll also touch upon Python, a lightweight web framework that will help us integrate and display our generated images. This foundational lesson will set the stage for more advanced topics in subsequent units.

A quick note on naming: Google's older standalone Imagen models have been deprecated, and image generation is now unified directly inside the Gemini models. Throughout this course we will use gemini-3.1-flash-image (also called Nano Banana 2), which is optimized for fast, high-volume generation. A higher-end sibling, gemini-3-pro-image (Nano Banana Pro), exists for professional asset production, and you can swap the model string at any point to experiment with it.

Setting Up the Environment

Before we dive into generating images, it's crucial to set up our environment correctly. First, ensure you have access to the Gemini API by retrieving your API key. This key is essential for authenticating your requests to the API. You can set this key as an environment variable named GOOGLE_API_KEY. On CodeSignal, many libraries come pre-installed, but it's good practice to know how to install them on your own device. For this lesson, you'll need the google-genai library for accessing the Gemini API and the PIL library for image processing. You can install these using pip:

Shell
pip install google-genai pillow

Configuring the Gemini API Client

With the environment set up, the next step is to configure the Gemini API client. This involves initializing the client with your API key. The API key is retrieved from the environment variable GOOGLE_API_KEY. If the key is not found, the script will raise an error, prompting you to set it before proceeding. Here's how you can initialize the client:

from google import genai
from google.genai import types
import os

# Retrieve API key from system environment variable
api_key = os.getenv("GOOGLE_API_KEY")

if not api_key:
    raise ValueError("GOOGLE_API_KEY not found in environment variables. Set it before running the script.")

base_url = os.getenv("GOOGLE_BASE_URL")

if not base_url:
    raise ValueError("GOOGLE_BASE_URL not found in environment variables. Set it before running the script.")

# Initialize the Gemini client
client = genai.Client(
    api_key=api_key,
    http_options=types.HttpOptions(
        base_url=base_url,
    ),
)

This setup ensures that your application can securely communicate with the Gemini API.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal