Generate images using text prompts

You can use Imagen on Vertex AI to generate new images from a text prompt you provide in the Google Cloud console or send in a request to the Vertex AI API .

For more information about writing text prompts for image generation and editing, see the prompt guide.

Locations

A location is a region you can specify in a request to control where data is stored at rest. For a list of available regions, see Generative AI on Vertex AI locations.

Safety filtering

Both input data and output content are checked for offensive material when you send an image generation request to Imagen. This means a text prompt input that's offensive can be blocked. Similarly, offensive output images might also be blocked, affecting the number of generated images you get in a response.

For more information about safety filtering and blocked content handling, see Responsible AI and usage guidelines for Imagen.

Performance and limitations

The following limits apply when you use an Imagen model for image generation:

Limits Value
(Imagen 3)
Maximum number of API requests per minute per project Imagen 3: 20

Imagen 3 Fast: 200
Maximum number of images returned per request (text-to-image generation) 4
Maximum image size uploaded or sent in a request (MB) 10 MB
Supported returned image resolution (pixels)
  • 1024x1024 pixels (1:1 aspect ratio)
  • 896x1280 (3:4 aspect ratio)
  • 1280x896 (4:3 aspect ratio)
  • 768x1408 (9:16 aspect ratio)
  • 1408x768 (16:9 aspect ratio)
Maximum number of input tokens (text-to-image generation prompt text) 480 tokens

Model versions

There are various versions of the image generation model you can use. For general information on Imagen model versioning, see Imagen models and lifecycle.

The following models and their associated features are available for image generation:

Model Model resource name and version Launch stage Features Aspect ratios Languages supported Billing
Imagen 3 Imagen 3: imagen-3.0-generate-001


Imagen 3 Fast: imagen-3.0-fast-generate-001

This is a low-latency model variant you can use for prototyping or low-latency use cases.


Imagen 3 Customization and Editing: imagen-3.0-capability-001
General Availability

Supported features:

  • Image generation
  • Digital watermarking and verification
  • User-configurable safety settings
  • Image customization (few-shot learning):
    • Subject customization (product, person, and animal companion)
    • Style customization
    • Controlled customization (scribble and canny edge)
    • Instruct customization (style transfer)
  • Image editing (mask-based):
    • inpainting (insert or remove)
    • outpainting
    • product image editing
  • 1:1 - 1024x1024 pixels (square)
  • 3:4  - 896x1280
  • 4:3  - 1280x896
  • 9:16 - 768x1408
  • 16:9 - 1408x768

General availability:

  • English

Preview:

  • Chinese (simplified)
  • Chinese (traditional)
  • Hindi
  • Japanese
  • Korean
  • Portuguese
  • Spanish

Yes, pricing applies for generation. The pricing for the Imagen 3 models is at a new SKU, so pricing differs from other models.

To view all features and launch stages, see the Imagen overview.
To view the pricing associated with each feature, see the pricing page.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. Set up authentication for your environment.

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    Java

    To use the Java 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.

    1. Install the Google Cloud CLI.
    2. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

    For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.

    Node.js

    To use the Node.js 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.

    1. Install the Google Cloud CLI.
    2. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

    For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.

    Python

    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.

    1. Install the Google Cloud CLI.
    2. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

    For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.

    REST

    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, then initialize it by running the following command:

      gcloud init

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Generate images with text

You can generate novel images using only descriptive text as an input. The following samples show you basic instructions to generate images, but you can also use additional parameters depending on your use case.

Console

  1. In the Google Cloud console, open the Vertex AI Studio > Media tab in the Vertex AI dashboard.

    Go to the Vertex AI Studio tab
  2. In the Write your prompt field, enter a description for the images you want to generate. For details about writing effective prompts, see the prompt guide.

    • For example: small boat on water in the morning watercolor illustration
  3. Optional. In the Model options box in the Parameters panel, select the model version to use. For more information, see model versions.

  4. Optional. Change standard and advanced parameters.

  5. To generate images, click Generate.

    A sample generated image using text
    Image generation view of images generated with Imagen on Vertex AI from the prompt: small red boat on water in the morning watercolor illustration muted colors.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud project ID.
  • MODEL_VERSION: The imagegeneration model version to use. Available values:
    • Imagen 3:
      • imagen-3.0-generate-001
      • imagen-3.0-fast-generate-001 - Low latency model version.
    • Default model version:
      • imagegeneration - Uses the default model version v.006. As a best practice, you should always specify a model version, especially in production environments.

    For more information about model versions and features, see model versions.

  • LOCATION: Your project's region. For example, us-central1, europe-west2, or asia-northeast3. For a list of available regions, see Generative AI on Vertex AI locations.
  • TEXT_PROMPT: The text prompt guides what images the model generates. This field is required for both generation and editing.
  • IMAGE_COUNT: The number of generated images. Accepted integer values: 1-8 (v.002), 1-4 (all other model versions). Default value: 4.

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/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

Save the request body in a file named 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

Save the request body in a file named 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
The following sample response is for a request with "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"
    }
  ]
}

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

In this sample you call the 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 model versions.


import vertexai
from vertexai.preview.vision_models import ImageGenerationModel

# 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-001")

