Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Googleapis Python genai Models Generate Images

From Leeroopedia
Knowledge Sources
Domains Computer_Vision, Generative_AI
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for generating images from text prompts using Imagen models provided by the google-genai models module.

Description

Models.generate_images sends a text prompt to an Imagen model and returns generated images. The method accepts a model identifier (e.g., imagen-4.0-generate-001), a text prompt, and optional configuration including number of images, aspect ratio, negative prompt, safety filter level, and output format. It returns a GenerateImagesResponse containing a list of GeneratedImage objects, each with an Image that can be displayed or saved.

Usage

Call client.models.generate_images with a descriptive text prompt and the appropriate Imagen model. Use config to control output parameters. Access generated images via response.generated_images[i].image.

Code Reference

Source Location

Signature

class Models:
    def generate_images(
        self,
        *,
        model: str,
        prompt: str,
        config: Optional[types.GenerateImagesConfigOrDict] = None,
    ) -> types.GenerateImagesResponse:
        """Generates images from a text prompt.

        Args:
            model: Imagen model ID (e.g., 'imagen-4.0-generate-001').
            prompt: Text description of the image to generate.
            config: Optional image generation configuration.
        """

Import

from google import genai

I/O Contract

Inputs

Name Type Required Description
model str Yes Imagen model ID (e.g., 'imagen-4.0-generate-001')
prompt str Yes Text description of the desired image
config.number_of_images Optional[int] No Number of images to generate
config.aspect_ratio Optional[str] No Aspect ratio (e.g., '1:1', '16:9', '9:16')
config.negative_prompt Optional[str] No Description of elements to exclude
config.safety_filter_level Optional[SafetyFilterLevel] No Safety filtering strictness

Outputs

Name Type Description
GenerateImagesResponse GenerateImagesResponse Response with .generated_images list of GeneratedImage objects
GeneratedImage.image Image Image object with .show(), .save(), .image_bytes

Usage Examples

Generate an Image

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_API_KEY")

response = client.models.generate_images(
    model="imagen-4.0-generate-001",
    prompt="A serene mountain lake at sunset with snow-capped peaks",
    config=types.GenerateImagesConfig(
        number_of_images=4,
        aspect_ratio="16:9",
    ),
)

# Display or save images
for i, gen_image in enumerate(response.generated_images):
    gen_image.image.save(f"mountain_lake_{i}.png")
    gen_image.image.show()  # Display in notebook

With Negative Prompt

response = client.models.generate_images(
    model="imagen-4.0-generate-001",
    prompt="A professional headshot photo, studio lighting",
    config=types.GenerateImagesConfig(
        number_of_images=1,
        negative_prompt="blurry, low quality, cartoon",
    ),
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment