生成图片

Image API 是 Product Studio 子 API 的一部分。该工具利用 Google Product Studio (GPS) API 的生成式 AI 工具生成和优化商品图片。您可以使用它来提高客户互动度和转化率。

您可以使用该 API 生成什么内容?

  • 商品图片,其中包含 AI 根据提供的商品信息和文字提示生成的背景
  • 移除了原始图片背景的商品图片
  • 原始图片分辨率经过提升的商品图片

生成图片背景

GenerateProductImageBackground 方法可以使用原始商品图片和详细说明背景的文本提示,创建带有 AI 生成的背景的商品图片。

该 API 接受:

  • 产品图片:可以是以下任一内容:
    • 指向商品图片的 URI(例如 {"image_uri": "https://my-store.com/img/1.png"}
    • 原始图片字节,例如 {"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
  • 提示:用于指定要生成的背景的字符串:
    • product_description:商品的说明。
    • background_description:所需背景的说明。

移除图片背景

RemoveProductImageBackground 方法可以生成从原始商品图片中移除背景的图片。

该 API 接受:

  • 产品图片:可以是以下任一内容:
    • 指向商品图片的 URI,例如 {"image_uri": "https://my-store.com/img/1.png"}
    • 原始图片字节,例如 {"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
  • 背景颜色:移除后背景的颜色,以 RGB 格式提供。如果未指定颜色,背景将是 RGBA 4 通道透明图片。

放大图片

UpscaleProductImage 方法可以生成分辨率更高的原始商品图片。

该 API 接受商品图片。可以是以下任一内容:

  • 指向商品图片的 URI,例如 {"image_uri": "https://my-store.com/img/1.png"}
  • 原始图片字节,例如 {"image_bytes": "image_bytes"}

使用客户端库

我们建议您使用客户端库来提交请求。如果您的语言没有受支持的客户端库,我们可以直接为您提供库文件(通过电子邮件或共享云端硬盘文件夹),以便您在 Maven 项目中进行安装。

示例

以下示例使用该 API 生成或优化商品图片。

为产品图片生成背景

该示例展示了如何使用您选择的背景生成商品图片。

请求

请求正文包含图片(URI 或字节)和用于生成背景的提示。以下示例展示了使用图片 URI 的请求结构:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground

{
   "input_image": {
      "image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
   },
   "config": {
      "product_description": "a jar"
      "background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
   }
}

或者,您也可以使用原始图片字节:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground

{
   "input_image": {
      "image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
   },
   "config": {
      "product_description": "a jar"
      "background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
   }
}

响应

您可能会收到如下回复:

{
  "generated_image": {
    "image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'"
    "generation_time": "2025-05-13T00:06:54.754828Z"
  },
}

或者,如果 output_config, 中的 return_image_uri 设置为 true,则会如下所示:

{
  "generated_image": {
    "uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEvIFfK0YaLKvwvIBc9ItJtJGFE6hi25o6-N3Fz6YTw57pFghfIVlCGve7eyJcQkhoAqGAf8NDzW70qjwGOCuHhiouqF2nqxBwCcfDxZPkYRl3h7CIjyev9asJw7x-0g0V_xvurfowZJ0moXu98kmV01A3Thv2ZRlTAiOPYuEb8wnY7EeyD9l48lDdwJgZ4jqxtTgrRbwvpno4NICj9q830RzATiBHuJ_WUFQJiHOMAqbGyrQIWQTQ",
    "generation_time": "2025-05-13T00:06:54.754828Z"
  }
}

从商品图片中移除背景

该示例展示了如何从商品图片中移除背景。

请求

请求正文包含图片(URI 或字节),以及可选的要替换背景的颜色。

注意:

  • 以 RGB 格式指定颜色值,值应在 [0-255] 范围内。
  • 如果您未指定背景颜色,则背景颜色将为 RGBA 4 通道透明图片。

下面的示例展示了使用图片 URI 并将背景设置为绿色 (0, 255, 0) 的请求结构:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:removeProductImageBackground

{
   "input_image": {
      "image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
   },
   "config": {
      "background_color": {
      "red": 0
      "green": 255
      "blue": 0
    }
   }
}

或者,您也可以使用原始图片字节执行相同的操作:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:removeProductImageBackground

{
   "input_image": {
      "image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
   },
   "config": {
      "background_color": {
      "red": 0
      "green": 255
      "blue": 0
    }
   }
}
响应

您可能会收到如下回复:

{
  "generated_image": {
    "image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'"
    "generation_time": "2025-05-13T00:06:54.754828Z"
  },
}

或者,如果 output_config 中的 return_image_uri 设置为 true,则将如下所示:

{
  "generated_image": {
    "uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEsYi83dcMKVV44i3vHDHzz_PJLuPyJHh9iX7IrsZ0ZIIRIS6SB4ovBmr0L9lZ1VNacZ5duwhcJ8Q8mXMYaIEArf2ieC2OuKEhRzVb-UxlkAUpitEpS5-M3Y3Ch3TGhQTZmBJXlsHLTuf0XcPwNHoNxJlgF0v_mQ6DIlPugZKC_WiYN2DNUkJivfr4WvifhMn5NaMKpDwpH4Acj-bdGNQ77M-8sj4SyEu3sHCF8ZtnMvF5DbGIYOJQ"
  }
}

放大商品图片

该示例展示了如何提高商品图片的分辨率。

请求

请求正文包含图片(URI 或字节)。下面是一个使用图片 URI 的请求结构示例:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:upscaleProductImage

{
   "input_image": {
      "image_uri": "https://services.google.com/fh/files/misc/ring_image_400_600.jpg"
   }
}

或者,您也可以选择改用原始图片字节:

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:upscaleProductImage

{
   "input_image": {
      "image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
   }
}
响应

您可能会收到如下回复:

{
  "generated_image": {
    "image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'"
    "generation_time": "2025-05-13T00:06:54.754828Z"
  },
}

或者,如果 output_config, 中的 return_image_uri 设置为 true,则将如下所示:

{
  "generated_image": {
    "uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEvIFfK0YaLKvwvIBc9ItJtJGFE6hi25o6-N3Fz6YTw57pFghfIVlCGve7eyJcQkhoAqGAf8NDzW70qjwGOCuHhiouqF2nqxBwCcfDxZPkYRl3h7CIjyev9asJw7x-0g0V_xvurfowZJ0moXu98kmV01A3Thv2ZRlTAiOPYuEb8wnY7EeyD9l48lDdwJgZ4jqxtTgrRbwvpno4NICj9q830RzATiBHuJ_WUFQJiHOMAqbGyrQIWQTQ"
  }
}

常见错误和问题

下面列出了一些常见问题及其解决方法。

“输入图片是必需的”

如果您收到以下错误消息,请在请求正文中添加 input_image

Error message:
"error": {
    "code": 400,
    "message": "[input_image] Input image is required to generate product image background.",
    "status": "INVALID_ARGUMENT",
 ...
}

“至少需要一个图片字段”

如果您收到以下错误消息,请仅在请求正文中正确填充 image_uriimage_bytes 之一。

Error message:
"error": {
    "code": 400,
    "message": "[input_image.image] At least one field of image is required to generate product image background.",
    "status": "INVALID_ARGUMENT",
 ...
}

例如,发布此代码会导致错误。

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground

{
   "config": {
      "product_description": "a jar"
      "background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
   }
}

“必须配置才能生成商品图片背景”

如果您收到此错误,请在请求正文中添加 config,并填充 product_descriptionbackground_description

Error message:
"error": {
    "code": 400,
    "message": "[config] Config is required to generate product image background.",
    "status": "INVALID_ARGUMENT",
 ...
}

例如,发布此代码会导致错误。

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground

{
   "input_image": {
      "image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
   }
}

“必须提供商品说明才能生成商品图片背景”

如果您收到以下两条错误消息中的任一条,请务必在 config 中同时填充 product_descriptionbackground_description

以下是缺少商品说明的示例:

Error message:
"error": {
    "code": 400,
    "message": "[config.product_description] Product description is required to generate product image background.",
    "status": "INVALID_ARGUMENT",
 ...
}

以下是背景说明缺失的示例:

Error message:
"error": {
    "code": 400,
    "message": "[config.background_description] Background description is required to generate product image background.",
    "status": "INVALID_ARGUMENT",
 ...
}

例如,运行以下请求将导致错误。

POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground

{
   "input_image": {
      "image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
   },
   "config": {
   }
}