Creating Images with Text in Gemini Using Python

Introduction to Text Integration in Image Generation

Welcome to the final lesson of this course on creating images with Gemini's Nano Banana and Python. In previous lessons, you explored various aspects of image generation, including crafting effective prompts and using photography modifiers. Now, we will focus on integrating text into your images, a powerful feature that can enhance the visual storytelling of your creations. Text integration allows you to add meaningful context or branding elements to your images, making them more engaging and informative.

Text rendering is an area where the Gemini image models particularly shine compared to the older Imagen generation — they are designed to produce legible, stylized text for infographics, logos, menus, and marketing assets. In this lesson, you will learn how to construct prompts that guide the AI to place text within images effectively. We will also cover how to generate these images using Gemini's image model and display them using Python. By the end of this lesson, you will be equipped to create images with text that can be used for various applications, such as logos, posters, or digital art.

Constructing Effective Prompts for Text Placement

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

  • Character Limits: Keep text short, ideally 25 characters or less, to ensure clarity and readability.
  • Multiple Phrases: Use up to three distinct phrases to provide additional information without cluttering the image.
  • Text Placement: Specify where you want the text to appear, such as "at the top arc" or "at the bottom arc."

Let's break down an example prompt:

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

This prompt provides clear guidance on text placement and style, helping the AI generate an image that meets your expectations.

Generating Images with Text Using Gemini

Now that you understand how to construct prompts, let's generate an image with text using Gemini's image model. Here's a step-by-step walkthrough of the code:

from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import os

# Retrieve API key from system environment variable
api_key = os.getenv("GOOGLE_API_KEY")

if not api_key:
    raise ValueError("GOOGLE_API_KEY not found in environment variables. Set it before running the script.")

base_url = os.getenv("GOOGLE_BASE_URL")

if not base_url:
    raise ValueError("GOOGLE_BASE_URL not found in environment variables. Set it before running the script.")

# Initialize the Gemini client
client = genai.Client(
    api_key=api_key,
    http_options=types.HttpOptions(
        base_url=base_url,
    ),
)

# Define the prompt with text placement guidance
prompt = (
    "A circular emblem featuring a central image of a mountain. "
    "At the top arc, the text 'Adventure Awaits' is curved gracefully, "
    "and at the bottom arc, the text 'Explore the Unknown' follows the curve. "
    "The design has a vintage aesthetic with serif fonts."
)

# Generate the image
response = client.models.generate_content(
    model="gemini-3.1-flash-image",
    contents=[prompt],
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
    ),
)

In this code, we initialize the Gemini client with your API key and define a prompt that includes text placement instructions. The generate_content method is used to request an image from the model, specifying the model name, the prompt inside contents, and a configuration that asks for an image back through response_modalities.

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