使用文本提示生成图片

您可以使用 Imagen on Vertex AI 根据文本提示生成新图片。支持的界面包括 Google Cloud 控制台和 Vertex AI API。

如需详细了解如何编写用于生成和修改图片的文本提示,请参阅提示指南

查看 Imagen for Generation 模型卡片

试用图片生成功能 (Vertex AI Studio)

在 Colab 中试用 Imagen

准备工作

  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. 为您的环境设置身份验证。

    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.

    Python

    如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. 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.

      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.

    Google Cloud

    REST

    如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

    使用文本生成图片

    您可以仅使用描述性文本作为输入来生成新图片。以下示例展示了生成图片的基本说明。

    控制台

    1. 在 Google Cloud 控制台中,前往 Vertex AI > Media Studio 页面。

      前往媒体工作室

    2. 点击 Imagen。系统会显示 Imagen Media Studio 图片生成页面。

    3. 可选:在设置窗格中,配置以下设置:

      • 模型:从可用选项中选择一个模型。

        如需详细了解可用模型,请参阅 Imagen 模型

      • 宽高比:从可用选项中选择一个宽高比。

      • 结果数量:调整滑块或输入介于 14 之间的值。

    4. 可选:在高级选项部分中,选择要生成图片的区域

    5. 编写提示框中,输入用于描述要生成的图片的文本提示。例如:“清晨水中的小船水彩插图”

      如需详细了解如何编写有效的提示,请参阅提示和图片属性指南

    6. 点击 生成

      数字水印会自动添加到生成的图片中。您无法使用 Google Cloud 控制台停用图片生成功能的数字水印。

      您可以选择图片以在图片详情窗口中查看。 带水印的图片包含数字水印徽章。您还可以明确验证图片水印

      使用文本生成的带水印图片示例
      使用提示中的 Imagen 2 生成的带水印图片的图片详细信息视图:“清晨水中的小船水彩插图”。

    Python

    安装

    pip install --upgrade google-genai

    如需了解详情,请参阅 SDK 参考文档

    设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

    # 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

    在此示例中,您将对 ImageGenerationModel 调用 generate_images 方法,并在本地保存生成的图片。然后,您可以选择使用笔记本中的 show() 方法显示生成的图片。如需详细了解模型版本和功能,请参阅 Imagen 模型

    from google import genai
    
    client = genai.Client()
    
    # TODO(developer): Update and un-comment below line
    # output_file = "output-image.png"
    
    image = client.models.generate_images(
        model="imagen-4.0-generate-preview-06-06",
        prompt="A dog reading a newspaper",
    )
    
    image.generated_images[0].image.save(output_file)
    
    print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    如需详细了解 imagegeneration 模型请求,请参阅 imagegeneration 模型 API 参考文档

    在使用任何请求数据之前,请先进行以下替换:

    • PROJECT_ID:您的 Google Cloud 项目 ID
    • MODEL_VERSION:要使用的 Imagen 模型版本。如需详细了解可用的模型,请参阅 Imagen 模型

    • LOCATION:您的项目的区域。 例如 us-central1europe-west2asia-northeast3。如需查看可用区域的列表,请参阅 Vertex AI 上的生成式 AI 位置
    • TEXT_PROMPT:用于指导模型生成什么图片的文本提示。生成和修改都需要此字段。
    • IMAGE_COUNT:生成的图片数量。 接受的整数值:1-8 (imagegeneration@002)、1-4(所有其他模型版本)。默认值:4。
    • 其他可选参数

      根据您的应用场景,使用以下可选变量。在 "parameters": {} 对象中添加以下部分或全部参数。此列表展示了常见的可选参数,但并非详尽无遗。如需详细了解可选参数,请参阅 Imagen API 参考文档:生成图片

      "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"
      }
      
      • ADD_WATERMARK:布尔值。可选。是否为生成的图片启用水印。将该字段设置为 true 时生成的任何图片都包含数字 SynthID,您可以使用此 ID 来验证带水印的图片。如果您省略此字段,则系统会使用默认值 true;您必须将该值设置为 false 才能停用此功能。仅当 seed 字段设置为 false 时,您才可以使用该字段来获取确定性输出。
      • ASPECT_RATIO:字符串。可选。用于控制宽高比的生成模式参数。支持的宽高比值及其预期用途:
        • 1:1(默认,方形)
        • 3:4(广告、社交媒体)
        • 4:3(电视、摄影)
        • 16:9(横向)
        • 9:16(纵向)
      • ENABLE_PROMPT_REWRITING:布尔值。可选。一个参数,使用基于 LLM 的重写提示功能,以提供更高质量的图片,从而更好地反映原始提示的意图。停用此功能可能会影响图片质量和提示遵循度。默认值:true
      • INCLUDE_RAI_REASON:布尔值。可选。是否在输入或输出被屏蔽的回答中启用 Responsible AI 过滤的原因代码。默认值:true
      • INCLUDE_SAFETY_ATTRIBUTES:布尔值。可选。是否针对未经过滤的输入和输出在回答中启用安全属性列表的四舍五入 Responsible AI 分数。安全属性类别:"Death, Harm & Tragedy""Firearms & Weapons""Hate""Health""Illicit Drugs""Politics""Porn""Religion & Belief""Toxic""Violence""Vulgarity""War & Conflict"。默认值为 false
      • MIME_TYPE:字符串。可选。图片的 MIME 类型。可用的值:
        • image/jpeg
        • image/gif
        • image/png
        • image/webp
        • image/bmp
        • image/tiff
        • image/vnd.microsoft.icon
      • COMPRESSION_QUALITY:整数。可选。仅适用于 JPEG 输出文件。模型为以 JPEG 文件格式生成的图片保留的细节程度。值:0100,其中数字越大表示压缩程度越高。默认值:75
      • PERSON_SETTING:字符串。可选。用于控制模型允许的人物类型或人脸生成的安全设置。可用的值:
        • allow_adult(默认):仅允许生成成年人(名人除外)。不允许针对任何设置生成名人。
        • dont_allow:禁止在生成的图片中包含人物或人脸。
      • SAFETY_SETTING:字符串。可选。一种用于控制生成图片的安全性过滤条件阈值的设置。可用的值:
        • block_low_and_above:最高安全阈值,使过滤的生成图片的数量最多。之前的值:block_most
        • block_medium_and_above(默认值):中等安全阈值,可平衡潜在有害内容和安全内容的过滤。之前的值:block_some
        • block_only_high:安全阈值,可减少因安全过滤器而被屏蔽的请求数量。此设置可能会增加 Imagen 生成的不良内容。 之前的值:block_few
      • SEED_NUMBER:整数。可选。您提供的任何非负整数,以使输出图片具有确定性。提供相同的种子编号会生成相同的输出图片。 如果您使用的模型支持数字水印,则必须设置 "addWatermark": false 才能使用此字段。接受的整数值:1 - 2147483647
      • OUTPUT_STORAGE_URI:字符串。可选。用于存储输出图片的 Cloud Storage 存储桶。如果未提供,则回答中会返回 base64 编码的图片字节。示例值:gs://image-bucket/output/

    HTTP 方法和网址:

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

    请求 JSON 正文:

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

    如需发送请求,请选择以下方式之一:

    curl

    将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

    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 的文件中,然后执行以下命令:

    $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 的请求。响应返回两个预测对象,其中生成的图片字节采用 base64 编码。
    {
      "predictions": [
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        },
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }

    如果您使用的是支持提示增强功能的模型,则回答中会包含一个额外的 prompt 字段,其中包含用于生成的增强型提示:

    {
      "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"
        }
      ]
    }
    

    后续步骤

    阅读有关 Imagen 和其他 Vertex AI 上的生成式 AI 产品的文章: