Creating Images with Gemini Image Generation and Django

Introduction to Gemini Image Generation and Django

Welcome to the first lesson of our course, "Creating Images with Gemini Image Generation and Django." In this course, you will explore the fascinating world of AI-driven image generation using Google's Gemini API and its Gemini image generation model. Our journey begins with understanding how to set up the environment and generate a simple image. We'll also touch upon Django, a powerful 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.

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 GEMINI_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:

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 GEMINI_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
import os

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.")

client = genai.Client(api_key=api_key)

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 the Gemini image generation model. 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 "A serene sunset over a mountain range." The generate_content method of the client is used to create the image. You can specify image output and the aspect ratio in the generation config. Here, we generate one image with a 16:9 aspect ratio:

from google.genai import types

prompt = "A serene sunset over a mountain range"

response = client.models.generate_content(
    model='gemini-3.1-flash-image',
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE"],
        image_config=types.ImageConfig(
            aspect_ratio="16:9",
        ),
    ),
)

When you call generate_content, the prompt and configuration parameters are sent as part of a request to the Gemini API's Gemini image generation model, which uses a large generative neural network to synthesize an image that best represents the given prompt. The result is returned as binary image data wrapped in a response object.

Gemini image generation does not use a number_of_images parameter. To create multiple images, call generate_content multiple times, as shown later in this lesson. The aspect_ratio option belongs in types.ImageConfig; supported values include 1:1 (square), 4:3 (fullscreen), 3:4 (portrait fullscreen), 16:9 (widescreen), and 9:16 (portrait). If you omit the aspect_ratio parameter, the API may use a default value. It's important to choose an appropriate aspect ratio depending on your application's design requirements—for instance, portrait modes are useful for mobile apps while 16:9 fits well in desktop or cinematic presentations.

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