Image generation API Stay organized with collections Save and categorize content based on your preferences.
The Imagen API lets you generate high quality images in seconds,using text prompt to guide the generation.
View Imagen for Generation model card
Supported model versions
Imagen API supports the following models:
imagen-4.0-generate-001imagen-4.0-fast-generate-001imagen-4.0-ultra-generate-001imagen-3.0-generate-002imagen-3.0-generate-001imagen-3.0-fast-generate-001imagen-3.0-capability-001
imagen-4.0-fast-generate-001 may generate undesireableresults if the prompt is complex and you use enhanced prompts. To fix this,setenhancePrompt tofalse.For more information about the features that each model supports, seeImagenmodels.
Example syntax
Syntax to create an image from a text prompt.
Syntax
Syntax to generate an image.
REST
curl-XPOST\-H"Authorization: Bearer$(gcloudauthprint-access-token)"\-H"Content-Type: application/json"\https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_VERSION}:predict\-d'{ "instances": [ { "prompt": "..." } ], "parameters": { "sampleCount":... }}'
Python
generation_model=ImageGenerationModel.from_pretrained("MODEL_VERSION")response=generation_model.generate_images(prompt="...",negative_prompt="...",aspect_ratio=...,)response.images[0].show()
Generate images
REST
| Parameters | |
|---|---|
addWatermark |
Optional. Add an invisible watermark to the generated images. The default value is |
aspectRatio |
Optional. The aspect ratio for the generated output image. The default value is "1:1". |
enhancePrompt |
Optional. An optional parameter to use an LLM-based prompt rewriting feature to deliver higher quality images that better reflect the original prompt's intent. Disabling this feature may impact image quality and prompt adherence. |
language |
Optional. The language code that corresponds to your text prompt language. The following values are supported:
|
negativePrompt |
Optional. A description of what to discourage in the generated images.
|
outputOptions |
Optional. Describes the output image format in an |
prompt |
Required. The text prompt for the image. |
personGeneration |
Optional. Allow generation of people by the model. The following values are supported:
The default value is |
safetySetting |
Optional. Adds a filter level to safety filtering. The following values are supported:
The default value is |
sampleCount |
Required. The number of images to generate. The default value is 4. |
sampleImageSize |
Optional. Specifies the generated image's output resolution. The accepted values are |
seed |
Optional. The random seed for image generation. This isn't available when If |
storageUri | Optional: Cloud Storage URI to store the generated images. |
Output options object
TheoutputOptions object describes the image output.
| Parameters | |
|---|---|
outputOptions.mimeType | Optional: The image format that the output should be saved as. The following values are supported:
The default value is |
outputOptions.compressionQuality | Optional: The level of compression if the output type is |
Response
The response body from the REST request.
| Parameter | |
|---|---|
predictions | An array ofVisionGenerativeModelResult objects, one for each requestedsampleCount. If any images are filtered by responsible AI, they are not included, unlessincludeRaiReason is set totrue. |
Vision generative model result object
Information about the model result.
| Parameter | |
|---|---|
bytesBase64Encoded | The base64 encoded generated image. Not present if the output image did not pass responsible AI filters. |
mimeType | The type of the generated image. Not present if the output image did not pass responsible AI filters. |
raiFilteredReason | The responsible AI filter reason. Only returned if |
safetyAttributes.categories | The safety attribute name. Only returned if |
safetyAttributes.scores | The safety attribute score. Only returned if |
Python
| Parameters | |
|---|---|
add_watermark |
Optional. Add a watermark to the generated image. The default value is |
aspect_ratio |
Optional. The aspect ratio for the generated output image. The default value is "1:1". |
compression_quality |
Optional. The level of compression if the output mime type is |
language |
Optional. The language of the text prompt for the image. The following values are supported:
The default value is |
negative_prompt |
Optional. A description of what to discourage in the generated images.
|
number_of_images |
Required. The number of images to generate. The default value is 1. |
output_gcs_uri |
Optional. Cloud Storage URI to store the generated images. |
output_mime_type |
Optional. The image format that the output should be saved as. The following values are supported:
The default value is |
prompt |
Required. The text prompt for the image. |
person_generation |
Optional. Allow generation of people by the model. The following values are supported:
The default value is |
safety_filter_level |
Optional. Adds a filter level to safety filtering. The following values are supported:
The default value is |
sample_image_size |
Optional. Specifies the generated image's output resolution. The accepted values are |
seed |
Optional. The random seed for image generation. This isn't available when If |
Examples
The following examples show how to use the Imagen modelsto generate images.
Generate images
REST
Before using any of the request data, make the following replacements:
- REGION: The region that your project is located in. For more information about supported regions, seeGenerative AI on Vertex AI locations.
- PROJECT_ID: Your Google Cloudproject ID.
MODEL_VERSION: The Imagen model version to use. For more information about available models, seeImagen models.
- TEXT_PROMPT: The text prompt that guides what images the model generates. This field is required for both generation and editing.
- IMAGE_COUNT: The number of images to generate. The accepted range of values is
1to4. - ADD_WATERMARK: boolean. Optional. Whether to enable a watermark for generated images. Any image generated when the field is set to
truecontains a digitalSynthID that you can use to verify a watermarked image. If you omit this field, the default value oftrueis used; you must set the value tofalseto disable this feature. You can use theseedfield to get deterministic output only when this field is set tofalse. - ASPECT_RATIO: string. Optional. A generation mode parameter that controls aspect ratio. Supported ratio values and their intended use:
1:1(default, square)3:4(Ads, social media)4:3(TV, photography)16:9(landscape)9:16(portrait)
- ENABLE_PROMPT_REWRITING: boolean. Optional. A parameter to use an LLM-based prompt rewriting feature to deliver higher quality images that better reflect the original prompt's intent. Disabling this feature may impact image quality and prompt adherence. Default value:
true. - INCLUDE_RAI_REASON: boolean. Optional. Whether to enable theResponsible AI filtered reason code in responses with blocked input or output. Default value:
true. - INCLUDE_SAFETY_ATTRIBUTES: boolean. Optional. Whether to enable rounded Responsible AI scores for a list of safety attributes in responses for unfiltered input and output. Safety attribute categories:
"Death, Harm & Tragedy","Firearms & Weapons","Hate","Health","Illicit Drugs","Politics","Porn","Religion & Belief","Toxic","Violence","Vulgarity","War & Conflict". Default value:false. - MIME_TYPE: string. Optional. The MIME type of the content of the image. Available values:
image/jpegimage/gifimage/pngimage/webpimage/bmpimage/tiffimage/vnd.microsoft.icon
- COMPRESSION_QUALITY: integer. Optional. Only applies to JPEG output files. The level of detail the model preserves for images generated in JPEG file format. Values:
0to100, where a higher number means more compression. Default:75. - PERSON_SETTING: string. Optional. The safety setting that controls the type of people or face generation the model allows. Available values:
allow_adult(default): Allow generation of adults only, except for celebrity generation. Celebrity generation is not allowed for any setting.dont_allow: Disable the inclusion of people or faces in generated images.
- SAFETY_SETTING: string. Optional. A setting that controls safety filter thresholds for generated images. Available values:
block_low_and_above: The highest safety threshold, resulting in the largest amount of generated images that are filtered. Previous value:block_most.block_medium_and_above(default): A medium safety threshold that balances filtering for potentially harmful and safe content. Previous value:block_some.block_only_high: A safety threshold that reduces the number of requests blocked due to safety filters. This setting might increase objectionable content generated by Imagen. Previous value:block_few.
- SEED_NUMBER: integer. Optional. Any non-negative integer you provide to make output images deterministic. Providing the same seed number always results in the same output images. If the model you're using supports digital watermarking, you must set
"addWatermark": falseto use this field. Accepted integer values:1-2147483647. - OUTPUT_STORAGE_URI: string. Optional. The Cloud Storage bucket to store the output images. If not provided, base64-encoded image bytes are returned in the response. Sample value:
gs://image-bucket/output/.
Additional optional parameters
Use the following optional variables depending on your use case. Add some or all of the following parameters in the"parameters": {} object. This list shows common optional parameters and isn't meant to be exhaustive. For more information about optional parameters, seeImagen API reference: Generate images.
"parameters": { "sampleCount":IMAGE_COUNT, "addWatermark":ADD_WATERMARK, "aspectRatio": "ASPECT_RATIO", "enhancePrompt":ENABLE_PROMPT_REWRITING, "includeRaiReason":INCLUDE_RAI_REASON, "includeSafetyAttributes":INCLUDE_SAFETY_ATTRIBUTES, "outputOptions": { "mimeType": "MIME_TYPE", "compressionQuality":COMPRESSION_QUALITY }, "personGeneration": "PERSON_SETTING", "safetySetting": "SAFETY_SETTING", "seed":SEED_NUMBER, "storageUri": "OUTPUT_STORAGE_URI"}HTTP method and URL:
POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/MODEL_VERSION:predict
Request JSON body:
{ "instances": [ { "prompt": "TEXT_PROMPT" } ], "parameters": { "sampleCount":IMAGE_COUNT }}To send your request, choose one of these options:
curl
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/MODEL_VERSION:predict"
PowerShell
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/MODEL_VERSION:predict" | Select-Object -Expand Content
"sampleCount": 2. The response returns two prediction objects, with the generatedimage bytes base64-encoded.{"predictions":[{"bytesBase64Encoded":"BASE64_IMG_BYTES","mimeType":"image/png"},{"mimeType":"image/png","bytesBase64Encoded":"BASE64_IMG_BYTES"}]}
If you use a model that supports prompt enhancement, the response includes anadditionalprompt field with the enhanced prompt used forgeneration:
{ "predictions": [ { "mimeType": "MIME_TYPE", "prompt": "ENHANCED_PROMPT_1", "bytesBase64Encoded": "BASE64_IMG_BYTES_1" }, { "mimeType": "MIME_TYPE", "prompt": "ENHANCED_PROMPT_2", "bytesBase64Encoded": "BASE64_IMG_BYTES_2" } ]}Python
Before trying this sample, follow thePython setup instructions in theVertex AI quickstart using client libraries. For more information, see theVertex AIPython API reference documentation.
To authenticate to Vertex AI, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
In this sample you call thegenerate_images method on theImageGenerationModel (@006 version) and save generatedimages locally. You then can optionally use theshow()method in a notebook to show you the generated images. For more information onmodel versions and features, seemodel versions.
importvertexaifromvertexai.preview.vision_modelsimportImageGenerationModel# TODO(developer): Update and un-comment below lines# PROJECT_ID = "your-project-id"# output_file = "input-image.png"# prompt = "" # The text prompt describing what you want to see.vertexai.init(project=PROJECT_ID,location="us-central1")model=ImageGenerationModel.from_pretrained("imagen-3.0-generate-002")images=model.generate_images(prompt=prompt,# Optional parametersnumber_of_images=1,language="en",# You can't use a seed value and watermark at the same time.# add_watermark=False,# seed=100,aspect_ratio="1:1",safety_filter_level="block_some",person_generation="allow_adult",)images[0].save(location=output_file,include_generation_parameters=False)# Optional. View the generated image in a notebook.# images[0].show()print(f"Created output image using{len(images[0]._image_bytes)} bytes")# Example response:# Created output image using 1234567 bytesWhat's next
- For more information, seeImagen on Vertex AIoverview andGenerate images using text prompts.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-17 UTC.