放大生成的图片、编辑后的图片或现有图片

您可以使用 Imagen on Vertex AI 的放大功能在不降低图片质量的情况下增加图片的大小。

模型版本

放大功能的可用性基于模型版本:

特征 Imagen (v.002) Imagen 2 (v.005) Imagen 2 (v.006)
提升 不支持 不支持

放大图片

使用以下代码示例放大现有、生成或编辑的图片。

控制台

  1. 按照使用文本生成图片说明生成图片。

  2. 选择要放大的图片。

  3. 点击放大/导出

  4. 选择放大图片

  5. 缩放比例2x4x)中选择一个值。

  6. 点击 导出以保存放大的图片。

REST

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

纵向扩容模式是 JSON 请求正文的 parameters 对象中的可选字段。使用 API 放大图片时,请指定 "mode": "upscale"upscaleConfig

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

  • LOCATION:您的项目的区域。 例如 us-central1europe-west2asia-northeast3。如需查看可用区域的列表,请参阅 Vertex AI 上的生成式 AI 位置
  • PROJECT_ID:您的 Google Cloud 项目 ID
  • B64_BASE_IMAGE:要修改或放大的基础图片。图片必须指定为 base64 编码的字节字符串。大小上限:10 MB。
  • IMAGE_SOURCE:您要修改或放大的图片的 Cloud Storage 位置。例如:gs://output-bucket/source-photos/photo.png
  • UPSCALE_FACTOR:可选。图片的放大系数。如果未指定,系统将根据输入图片的较长边和 sampleImageSize 确定放大系数。可用值:x2x4

HTTP 方法和网址:

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

请求 JSON 正文:

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

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

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 \
"http://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002: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 "http://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@002:predict" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应:

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

后续步骤