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.

Iterating and Refining Prompts

Crafting effective prompts is an iterative process. Start with a simple idea, generate an image, review the output, and then refine the prompt.

For example:

A black cat

can become:

A black cat with green eyes sitting on a windowsill

and then:

A black cat with green eyes sitting on a windowsill overlooking a rainy cyberpunk city at night, neon reflections on the glass, digital art style

Each refinement gives the model more information about what you want.

Common Image Styles

When using Gemini image generation, you can guide the visual look by including style descriptors such as:

  • Digital Art: Polished, vibrant, modern visuals.
  • Realistic Photography: Detailed images that resemble real photos.
  • Watercolor Painting: Soft, fluid, traditional painting effects.
  • Pixel Art: Retro, blocky, game-like visuals.
  • Anime Style: Clean lines, expressive characters, stylized environments.
  • Concept Art: Exploratory visuals often used for games, films, or storytelling.
Summary and Preparation for Practice Exercises

In this lesson, we explored the importance of crafting effective prompts for image generation. We discussed the key components of a prompt — subject, context, and style — and how they influence the generated image.

As you move on to the practice exercises, experiment with different prompts and styles. Observe how each change affects the final 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