Imagen 3 in the Gemini API

The Gemini API provides access to Imagen 3, Google's highest quality text-to-image model, featuring a number of new and improved capabilities. Imagen 3 can do the following:

  • Generate images with better detail, richer lighting, and fewer distracting artifacts than previous models
  • Understand prompts written in natural language
  • Generate images in a wide range of formats and styles
  • Render text more effectively than previous models

Generate images

This section shows you how to instantiate an Imagen model and generate images.

After you install the Google Gen AI SDK, you can use the following code to generate images:

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

client = genai.Client(api_key='GEMINI_API_KEY')

response = client.models.generate_images(
    model='imagen-3.0-generate-002',
    prompt='Fuzzy bunnies in my kitchen',
    config=types.GenerateImagesConfig(
        number_of_images= 4,
    )
)
for generated_image in response.generated_images:
  image = Image.open(BytesIO(generated_image.image.image_bytes))
  image.show()

The code sample should output four images similar to this one:

AI-generated image of two fuzzy bunnies in the kitchen

Python developers can also try out the Getting Started with Imagen notebook in the Gemini Cookbook.

Imagen model parameters

The following parameters are available for generate_images():

  • prompt: The text prompt for the image.
  • number_of_images: The number of images to generate, from 1 to 4 (inclusive). The default is 4.
  • aspect_ratio: Changes the aspect ratio of the generated image. Supported values are "1:1", "3:4", "4:3", "9:16", and "16:9". The default is "1:1".
  • safety_filter_level: Adds a filter level to safety filtering. The following values are valid:
    • "BLOCK_LOW_AND_ABOVE": Block when the probability score or the severity score is LOW, MEDIUM, or HIGH.
    • "BLOCK_MEDIUM_AND_ABOVE": Block when the probability score or the severity score is MEDIUM or HIGH.
    • "BLOCK_ONLY_HIGH": Block when the probability score or the severity score is HIGH.
  • person_generation: Allow the model to generate images of people. The following values are supported:
    • "DONT_ALLOW": Block generation of images of people.
    • "ALLOW_ADULT": Generate images of adults, but not children. This is the default.

A non-visible digital SynthID watermark is always added to generated images.

Text prompt language

The following input text prompt languages are supported:

  • English (en)

What's next

To learn more about prompt writing for Imagen, see the Imagen prompt guide.