images = model.generate_images(
    prompt=prompt,
    # Optional parameters
    number_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 bytes

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Java API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

In this sample, you specify the imagen-3.0-generate-001 model as part of an EndpointName. The EndpointName is passed to the predict method which is called on a PredictionServiceClient. The service generates images which are then saved locally. For more information on model versions and features, see model versions.


import com.google.api.gax.rpc.ApiException;
import com.google.cloud.aiplatform.v1.EndpointName;
import com.google.cloud.aiplatform.v1.PredictResponse;
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
import com.google.gson.Gson;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class GenerateImageSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String location = "us-central1";
    String prompt = ""; // The text prompt describing what you want to see.

    generateImage(projectId, location, prompt);
  }

  // Generate an image using a text prompt using an Imagen model
  public static PredictResponse generateImage(String projectId, String location, String prompt)
      throws ApiException, IOException {
    final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
    PredictionServiceSettings predictionServiceSettings =
        PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (PredictionServiceClient predictionServiceClient =
        PredictionServiceClient.create(predictionServiceSettings)) {

      final EndpointName endpointName =
          EndpointName.ofProjectLocationPublisherModelName(
              projectId, location, "google", "imagen-3.0-generate-001");

      Map<String, Object> instancesMap = new HashMap<>();
      instancesMap.put("prompt", prompt);
      Value instances = mapToValue(instancesMap);

      Map<String, Object> paramsMap = new HashMap<>();
      paramsMap.put("sampleCount", 1);
      // You can't use a seed value and watermark at the same time.
      // paramsMap.put("seed", 100);
      // paramsMap.put("addWatermark", false);
      paramsMap.put("aspectRatio", "1:1");
      paramsMap.put("safetyFilterLevel", "block_some");
      paramsMap.put("personGeneration", "allow_adult");
      Value parameters = mapToValue(paramsMap);

      PredictResponse predictResponse =
          predictionServiceClient.predict(
              endpointName, Collections.singletonList(instances), parameters);

      for (Value prediction : predictResponse.getPredictionsList()) {
        Map<String, Value> fieldsMap = prediction.getStructValue().getFieldsMap();
        if (fieldsMap.containsKey("bytesBase64Encoded")) {
          String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue();
          Path tmpPath = Files.createTempFile("imagen-", ".png");
          Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded));
          System.out.format("Image file written to: %s\n", tmpPath.toUri());
        }
      }
      return predictResponse;
    }
  }

  private static Value mapToValue(Map<String, Object> map) throws InvalidProtocolBufferException {
    Gson gson = new Gson();
    String json = gson.toJson(map);
    Value.Builder builder = Value.newBuilder();
    JsonFormat.parser().merge(json, builder);
    return builder.build();
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Node.js API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

In this sample, you call the predict method on a PredictionServiceClient. The service generates images which are then saved locally. For more information on model versions and features, see model versions.

/**
 * TODO(developer): Update these variables before running the sample.
 */
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';
const prompt = 'a dog reading a newspaper';

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function generateImage() {
  const fs = require('fs');
  const util = require('util');
  // Configure the parent resource
  const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagen-3.0-generate-001`;

  const promptText = {
    prompt: prompt, // The text prompt describing what you want to see
  };
  const instanceValue = helpers.toValue(promptText);
  const instances = [instanceValue];

  const parameter = {
    sampleCount: 1,
    // You can't use a seed value and watermark at the same time.
    // seed: 100,
    // addWatermark: false,
    aspectRatio: '1:1',
    safetyFilterLevel: 'block_some',
    personGeneration: 'allow_adult',
  };
  const parameters = helpers.toValue(parameter);

  const request = {
    endpoint,
    instances,
    parameters,
  };

  // Predict request
  const [response] = await predictionServiceClient.predict(request);
  const predictions = response.predictions;
  if (predictions.length === 0) {
    console.log(
      'No image was generated. Check the request parameters and prompt.'
    );
  } else {
    let i = 1;
    for (const prediction of predictions) {
      const buff = Buffer.from(
        prediction.structValue.fields.bytesBase64Encoded.stringValue,
        'base64'
      );
      // Write image content to the output file
      const writeFile = util.promisify(fs.writeFile);
      const filename = `output${i}.png`;
      await writeFile(filename, buff);
      console.log(`Saved image ${filename}`);
      i++;
    }
  }
}
await generateImage();

Use parameters to generate images

When you generate images there are several standard and advanced parameters you can set depending on your use case.

Add or verify an image watermark

By default, a digital watermark is added to any images generated by a model version that supports watermark generation. This features adds a non-visible digital watermark—called a SynthID—to images. You can then verify if an image contains a digital watermark or not.

Generate watermarked images

Use the following samples to generate images with a digital watermark.

Console

  1. In the Google Cloud console, open the Vertex AI Studio > Media tab in the Vertex AI dashboard.

    Go to the Vertex AI Studio tab
  2. In the Write your prompt field, enter a description for the images you want to generate. For details about writing effective prompts, see the prompt guide.

    • For example: small boat on water in the morning watercolor illustration
  3. Optional. In the Model options box in the Parameters panel, select the model version to use. For more information, see model versions.

  4. Optional. Change standard and advanced parameters.

  5. To generate images, click Generate.

  6. Model version 006 and greater: 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 go to the Image detail window. Watermarked images contain a Digital watermark badge. You can also explicitly verify an image watermark.

    A sample watermarked generated image using text
    Image detail view of a watermarked image generated with Imagen 2 from the prompt: small red boat on water in the morning watercolor illustration muted colors.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud project ID.
  • MODEL_VERSION: The imagegeneration model version to use. Available values:
    • imagen-3.0-generate-001
    • imagen-3.0-fast-generate-001 - Low latency model version.
    • imagegeneration@006

    For more information about model versions and features, see model versions.

  • LOCATION: Your project's region. For example, us-central1, europe-west2, or asia-northeast3. For a list of available regions, see Generative AI on Vertex AI locations.
  • TEXT_PROMPT: The text prompt guides what images the model generates. This field is required for both generation and editing.
  • IMAGE_COUNT: The number of generated images. Accepted integer values: 1-8 (v.002), 1-4 (all other model versions). Default value: 4.
  • addWatermark - A boolean to enable a watermark for generated images. Any image generated when the field is set to 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.

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_VERSION:predict

Request JSON body:

{
  "instances": [
    {
      "prompt": "TEXT_PROMPT"
    }
  ],
  "parameters": {
    "sampleCount": IMAGE_COUNT,
    "addWatermark": true
  }
}

To send your request, choose one of these options:

curl

Save the request body in a file named 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

Save the request body in a file named 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
The following sample response is for a request with "sampleCount": 2. The response returns two prediction objects, with the generated image bytes base64-encoded. The digital watermark is automatically added to images, so the response is the same as a non-watermarked response.
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    }
  ]
}

Vertex AI SDK for Python


import vertexai
from vertexai.preview.vision_models import ImageGenerationModel

# 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-001")

images = model.generate_images(
    prompt=prompt,
    # Optional parameters
    number_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 bytes

Node.js

/**
 * TODO(developer): Update these variables before running the sample.
 */
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';
const prompt = 'a dog reading a newspaper';

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function generateImage() {
  const fs = require('fs');
  const util = require('util');
  // Configure the parent resource
  const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagen-3.0-generate-001`;

  const promptText = {
    prompt: prompt, // The text prompt describing what you want to see
  };
  const instanceValue = helpers.toValue(promptText);
  const instances = [instanceValue];

  const parameter = {
    sampleCount: 1,
    // You can't use a seed value and watermark at the same time.
    // seed: 100,
    // addWatermark: false,
    aspectRatio: '1:1',
    safetyFilterLevel: 'block_some',
    personGeneration: 'allow_adult',
  };
  const parameters = helpers.toValue(parameter);

  const request = {
    endpoint,
    instances,
    parameters,
  };

  // Predict request
  const [response] = await predictionServiceClient.predict(request);
  const predictions = response.predictions;
  if (predictions.length === 0) {
    console.log(
      'No image was generated. Check the request parameters and prompt.'
    );
  } else {
    let i = 1;
    for (const prediction of predictions) {
      const buff = Buffer.from(
        prediction.structValue.fields.bytesBase64Encoded.stringValue,
        'base64'
      );
      // Write image content to the output file
      const writeFile = util.promisify(fs.writeFile);
      const filename = `output${i}.png`;
      await writeFile(filename, buff);
      console.log(`Saved image ${filename}`);
      i++;
    }
  }
}
await generateImage();

Verify a watermarked image

Use the following samples to verify that an image has a watermark.

Console

  1. In the Google Cloud console, open the Vertex AI Studio > Media tab in the Vertex AI dashboard.

    Go to the Vertex AI Studio tab

  2. In the lower panel, click Verify.

  3. Click Upload image.

  4. Select a locally-saved generated image.

    Sample verified watermark in generated image in console

REST

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud project ID.
  • LOCATION: Your project's region. For example, us-central1, europe-west2, or asia-northeast3. For a list of available regions, see Generative AI on Vertex AI locations.
  • B64_IMAGE: The image to verify that contains a digital watermark. The image must be specified as a base64-encoded byte string. Size limit: 10 MB.

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imageverification@001:predict

Request JSON body:

{
  "instances": [
    {
      "image": {
        "bytesBase64Encoded": "B64_IMAGE"
      }
    }
  ],
  "parameters": {
    "watermarkVerification": true
  }
}

To send your request, choose one of these options:

curl

Save the request body in a file named 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/imageverification@001:predict"

PowerShell

Save the request body in a file named 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/imageverification@001:predict" | Select-Object -Expand Content
The following sample response is for a request with an image that has a digital watermark. The decision field can return any of the following values: ACCEPT or REJECT.
{
  "predictions": [
    {
      "decision": "ACCEPT"
    }
  ]
}

Vertex AI SDK for Python


import vertexai
from vertexai.preview.vision_models import (
    Image,
    WatermarkVerificationModel,
)

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# input_file = "input-image.png"

vertexai.init(project=PROJECT_ID, location="us-central1")

verification_model = WatermarkVerificationModel.from_pretrained(
    "imageverification@001"
)
image = Image.load_from_file(location=input_file)

watermark_verification_response = verification_model.verify_image(image)

print(
    f"Watermark verification result: {watermark_verification_response.watermark_verification_result}"
)
# Example response:
# Watermark verification result: ACCEPT
# or "REJECT" if the image does not contain a digital watermark.

Node.js

/**
 * TODO(developer): Update these variables before running the sample.
 */
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';
const inputFile = 'resources/dog_newspaper.png'; // has watermark

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function verifyImageWatermark() {
  const fs = require('fs');
  // Configure the parent resource
  const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imageverification@001`;

  const imageFile = fs.readFileSync(inputFile);
  // Convert the image data to a Buffer and base64 encode it.
  const encodedImage = Buffer.from(imageFile).toString('base64');

  const instance = {
    image: {
      bytesBase64Encoded: encodedImage,
    },
  };
  const instanceValue = helpers.toValue(instance);
  const instances = [instanceValue];

  const request = {
    endpoint,
    instances,
  };

  // Predict request
  const [response] = await predictionServiceClient.predict(request);
  const predictions = response.predictions;
  if (predictions.length === 0) {
    console.log('No decision was generated. Check the request image.');
  } else {
    predictions.forEach(prediction => {
      // "ACCEPT" if the image contains a digital watermark
      // "REJECT" if the image does not contain a digital watermark
      console.log(prediction.structValue.fields.decision.stringValue);
    });
  }
}
await verifyImageWatermark();

Configure Responsible AI (RAI) safety settings

There are several Responsible AI (RAI) filtering parameters you can use with an image generation model. For example, you can let the model report RAI filter codes for blocked content, disable people or face generation using RAI filters, set the level of content filtering, or return rounded RAI scores of list of safety attributes for input and output.

For more detailed information about Responsible AI (RAI), its associated parameters, and their sample output, see Understand and configure Responsible AI for Imagen.

The following samples show you how to set available RAI parameters for image generation.

Console

  1. In the Google Cloud console, open the Vertex AI Studio > Media tab in the Vertex AI dashboard.

    Go to the Vertex AI Studio tab

  2. Add your text prompt and choose your input parameters.

  3. If not expanded, click Advanced options.

  4. Click Safety settings.

  5. Choose your safety settings:

    • Person/face generation: Choose a setting:
      • Allow (All ages)
      • Allow (Adults only)
      • Don't allow
    • Safety filter threshold: Choose a setting:
      • Block low and above
      • Block medium and above
      • Block only high
  6. Click Save.

  7. Click Generate.

REST

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud project ID.
  • LOCATION: Your project's region. For example, us-central1, europe-west2, or asia-northeast3. For a list of available regions, see Generative AI on Vertex AI locations.
  • TEXT_PROMPT: The text prompt guides what images the model generates. This field is required for both generation and editing.
  • IMAGE_COUNT: The number of generated images. Accepted integer values: 1-8 (v.002), 1-4 (all other model versions). Default value: 4.
  • SAFETY_SETTING: 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.
  • PERSON_SETTING: Optional. The safety setting that controls the type of people or face generation the model allows. Available values:
    • allow_all: Allow generation of people of all ages. This option is only available for Offline customers.
    • 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.
  • INCLUDE_RAI_REASON: Optional. A boolean whether to enable the Responsible AI filtered reason code in responses with blocked input or output. Default value: false.
  • INCLUDE_SAFETY_ATTRIBUTES: 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.

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@006:predict

Request JSON body:

{
  "instances": [
    {
      "prompt": "TEXT_PROMPT"
    }
  ],
  "parameters": {
    "sampleCount": IMAGE_COUNT,
    "safetySetting": "SAFETY_SETTING",
    "personGeneration": "PERSON_SETTING",
    "includeRaiReason": INCLUDE_RAI_REASON,
    "includeSafetyAttributes": INCLUDE_SAFETY_ATTRIBUTES
  }
}

To send your request, choose one of these options:

curl

Save the request body in a file named 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/imagegeneration@006:predict"

PowerShell

Save the request body in a file named 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/imagegeneration@006:predict" | Select-Object -Expand Content
The response you get depends on the safety settings you set. For more information, see Understand and configure Responsible AI (RAI) for Imagen.

Text prompt language

This optional parameter lets you set the language of the input text for image generation or image editing.

a book image generated from a prompt in hindi
Image generated from prompt: ऊपर से देखा गया किताबों का ढेर। सबसे ऊपरी पुस्तक में एक पक्षी का जलरंग चित्रण है। किताब पर VERTEX AI मोटे अक्षरों में लिखा हुआ है *

* A pile of books seen from above. The topmost book contains a watercolor illustration of a bird. VERTEX AI is written in bold letters on the book
an image of a woman from a prompt in korean
Image generated from prompt: 어두운 노란색과 청록색으로 이루어진 밝은 색의 옷을입고 귀걸이를 끼고있는 여자 포스트 모던 패션 사진

Woman wearing bright colors, in the style of dark yellow and dark cyan, wearing earrings, postmodern fashion photography

Before you begin

Complete the following additional steps before you use this feature:

  1. Use the following command to create a service identity for Vertex AI to use in your project:

    gcloud beta services identity create --service=aiplatform.googleapis.com --project=PROJECT_ID
    
  2. Request feature access. To request access, send an email to the Google Cloud Trusted Testers Access: GenApp Builder group. Reference Multi-Lingual Prompts in your message, and include your project number. The approval process usually takes several hours.

Set text prompt language

The following input text prompt language values are supported:

  • Chinese (simplified) (zh/zh-CN)
  • Chinese (traditional) (zh-TW)
  • English (en, default value)
  • Hindi (hi)
  • Japanese (ja)
  • Korean (ko)
  • Portuguese (pt)
  • Spanish (es)

Console

If your prompt is in one of the supported languages, Imagen automatically detects and translates your text and returns your generated or edited images.

If your prompt is in an unsupported language, Imagen uses the text verbatim for the request. This might result in unexpected output.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your Google Cloud project ID.
  • TEXT_PROMPT: The text prompt guides what images the model generates. This field is required for both generation and editing.
  • PROMPT_LANGUAGE: The language code that corresponds to your text prompt language. In this example it would be hi. Available values:
    • auto - Automatic detection. If Imagen detects a supported language, the prompt (and optionally, a negative prompt), are translated to English. If the language detected is not supported, Imagen uses the input text verbatim, which might result in unexpected output. No error code is returned.
    • en - English (default value if omitted)
    • es - Spanish
    • hi - Hindi
    • ja - Japanese
    • ko - Korean
    • pt - Portuguese
    • zh-TW - Chinese (traditional)
    • zh or zh-CN - Chinese (simplified)

HTTP method and URL:

POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/imagegeneration@005:predict

Request JSON body:

{
  "instances": [
    {
      "prompt": "सूर्यास्त के समय एक समुद्र तट। उड़ते पक्षी, हवा में लहराते नारियल के पेड़। लोग समुद्र तट पर सैर का आनंद ले रहे हैं।"
    }
  ],
  "parameters": {
    "language": "PROMPT_LANGUAGE"
  }
}

To send your request, choose one of these options:

curl

Save the request body in a file named 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://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/imagegeneration@005:predict"

PowerShell

Save the request body in a file named 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://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/imagegeneration@005:predict" | Select-Object -Expand Content
The following sample response is for a request with "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"
    }
  ]
}

Aspect ratio

Depending on how you plan to use your generated images, some aspect ratios may work better than others. Choose the aspect ratio that best suits your use case.

Supported aspect ratios and their intended use:

Aspect ratio Intended use Image resolution (pixels) Sample image
1:1 default, square, general use 1024x1024 (Imagen v.002)

1536x1536 (Imagen 2 v.005, v.006)

1024x1024 (Imagen 3)
Sample generated image in the console
Prompt: overhead shot of a pasta dinner, studio photo in the style of food magazine cover.
3:4 TV, media, film 1344x1792 (Imagen 2 v.006)

896x1280 (Imagen 3)
Sample generated image in the console
Prompt: commercial photoshoot, fragrance ad, lavender vanilla scented bottle on a light colored background.
4:3 TV, media, film 1792x1344 (Imagen 2 v.006)

1280x896 (Imagen 3)
Sample generated image in the console
Prompt: commercial photoshoot, green and gray high top sneakers, 4k, dramatic angles.
9:16 portrait, tall objects, mobile devices 1134x2016 (Imagen 2 v.005, v.006)

768x1408 (Imagen 3)
Sample generated image in the console
Prompt: skyscrapers in new york city, futuristic rendering, concept, digital art.
16:9 landscape 2016x1134 (Imagen 2 v.006)

1408x768 (Imagen 3)
Sample generated image in the console
Prompt: nature photography, a beach in hawaii with the ocean in the background, lens flare, sunset.

Console

  1. Follow the generate image with text instructions to open the Vertex AI Studio and enter your text prompt.

  2. In the Parameters panel, select an aspect ratio from the Aspect ratio menu.

  3. Click Generate.

REST

Aspect ratio is an optional field in the parameters object of a JSON request body.

  1. Follow the generate image with text instructions to replace other request body variables.

  2. Replace the following:

    • ASPECT_RATIO: 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)
    {
      "instances": [
        ...
      ],
      "parameters": {
        "sampleCount": IMAGE_COUNT,
        "aspectRatio": "ASPECT_RATIO"
      }
    }
    
  3. Follow the generate image with text instructions to send your REST request.

Number of results

Use the number of results parameter to limit the amount of images returned for each request (generate or edit) you send.

Console

  1. Follow the generate image with text instructions to open the Vertex AI Studio and enter your text prompt.

  2. In the Parameters panel, select a valid integer value in the Number of results field.

  3. Click Generate.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Number of results is a field in the parameters object of a JSON request body.

  1. Follow the generate image with text instructions to replace other request body variables.

  2. Replace the following:

    • IMAGE_COUNT: The number of generated images. Accepted integer values: 1-8 (v.002), 1-4 (all other model versions). Default value: 4.
    {
      "instances": [
        ...
      ],
      "parameters": { 
        "sampleCount": IMAGE_COUNT
      }
    }
    
  3. Follow the generate image with text instructions to send your REST request.

Negative prompt

A negative prompt is a description of what you want to omit in generated images. For example, consider the prompt "a rainy city street at night with no people". The model may interpret "people" as a directive of what include instead of omit. To generate better results, you could use the prompt "a rainy city street at night" with a negative prompt "people".

Imagen generates these images with and without a negative prompt:

Text prompt only

  • Text prompt: "a pizza"

three sample pizza images

Text prompt and negative prompt

  • Text prompt: "a pizza"
  • Negative prompt: "pepperoni"

three sample pizza images without pepperoni

Console

  1. Follow the generate image with text instructions to open the Vertex AI Studio and enter your text prompt.

  2. In the Parameters panel, enter a negative prompt in the Negative prompt field.

  3. Click Generate.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Negative prompt is an optional field in the parameters object of a JSON request body.

  1. Follow the generate image with text instructions to replace other request body variables.

  2. Replace the following:

    • NEGATIVE_PROMPT: A negative prompt to help generate the images. For example: "animals" (removes animals), "blurry" (makes the image clearer), "text" (removes text), or "cropped" (removes cropped images).
    {
      "instances": [
        ...
      ],
      "parameters": {
        "sampleCount": IMAGE_COUNT,
        "negativePrompt": "NEGATIVE_PROMPT"
      }
    }
    
  3. Follow the generate image with text instructions to send your REST request.

Seed number

A seed number is a number you add to a request to make generated images deterministic. Adding a seed number with your request is a way to assure you get the same generated images each time. For example, you can provide a prompt, set the number of results to 1, and use a seed number to get the same image each time you use all those same input values. If you send the same request with the number of results set to 8, you will get the same eight images. However, the images aren't necessarily returned in the same order.

Console

  1. Follow the generate image with text instructions to open the Vertex AI Studio and enter your text prompt.

  2. In the Parameters panel, click the Advanced options expandable section.

  3. In the Seed field, enter a seed number.

  4. Click Generate.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Seed number is an optional field in the parameters object of a JSON request body.

  1. Follow the generate image with text instructions to replace other request body variables.

  2. Replace the following:

    • SEED_NUMBER: Any non-negative integer you provide to make output images deterministic. Providing the same seed number always results in the same output images. Accepted integer values: 1 - 2147483647.
    {
      "instances": [
        ...
      ],
      "parameters": {
        "sampleCount": IMAGE_COUNT,
        "seed": SEED_NUMBER,
        // required for model version 006 and greater only when using a seed number
        "addWatermark": false
      }
    }
    
  3. Follow the generate image with text instructions to send your REST request.

Predefined style

The style of image you are looking to generate. You can use this feature to create images in popular styles such as digital art, watercolor, or cyberpunk.

Console

  1. Follow the generate image with text instructions to open the Vertex AI Studio and enter your text prompt.

  2. In Style section of the Parameters panel, chose a style from the menu.

  3. Click Generate.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Predefined style is an optional field in the parameters object of a JSON request body.

  1. Follow the generate image with text instructions to replace other request body variables.

  2. Replace the following:

    • IMAGE_STYLE: One of the available predefined styles:
      • photograph
      • digital_art
      • landscape
      • sketch
      • watercolor
      • cyberpunk
      • pop_art
    {
      "instances": [
        ...
      ],
      "parameters": {
        "sampleCount": IMAGE_COUNT,
        "sampleImageStyle": "IMAGE_STYLE"
      }
    }
    
  3. Follow the generate image with text instructions to send your REST request.

Upscale an image

Use upscaling to increase the size of existing, generated, or edited images without losing quality.

Console

  1. Follow the generate image with text instructions to generate images.

  2. Select the image to upscale.

  3. Click Export.

  4. Select Upscale images.

  5. Choose a value from the Scale factor.

  6. Click Export to save the upscaled image.

REST

For more information about imagegeneration model requests, see the imagegeneration model API reference.

Upscaling mode is an optional field in the parameters object of a JSON request body. When you upscale an image using the API, specify "mode": "upscale" and upscaleConfig.

Before using any of the request data, make the following replacements:

  • LOCATION: Your project's region. For example, us-central1, europe-west2, or asia-northeast3. For a list of available regions, see Generative AI on Vertex AI locations.
  • PROJECT_ID: Your Google Cloud project ID.
  • B64_BASE_IMAGE: The base image to edit or upscale. The image must be specified as a base64-encoded byte string. Size limit: 10 MB.
  • IMAGE_SOURCE: The Cloud Storage location of the image you want to edit or upscale. For example: gs://output-bucket/source-photos/photo.png.
  • UPSCALE_FACTOR: Optional. The factor to which the image will be upscaled. If not specified, the upscale factor will be determined from the longer side of the input image and sampleImageSize. Available values: x2 or x4 .

HTTP method and URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict

Request JSON body:

{
  "instances": [
    {
      "prompt": "",
      "image": {
        // use one of the following to specify the image to upscale
        "bytesBase64Encoded": "B64_BASE_IMAGE"
        "gcsUri": "IMAGE_SOURCE"
        // end of base image input options
      },
    }
  ],
  "parameters": {
    "sampleCount": 1,
    "mode": "upscale",
    "upscaleConfig": {
      "upscaleFactor": "UPSCALE_FACTOR"
    }
  }
}

To send your request, choose one of these options:

curl

Save the request body in a file named 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/imagegeneration@002:predict"

PowerShell

Save the request body in a file named 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/imagegeneration@002:predict" | Select-Object -Expand Content

You should receive a JSON response similar to the following:

{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "iVBOR..[base64-encoded-upscaled-image]...YII="
    }
  ]
}

What's next

Read articles about Imagen and other Generative AI on Vertex AI products: