Introduction to Gemini Image Generation with Python

Welcome to the first lesson of our course, "Generating a Simple Image with Gemini and FastAPI". In this course, you will explore the fascinating world of AI image generation using Python and Google's Gemini API.

Our journey begins with learning how to set up the environment, configure the Gemini client, generate an image from a text prompt, and save the image to a local folder. We'll use the google-genai SDK to communicate with Gemini and Pillow behind the scenes through Gemini's part.as_image() helper to work with generated image data.

This foundational lesson will prepare you for more advanced image generation topics in later units, including prompt refinement, styles, photography modifiers, and text placement.

Setting Up the Environment

Before we generate images, we need to set up our environment. First, ensure you have access to the Gemini API and have retrieved your API key. This key authenticates your requests to the API. In this course, we will read the key from an environment variable named GEMINI_API_KEY.

pip install google-genai pillow

On CodeSignal, many libraries may already be installed, but it's still important to know which dependencies your project needs when running locally.

Configuring the Gemini API Client

With the environment ready, the next step is configuring the Gemini API client. The code below reads the API key and base URL from environment variables, validates that they exist, and initializes the Gemini client.

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

# Model ID for Gemini 3 image generation
GEMINI_IMAGE_MODEL = "gemini-3.1-flash-image"

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

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

base_url = os.getenv("GEMINI_BASE_URL")

if not base_url:
    raise ValueError("GEMINI_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.

Generating a Simple Image

Now, let's generate a simple image using Gemini. We'll start by defining a prompt, which is a textual description of the image you want to create.

In this example, the prompt is:

prompt = "A serene sunset over a mountain range"

To generate the image, we use client.models.generate_content(...). Unlike Imagen-specific examples that use generate_images(...), Gemini native image generation uses the content generation API and returns images as inline data parts.

response = client.models.generate_content(
    model=GEMINI_IMAGE_MODEL,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE"],
        image_config=types.ImageConfig(
            aspect_ratio="16:9",
        )
    ),
)

The model parameter specifies the Gemini image generation model. In this course, we use:

GEMINI_IMAGE_MODEL = "gemini-3.1-flash-image"

We also pass response_modalities=["IMAGE"] to ensure the API knows we expect an image back. The image_config parameter lets you control generation settings such as:

  • aspect_ratio: The shape of the generated image, such as "1:1" or "16:9".
Processing and Saving the Generated Image

Once the image is generated, we need to extract the image part from the response. Gemini responses may contain multiple parts, including text and image data. We filter for parts with inline_data, convert the first image part to a PIL image using as_image(), and save it.

image_parts = [part for part in response.parts if part.inline_data]

if not image_parts:
    raise RuntimeError("No image returned from Gemini")

image = image_parts[0].as_image()

output_path = os.path.join("static", "images", "generated_image.png")
os.makedirs(os.path.dirname(output_path), exist_ok=True)
image.save(output_path)

The os.makedirs(...) call ensures the output folder exists before saving the file.

Complete Example
from google import genai
from google.genai import types
import os

GEMINI_IMAGE_MODEL = "gemini-3.1-flash-image"

api_key = os.getenv("GEMINI_API_KEY")

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

base_url = os.getenv("GEMINI_BASE_URL")

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

client = genai.Client(
    api_key=api_key,
    http_options=types.HttpOptions(
        base_url=base_url,
    ),
)

prompt = "A serene sunset over a mountain range"

response = client.models.generate_content(
    model=GEMINI_IMAGE_MODEL,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE"],
        image_config=types.ImageConfig(
            aspect_ratio="16:9",
        )
    ),
)

image_parts = [part for part in response.parts if part.inline_data]

if not image_parts:
    raise RuntimeError("No image returned from Gemini")

image = image_parts[0].as_image()

output_path = os.path.join("static", "images", "generated_image.png")
os.makedirs(os.path.dirname(output_path), exist_ok=True)
image.save(output_path)
Summary and Next Steps

In this lesson, you learned how to set up your environment, configure the Gemini API client, generate an image from a prompt, and save the generated image. This foundational knowledge will be important as you progress through the course.

As you move on to the practice exercises, experiment with different prompts and image configurations to see how they affect the generated output.

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