Crafting Effective Prompts for Gemini Image Generation

Introduction to Crafting Effective Prompts

Welcome back! In the previous lesson, you learned how to generate a simple image using the Gemini API and Gemini. Now, we will delve deeper into the art of crafting effective prompts to achieve desired image outputs.

A well-written prompt directly influences the quality, clarity, and relevance of the generated image. In this lesson, we will explore the key components of a prompt:

  • subject
  • context
  • style

Understanding these components will help you create more detailed and specific prompts, leading to more accurate and visually appealing images.

Understanding Prompt Components

A prompt is a textual description that guides the image generation process.

The subject is the primary focus of the image, such as a cat, a robot, or a landscape.

The context provides additional details about the setting or environment, such as a bustling city at night or a quiet forest.

The style defines the artistic approach, such as digital art, watercolor painting, realistic photography, or sketch.

For example, a simple prompt like:

A cat

may generate a generic image of a cat.

A more detailed prompt like:

A black cat sitting on a windowsill overlooking a bustling city at night, in the style of digital art

gives Gemini more direction, resulting in a richer and more specific image.

Example: Crafting and Testing Prompts

Let's walk through an example showing how different prompts affect generated images.

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

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,
    ),
)

prompts = [
    "A cat",
    "A black cat sitting on a windowsill",
    "A black cat sitting on a windowsill overlooking a bustling city at night, in the style of digital art",
    "A close-up of a black cat sitting on a windowsill overlooking a bustling city at night, in the style of digital art",
]

for index, prompt in enumerate(prompts):
    response = client.models.generate_content(
        model=GEMINI_IMAGE_MODEL,
        contents=prompt,
        config=types.GenerateContentConfig(
            response_modalities=["IMAGE"],
            image_config=types.ImageConfig(
                aspect_ratio="1:1",
            )
        ),
    )

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

    if not image_parts:
        print(f"No image generated for prompt: {prompt}")
        continue

    image = image_parts[0].as_image()
    timestamp = int(time.time() * 1000)
    image_filename = f"image_{timestamp}_{index}.png"
    output_path = os.path.join("static", "images", image_filename)
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    image.save(output_path)

The code iterates over each prompt, generates an image for each one, and saves the output with a unique filename. The level of detail in each prompt directly affects the complexity and specificity of the generated image.

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