Introduction to Text Integration in Image Generation

Welcome to the final lesson of this course on creating images with Gemini and FastAPI. In previous lessons, you explored prompt crafting, quality modifiers, and photography modifiers. Now, we will focus on integrating text into generated images.

Text integration allows you to add meaningful context, branding elements, labels, slogans, or decorative typography to your images. This is useful for logos, posters, emblems, banners, and promotional graphics.

Constructing Effective Prompts for Text Placement

Creating effective prompts is crucial for guiding the AI to place text within images. When constructing prompts, consider the following guidelines:

  • Keep text short: Short text is easier to render clearly.
  • Use exact wording: Put text inside quotation marks.
  • Specify placement: Tell the model where the text should appear.
  • Describe typography: Include font style, size, color, and orientation.
  • Avoid clutter: Too many text elements may reduce readability.

Example prompt:

A circular emblem featuring a central image of a mountain. At the top arc, include the exact text 'Adventure Awaits' curved gracefully, and at the bottom arc, include the exact text 'Explore the Unknown' following the curve. The design has a vintage aesthetic with serif fonts.

This prompt gives clear guidance on:

  • image subject
  • exact text
  • text placement
  • typography
  • overall design style
Generating Images with Text Using Gemini
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 circular emblem featuring a central image of a mountain. "
    "At the top arc, include the exact text 'Adventure Awaits' curved gracefully, "
    "and at the bottom arc, include the exact text 'Explore the Unknown' following the curve. "
    "The design has a vintage aesthetic with serif fonts."
)

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",
        )
    ),
)
Processing and Displaying Generated Images in FastAPI

Once the image is generated, extract the image part and save it to the static/images folder.

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)

This saved image can then be served in a FastAPI web application using static file mounting.

Example: Creating a Vintage Emblem with Text

The example prompt guides Gemini to create a circular emblem with a central mountain and curved text. You can iterate on the prompt by adjusting:

  • text content
  • placement
  • font style
  • text color
  • orientation
  • overall composition

Text integration may require experimentation. If the text is not rendered exactly as expected, try shortening the text, increasing placement clarity, or separating text instructions into distinct sentences.

Summary and Next Steps

Congratulations on completing the course! In this lesson, you learned how to guide Gemini to generate images with text, including placement, style, color, size, and orientation.

As you move on to the practice exercises, experiment with different prompt structures and text layouts.

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