图像生成访问和使用
Amazon Nova Canvas 可通过 Bedrock InvokeModel API 使用,并在执行模型推理时支持以下推理参数和模型回复。
图像生成请求和回复格式
使用 Amazon Nova Canvas 模型调用 InvokeModel 时,请将请求的 body
字段替换为与应用场景匹配的格式。所有任务共享一个 imageGenerationConfig
对象,但每个任务都有一个特定于该任务的参数对象。支持以下应用场景:
任务类型值 | 任务参数字段 | 任务类别 | 描述 |
---|---|---|---|
TEXT_IMAGE with text only |
textToImageParams |
Generation | Generate an image using a text prompt. |
TEXT_IMAGE with image conditioning |
textToImageParams |
Generation | Provide an input conditioning image along with a text prompt to generate an image that follows the layout and composition of the conditioning image. |
COLOR_GUIDED_GENERATION |
colorGuidedGenerationParams |
Generation | Provide a list of color values in hexadecimal format (e.g. #FF9800) along with a text prompt and optional reference image to generate an image that follows the specified color palette. |
IMAGE_VARIATION |
imageVariationParams |
Generation | Provide one or more input images—with or without a text prompt—to influence the generated image. Can be used to influence the visual style of the generated image (when used with a text prompt), to generate variations of a single image (when used without a text prompt), and for other creative effects and control. |
INPAINTING |
inPaintingParams |
Editing | Modify an image by changing the area inside of a masked region. Can be used to add, remove, or replace elements of an image. |
OUTPAINTING |
outPaintingParams |
Editing | Modify an image by changing the area outside of a masked region. Can be used to replace the background behind a subject. |
BACKGROUND_REMOVAL |
backgroundRemovalParams |
Editing | Automatically remove the background of any image, replacing the background with transparent pixels. Can be useful when you want to later composite the image with other elements in an image editing app, presentation, or website. The background can easily be changed to a solid color through custom code as well. |
用于图像生成的输入图像
许多任务类型都要求在请求中包含一张或多张输入图像。请求中所用的任何图像都必须编码为 Base64 字符串。图像通常采用 PNG 或 JPEG 格式,但每个颜色通道必须为 8 位(RGB)。PNG 图像可以包含额外的 Alpha 通道,但该通道不得包含任何透明或半透明像素。有关支持的输入图像尺寸的具体详细信息,请参阅支持的图像分辨率。
遮罩图像是一种指明要进行补绘或扩绘的区域的图像。这种图像只能包含纯黑色和纯白色像素。
对于补绘请求,涂成黑色的区域称为遮罩区域,将被更改。遮罩图像的其余部分必须仅包含纯白色像素。纯白色像素表示遮罩区域之外的部分。
对于扩绘请求,模型将更改涂成白色的区域。
遮罩图像不得包含任何非纯黑色或纯白色的像素。如果使用 JPEG 图像作为遮罩,则必须以 100% 的质量对其进行压缩,以免在压缩过程中引入非白色或非黑色像素。
有关如何在 Base64 字符串中对图像进行编码或解码的示例,请参阅代码示例。
遮罩图像
编辑图像时,遮罩是定义要编辑区域的一种方式。可以通过以下两种方式之一定义遮罩:
-
maskPrompt
– 编写一个自然语言文本提示来描述图像中要遮罩的部分。 -
maskImage
– 黑白图像,其中纯黑色像素表示遮罩内的区域,纯白色像素表示遮罩外的区域。对于补绘请求,模型将更改黑色像素区域。对于扩绘请求,模型将更改白色像素区域。
可以使用照片编辑工具来绘制遮罩,也可以使用自定义代码来创建遮罩。否则,请用 maskPrompt 字段让模型推断遮罩。
支持的图像分辨率
您可以为生成任务指定任何输出分辨率,只要图像符合以下要求:
-
每条边的像素必须在 320 到 4096 像素之间(含两端值)。
-
每条边必须均可被 16 整除。
-
宽高比必须在 1:4 到 4:1 之间。也就是说,一边的长度不能超过另一边的 4 倍。
-
总像素数必须小于 4,194,304。
这些限制条件中的大多数也适用于输入图像。不过,图像的每条边不要求被 16 整除。