Method Signature
def generate_images (
self ,
* ,
model : str ,
prompt : str ,
config : Optional[GenerateImagesConfig] = None ,
) -> GenerateImagesResponse
async def generate_images (
self ,
* ,
model : str ,
prompt : str ,
config : Optional[GenerateImagesConfig] = None ,
) -> GenerateImagesResponse
Description
Generates images based on a text description using Imagen models. Supports various image generation parameters including aspect ratio, safety filters, and output format control.
Parameters
The Imagen model to use for generation. Examples:
'imagen-3.0-generate-002'
'imagen-3.0-fast-generate-001'
'imagen-3.0-generate-001'
A text description of the images to generate. Example: 'A serene mountain landscape at sunset with a lake in the foreground'
Configuration for image generation. Number of images to generate (default: 1, max: 4)
Aspect ratio of generated images. Options:
'1:1' - Square (default)
'16:9' - Landscape
'9:16' - Portrait
'4:3'
'3:4'
Text describing what to avoid in the image. Example: 'blurry, low quality, distorted' Vertex AI only
Random seed for reproducible generation. Vertex AI only
How closely to follow the prompt (1.0 to 20.0). Higher values = more faithful to prompt.
Safety filter strictness level. Options:
'BLOCK_LOW_AND_ABOVE'
'BLOCK_MEDIUM_AND_ABOVE'
'BLOCK_ONLY_HIGH'
'BLOCK_NONE' (Vertex AI only)
Policy for generating images of people. Options:
'DONT_ALLOW' - Don’t generate people
'ALLOW_ADULT' - Allow adult depictions only
'ALLOW_ALL' (Vertex AI only)
include_safety_attributes
Include safety classification in response (default: false)
Include RAI (Responsible AI) filtering reasons (default: false)
Language code for prompt interpretation (e.g., 'en', 'es', 'ja')
Output image format. Options:
'image/jpeg'
'image/png' (default)
output_compression_quality
JPEG compression quality (1-100). Only applies when output_mime_type is 'image/jpeg'.
Add a watermark to generated images (default: true) Vertex AI only
Image dimensions. Examples: '1024', '512'
Automatically enhance the prompt for better results Vertex AI only
Google Cloud Storage URI to save generated images Example: 'gs://my-bucket/images/' Vertex AI only
Labels to attach to the generation request Vertex AI only
Response
List of generated images. Show GeneratedImage properties
The generated image object with methods:
.show() - Display the image
.save(path) - Save to file
.to_pil() - Convert to PIL Image
.uri - GCS URI if saved to cloud storage
.image_bytes - Raw image bytes
Reason if image was filtered by Responsible AI filters
Safety classifications (if include_safety_attributes=True) Safety scores for different categories
Whether image was blocked
The enhanced prompt used (if enhance_prompt=True) Vertex AI only
positive_prompt_safety_attributes
Safety attributes for the prompt itself
Code Examples
Basic Image Generation
from google import genai
client = genai.Client( vertexai = True , project = 'my-project' , location = 'us-central1' )
response = client.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'A majestic mountain landscape at sunset'
)
# Display the image
response.generated_images[ 0 ].image.show()
# Save the image
response.generated_images[ 0 ].image.save( 'mountain.png' )
Multiple Images with Custom Settings
from google.genai import types
response = client.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'A cute robot playing with a cat' ,
config = types.GenerateImagesConfig(
number_of_images = 4 ,
aspect_ratio = '16:9' ,
guidance_scale = 15.0 ,
include_rai_reason = True ,
)
)
# Process all generated images
for i, gen_image in enumerate (response.generated_images):
gen_image.image.save( f 'robot_cat_ { i } .png' )
if gen_image.rai_filtered_reason:
print ( f "Image { i } filtered: { gen_image.rai_filtered_reason } " )
With Negative Prompt and Safety Settings
response = client.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'A photorealistic portrait of a person smiling' ,
config = types.GenerateImagesConfig(
negative_prompt = 'blurry, distorted, low quality, cartoon' ,
person_generation = 'ALLOW_ADULT' ,
safety_filter_level = 'BLOCK_MEDIUM_AND_ABOVE' ,
include_safety_attributes = True ,
)
)
if response.generated_images:
print ( f "Safety attributes: { response.generated_images[ 0 ].safety_attributes } " )
response.generated_images[ 0 ].image.show()
Save to Cloud Storage
response = client.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'An abstract geometric pattern' ,
config = types.GenerateImagesConfig(
number_of_images = 2 ,
output_gcs_uri = 'gs://my-bucket/images/' ,
output_mime_type = 'image/jpeg' ,
output_compression_quality = 90 ,
)
)
for gen_image in response.generated_images:
print ( f "Image saved to: { gen_image.image.uri } " )
High-Quality Output Settings
response = client.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'A detailed illustration of a steampunk city' ,
config = types.GenerateImagesConfig(
aspect_ratio = '16:9' ,
guidance_scale = 18.0 ,
image_size = '1024' ,
output_mime_type = 'image/png' ,
enhance_prompt = True ,
seed = 42 , # For reproducibility
)
)
print ( f "Enhanced prompt: { response.generated_images[ 0 ].enhanced_prompt } " )
response.generated_images[ 0 ].image.show()
Async Usage
import asyncio
from google import genai
client = genai.Client( vertexai = True , project = 'my-project' , location = 'us-central1' )
async def generate ():
response = await client.aio.models.generate_images(
model = 'imagen-3.0-generate-002' ,
prompt = 'A futuristic cityscape at night' ,
config = { 'number_of_images' : 2 }
)
for i, gen_image in enumerate (response.generated_images):
gen_image.image.save( f 'city_ { i } .png' )
asyncio.run(generate())
Notes
Generation typically takes 5-30 seconds depending on the model and settings
Some configuration options are only available on Vertex AI
Images may be filtered by safety systems if they violate content policies
Use include_rai_reason=True to understand why images were filtered
The enhance_prompt feature can significantly improve results but may alter your intent
Higher guidance_scale values make the model follow the prompt more closely but may reduce creativity
See the Imagen guide for more information on editing and upscaling images with edit_image and upscale_image methods.