Introduction to Text Integration in Image Generation with Python

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:

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