You can use Imagen on Vertex AI to generate new images from a text prompt.
Supported interfaces include the Google Cloud console and the Vertex AI API.
For more information about writing text prompts for image generation and editing,
see the prompt guide.
View Imagen for Generation model card
Try image generation (Vertex AI Studio)
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI API.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI API.
Set up authentication for your environment. Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and
APIs, you don't need to set up authentication.
To use the Python samples on this page in a local
development environment, install and initialize the gcloud CLI, and
then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first
sign in to the gcloud CLI with your federated identity.
If you're using a local shell, then create local authentication credentials for your user
account:
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider
(IdP), confirm that you have
signed in to the gcloud CLI with your federated identity.
For more information, see
Set up ADC for a local development environment
in the Google Cloud authentication documentation.
To use the REST API samples on this page in a local development environment, you use the
credentials you provide to the gcloud CLI.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first
sign in to the gcloud CLI with your federated identity.
For more information, see
Authenticate for using REST
in the Google Cloud authentication documentation.
You can generate novel images using only descriptive text as an input. The
following samples show you basic instructions to generate images. In the Google Cloud console, go to the Vertex AI > Media
Studio page. Click Imagen. The Imagen Media Studio image generation page is
displayed. Optional: In the Settings pane, configure the following settings: Model: Choose a model from the available options. For more information about available models, see Imagen
models Aspect ratio: Choose an aspect ratio from the available options. Number of results: Adjust the slider or enter a value between
1 and 4. Output resolution: Choose a resolution from the available options. Optional: In the Advanced options section, select a Region to
generate your images in. In the Write your prompt box, enter your text prompt that describes
the images to generate. For example, small boat on water in the morning
watercolor illustration. For more information details about writing effective prompts, see Prompt
and image attribute
guide. Click A digital watermark is automatically added to generated images. You can't
disable digital watermark for image generation using the Google Cloud console. You can select an image to view in the Image detail window.
Watermarked images contain a verify an image watermark.
To learn more, see the
SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
In this sample you call the
For more information about
Before using any of the request data,
make the following replacements:
MODEL_VERSION: The Imagen model version
to use. For more information about available models, see
Imagen
models.
Additional optional parameters Use the following optional variables depending on your use
case. Add some or all of the following parameters in the
HTTP method and URL:
Request JSON body:
To send your request, choose one of these options:
Save the request body in a file named
Save the request body in a file named
If you use a model that supports prompt enhancement, the response includes an
additional Before you begin
Console
Python
gcloud auth application-default login
REST
Generate images with text
Console
Python
Install
pip install --upgrade google-genai
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
generate_images
method on the
ImageGenerationModel
and save generated images locally. You
then can optionally use the show()
method in a notebook to
show you the generated images. For more information on model versions and
features, see Imagen
models.REST
imagegeneration
model requests, see the
imagegeneration
model API reference.
us-central1
, europe-west2
, or asia-northeast3
. For a list
of available regions, see
Generative AI on Vertex AI locations.
imagegeneration@002
), 1-4 (all other model versions).
Default value: 4."parameters": {}
object.
This list shows common optional parameters and isn't meant to be exhaustive. For more
information about optional parameters,
see Imagen 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"
}
true
contains a digital
SynthID that you can use to verify
a watermarked image.
If you omit this field, the default value of true
is used; you must set the value
to false
to disable this feature. You can use the seed
field to get
deterministic output only when this field is set to false
.
1:1
(default, square)3:4
(Ads, social media)4:3
(TV, photography)16:9
(landscape)9:16
(portrait)true
.
true
.
"Death, Harm & Tragedy"
,
"Firearms & Weapons"
, "Hate"
, "Health"
,
"Illicit Drugs"
, "Politics"
, "Porn"
,
"Religion & Belief"
, "Toxic"
, "Violence"
,
"Vulgarity"
, "War & Conflict"
. Default value: false
.
image/jpeg
image/gif
image/png
image/webp
image/bmp
image/tiff
image/vnd.microsoft.icon
0
to 100
, where a higher number means more compression. Default:
75
.
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.
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
.
"addWatermark": false
to use this field.
Accepted integer values: 1
- 2147483647
.
gs://image-bucket/output/
.
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict
{
"instances": [
{
"prompt": "TEXT_PROMPT"
}
],
"parameters": {
"sampleCount": IMAGE_COUNT
}
}
curl
request.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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict"PowerShell
request.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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict" | Select-Object -Expand Content"sampleCount":
2
. The response returns two prediction objects, with the generated
image bytes base64-encoded.
{
"predictions": [
{
"bytesBase64Encoded": "BASE64_IMG_BYTES",
"mimeType": "image/png"
},
{
"mimeType": "image/png",
"bytesBase64Encoded": "BASE64_IMG_BYTES"
}
]
}
prompt
field with the enhanced prompt used for
generation:
{
"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"
}
]
}
What's next
Read articles about Imagen and other Generative AI on Vertex AI products:
- A developer's guide to getting started with Imagen 3 on Vertex AI
- New generative media models and tools, built with and for creators
- New in Gemini: Custom Gems and improved image generation with Imagen 3
- Google DeepMind: Imagen 3 - Our highest quality text-to-image model