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
To learn more about prompt writing for Imagen, see the Imagen prompt guide.
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
client = genai.Client(api_key='GEMINI_API_KEY')
response = client.models.generate_image(
model='imagen-3.0-generate-002',
prompt='Fuzzy bunnies in my kitchen',
config=types.GenerateImageConfig(
negative_prompt= 'people',
number_of_images= 4,
include_rai_reason= True,
output_mime_type= 'image/jpeg'
)
)
response.generated_images[0].image.show()
The code sample should display four images similar to this one:
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 isLOW
,MEDIUM
, orHIGH
."BLOCK_MEDIUM_AND_ABOVE"
: Block when the probability score or the severity score isMEDIUM
orHIGH
."BLOCK_ONLY_HIGH"
: Block when the probability score or the severity score isHIGH
.
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.
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
Imagen 3 in Gemini API is in early access. Stay tuned for announcements about the status of the feature.