Stability AI 图像服务 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Stability AI 图像服务

您可以将 Stability AI 图像服务与 Amazon Bedrock 配合使用,访问十三种专门的图像编辑工具,这些工具旨在加快专业创作工作流程。借助 Stability AI 图像服务,您可以根据草图生成图像、重构和重新设计现有图像的风格,或者移除并替换图像中的对象。

本节介绍如何使用对稳定性 AI 图像服务进行推理调用。InvokeModel此部分还提供了 Python 中的代码示例,以及使用 Stability AI 图像服务之前和之后的图像示例。

Stability AI 图像服务分为以下类别:

  • 编辑:基于 AI 的图像编辑服务,包括使用蒙版(生成式填充)或文字进行补绘。包括用于产品展示和广告的工具,以及背景删除等基本工具。

  • 控制:可能需要提示、映射和其他指南。这些服务利用 ControlNets 了基于稳定扩散模型的类似技术。

注意

订阅任何编辑或控制 Stability AI 图像服务会自动将您注册到所有十三个可用的 Stability AI 图像服务中。

请求和响应

请求正文在请求body字段中传递给InvokeModel

模型调用请求正文字段

当你使用 Stability AI Image Services InvokeModel 拨打电话时,在正文字段中填入如下所示的 JSON 对象。

{ 'prompt': 'Create an image of a panda' }

模型调用响应正文字段

当你使用 Stability AI Image Services InvokeModel 拨打电话时,响应如下所示

{ 'seeds': [2130420379], 'finish_reasons': [null], 'images': ['...'] }
  • seeds –(字符串)用于为模型生成图像的种子列表。

  • finish_reasons – 表示请求是否被过滤的枚举。null 表示请求成功。当前可能的值:"Filter reason: prompt", "Filter reason: output image", "Filter reason: input image", "Inference error", null

  • images – 以 base64 字符串格式生成的图像列表。

欲了解更多信息,请参阅 https://platform.us.stability。 ai/docs/api-reference#tag/v第 1 代

高档

以下部分描述了高档的稳定性 AI 图像服务。

Creative Usplace 可以拍摄 64x64 到 100 万像素之间的图像,然后将其放大到 4K 分辨率。该服务可以将图像放大 20 到 40 倍,同时保留并经常提高质量。Creative Usplace 在高度降解的图像上效果最好,不适用于 100 万像素或以上的照片,因为它会进行大量的重新构想。

Creative Usplace 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image −(字符串)要放大的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素数必须介于 4,096 到 1,048,576 像素之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • creativity-(数字)表示模型在放大图像时应具有多大的创造@@ 。值越高,则在放大期间会向图像添加更多细节。范围介于 0.1 和 0.5 之间。默认值 0.3

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • style_preset – 引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-creative-upscale-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "This dreamlike digital art captures a vibrant, kaleidoscopic Big Ben in London", "creativity": 0.30 } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-creative-upscale-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "This dreamlike digital art captures a vibrant, kaleidoscopic Big Ben in London", "creativity": 0.30 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表使用以下提示显示了 Creative Usplace 操作的输入和输出图像:这幅梦幻般的数字艺术在郁郁葱葱的雨林中捕捉了一只充满活力、千变万化的鸟

保守派高档拍摄的图像介于 64x64 到 100 万像素之间,然后将其放大到 4K 分辨率。该服务可以将图像放大 20 到 40 倍,同时保留所有方面。保守高档可最大限度地减少对图像的更改,不应用于重新构想图像。

保守派高档具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image −(字符串)要放大的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • creativity-(数字)表示模型在放大图像时应具有多大的创造@@ 。值越高,则在放大期间会向图像添加更多细节。范围介于 0.1 和 0.5 之间。默认值 0.35

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-conservative-upscale-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "This dreamlike digital art captures a vibrant, kaleidoscopic Big Ben in London", "creativity": 0.30 } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-conservative-upscale-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "This dreamlike digital art captures a vibrant, kaleidoscopic Big Ben in London", "creativity": 0.30 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表使用以下提示显示了保守党高档操作的输入和输出图像:森林里一只巨型鸡的照片

Fast Uscale 使用预测性和生成式 AI 将图像分辨率提高了 4 倍。这项轻巧而快速的服务非常适合提高压缩图像的质量,使其适用于社交媒体帖子和其他应用程序。

快速升级具有以下必需参数:

  • image −(字符串)要放大的 Base64 图像。宽度必须介于 32 到 1,536 像素之间。高度必须介于 32 到 1,536 像素之间。总像素数必须介于 1,024 到 1,048,576 像素之间。支持的格式:jpeg、png、webp。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-fast-upscale-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64 } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-fast-upscale-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示了 “快速升级” 操作的输入和输出图像。

编辑

以下部分介绍如何编辑 Stability AI 图像服务。

Inpaint 可以智能地修改图像,根据蒙版图像的内容填充指定区域或用新内容替换指定区域。

Inpaint 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)要补绘的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • style_preset –(字符串)引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • mask:(字符串)逐个像素控制补绘过程中的强度,可以通过第二张图像(传递到此参数)或通过图像参数的 alpha 通道。

    • 在蒙版中传入:传递给此参数的图像应为黑白图像,在像素级别,基于给定像素的明暗度表示补绘的强度。全黑像素表示没有补绘强度,而全白像素代表最大强度。如果蒙版的尺寸与图像参数的大小不同,则会自动调整其大小。

    • Alpha 通道支持:如果您没有显式提供蒙版,则将从图像参数的 Alpha 通道派生蒙版。透明像素将会补绘,而不透明像素将会保留。在同时提供带有 Alpha 通道的图像以及蒙版时,蒙版将优先。

  • grow_mask:将蒙版的边缘在所有方向上向外扩充指定的像素数。蒙版周围的扩展区域将进行模糊处理,这有助于平滑补绘的内容与原始图像之间的过渡。范围介于 0 到 20 之间。默认值:5。如果您注意到补绘内容的周围有接缝或粗糙边缘,请尝试使用此参数。请注意,过度生长可能会掩盖掩 and/or 盖附近蒙版区域的精细细节。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.png" mask = "./content/mask.png" region = "us-east-1" model_id = "us.stability.stable-image-inpaint-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') with open(mask, "rb") as mask_file: mask_base64 = base64.b64encode(mask_file.read()).decode('utf-8') params = { "image": image_base64, "mask": mask_base64, "prompt": "artificer of time and space" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" mask = "./content/mask.png" region = "us-east-1" model_id = "us.stability.stable-image-inpaint-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') with open(mask, "rb") as mask_file: mask_base64 = base64.b64encode(mask_file.read()).decode('utf-8') params = { "image": image_base64, "mask": mask_base64, "prompt": "artificer of time and space" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示补绘操作的输入和输出图像。

Input

遮罩

Output

“Man in metropolis”,使用 Stable Image Ultra 生成,由 Sanwal Yousaf 提供提示并编辑。根据 CC BY 4.0 授予许可

Outpaint 在图像中插入其他内容,以向任何方向填充空间。与其他自动或手动尝试扩展图像内容相比,Outpaint 服务最大限度地减少了原始图像已被编辑的迹象。

Outpaint 具有以下必需的参数:

  • image −(字符串)要画出的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

    注意

    必须为至少一个输出方向(左、右、向上或向下)提供非零值。为了获得最佳质量的效果,请在选择绘制方向时考虑原始图像的构图和内容。

以下参数可选:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • style_preset –(字符串)引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • creativity-(数字)表示模型在超越图像时应具有多大的创造@@ 。较高的值将导致在画外画期间向图像中添加更多创意内容。范围介于 0.1 和 1.0 之间。默认值:0.5。

  • le ft −(整数)图像左侧要绘制的像素数。必须为至少一个出色方向提供非零值。范围 0 到 2000。默认值 0。

  • ri ght-(整数)图像右侧要绘制的像素数。必须为至少一个出色方向提供非零值。范围 0 到 2000。默认值 0。

  • up −(整数)要在图像顶部绘制的像素数。必须为至少一个出色方向提供非零值。范围 0 到 2000。默认值 0。

  • down −(整数)要在图像底部绘制的像素数。必须为至少一个出色方向提供非零值。范围 0 到 2000。默认值 0。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-outpaint-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "left": 512, "right": 512, "up": 200, "down": 100 } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-outpaint-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "left": 512, "right": 512, "up": 200, "down": 100 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示了 Outpaint 操作的输入和输出图像。

通过 Search and Recolor,您可以使用提示更改图像中特定对象的颜色。此服务是补绘的特定版本,不需要蒙版。服务会自动划分对象,并使用提示中要求的颜色对其进行重新着色。

Search and Recolor 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)要重新着色的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

  • select_prompt:(字符串)对要在图像中搜索的内容的简短描述。最多 10000 个字符。

以下参数可选:

  • style_preset –(字符串)引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • grow_mask:将蒙版的边缘在所有方向上向外扩充指定的像素数。蒙版周围的扩展区域将进行模糊处理,这有助于平滑补绘的内容与原始图像之间的过渡。范围介于 0 到 20 之间。默认值:5。如果您注意到补绘内容的周围有接缝或粗糙边缘,请尝试使用此参数。请注意,过度生长可能会掩盖掩 and/or 盖附近蒙版区域的精细细节。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-search-recolor-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "pink jacket", "select_prompt": "jacket" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-search-recolor-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "pink jacket", "select_prompt": "jacket" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示对输入图像,使用以下提示的 Search and Recolor 操作得到的输出图像:pink jacket

Input

Output

“Man wearing puffer jacket”,使用 Stable Image Ultra 生成,由 Sanwal Yousaf 提供提示并编辑。根据 CC BY 4.0 授予许可

通过 Search and Replace,您可以使用搜索提示,通过简单语言来标识要替换的对象。该服务将自动划分对象,并将其替换为提示中提出的对象,而无需使用蒙版。

Search and Replace 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)要重新着色的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

  • search_prompt:对要在图像中补绘的内容的简短描述。最多 10000 个字符。

以下参数可选:

  • style_preset –(字符串)引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • grow_mask:将蒙版的边缘在所有方向上向外扩充指定的像素数。蒙版周围的扩展区域将进行模糊处理,这有助于平滑补绘的内容与原始图像之间的过渡。范围介于 0 到 20 之间。默认值:5。如果您注意到补绘内容的周围有接缝或粗糙边缘,请尝试使用此参数。请注意,过度生长可能会掩盖掩 and/or 盖附近蒙版区域的精细细节。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-search-replace-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "jacket", "search_prompt": "sweater", } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-search-replace-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "jacket", "search_prompt": "sweater", } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示对输入图像,使用以下提示的 Search and Replace 操作得到的输出图像:jacket

Input

Output

“Female model wearing fall sweater”,使用 Stable Image Ultra 生成。由 Sanwal Yousaf 提供提示并编辑。根据 CC BY 4.0 授予许可

通过 Erase,您可以使用图像蒙版移除不需要的元素,同时智能地保持背景一致性。

Erase 具有以下必需参数:

  • image:(字符串)要进行擦除的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • mask:(字符串)逐个像素控制补绘过程中的强度,可以通过第二张图像(传递到此参数)或通过图像参数的 alpha 通道。

    • 在蒙版中传入:传递给此参数的图像应为黑白图像,在像素级别,基于给定像素的明暗度表示补绘的强度。全黑像素表示没有补绘强度,而全白像素代表最大强度。如果蒙版的尺寸与图像参数的大小不同,则会自动调整其大小。

    • Alpha 通道支持:如果您没有显式提供蒙版,则将从图像参数的 Alpha 通道派生蒙版。透明像素将会补绘,而不透明像素将会保留。在同时提供带有 Alpha 通道的图像以及蒙版时,蒙版将优先。

  • grow_mask:将蒙版的边缘在所有方向上向外扩充指定的像素数。蒙版周围的扩展区域将进行模糊处理,这有助于平滑补绘的内容与原始图像之间的过渡。范围介于 0 到 20 之间。默认值:5。如果您注意到补绘内容的周围有接缝或粗糙边缘,请尝试使用此参数。请注意,过度生长可能会掩盖掩 and/or 盖附近蒙版区域的精细细节。

注意

为了获得最佳的擦除效果,请确保蒙版准确定义了要移除的区域。如果未显式提供掩码,则服务将使用输入图像的 Alpha 通道。如果同时提供两者,则蒙版将优先。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.png" mask = "./content/mask.png" region = "us-east-1" model_id = "us.stability.stable-image-erase-object-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8'), with open(mask, "rb") as mask_file: mask_base64 = base64.b64encode(mask_file.read()).decode('utf-8') params = { "image": image_base64, "mask": mask_base64 } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" mask = "./content/mask.png" region = "us-east-1" model_id = "us.stability.stable-image-erase-object-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8'), with open(mask, "rb") as mask_file: mask_base64 = base64.b64encode(mask_file.read()).decode('utf-8') params = { "image": image_base64, "mask": mask_base64 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示 Erase 操作的输入和输出图像。

Input

遮罩

Output

“Students Desk”,使用 Stable Image Ultra 生成。由 Sanwal Yousaf 提供提示并编辑。根据 CC BY 4.0 授予许可

通过 Remove Background,您可以精准地将主体与背景隔离开来。

Remove Background 具有以下必需参数:

  • image:(字符串)要移除背景的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-remove-background-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.png" region = "us-east-1" model_id = "us.stability.stable-image-remove-background-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64 } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示 Remove Background 操作的输入和输出图像。

Input

Output

“Female model wearing fall sweater”,使用 Stable Image Ultra 生成。由 Sanwal Yousaf 提供提示并编辑。根据 CC BY 4.0 授予许可

控件

以下部分介绍如何控制 Stability AI 图像服务。

通过精确的控制,将粗略的手绘草图提升为精致的输出图像。对于非草图图像,Control Sketch 可以利用图像中的轮廓线和边缘,细致地处理最终外观。

Control Sketch 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)草图的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • control_strength:(数字)图像对生成过程的影响或控制程度有多高。使用 0 和 1 之间的浮点数表示,0 表示影响最小,1 表示影响最大。默认值:0.7。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • style_preset – 引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-control-sketch-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "a house with background of mountains and river flowing nearby" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-control-sketch-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "a house with background of mountains and river flowing nearby" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示对输入图像,使用以下提示的 Control Sketch 调用得到的输出图像:a house with background of mountains and river flowing nearby

Input

Output

“House, mountains, and river sketch”,作者:Sanwal Yousaf。根据 CC BY 4.0 授予许可

通过 Control Structure,您可以在保持输入图像结构的情况下生成图像。这在高级内容创作场景中尤为有用,例如重新创建场景或渲染模型中的角色。

Control Structure 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)草图的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • control_strength:(数字)图像对生成过程的影响或控制程度有多高。使用 0 和 1 之间的浮点数表示,0 表示影响最小,1 表示影响最大。默认值:0.7。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • style_preset – 引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-control-structure-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "surreal structure with motion generated sparks lighting the scene" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-control-structure-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "surreal structure with motion generated sparks lighting the scene" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示对输入图像,使用以下提示的 Control Structure 操作得到的输出图像:surreal structure with motion generated sparks lighting the scene

Input

Output

“Person sitting on brown box”,作者:Pawel L。根据 CC 授予许可

通过 Style Guide,您可以从输入图像中提取风格元素,用来指导根据提示创作输出图像。这会得到与输入图像具有相同风格的新图像。

Style Guide 具有以下必需参数:

  • prompt:您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。最少 0 个字符,最多 10000 个字符。

  • image:(字符串)草图的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • aspect_ratio:(字符串)控制生成的图像的宽高比。此参数仅对 text-to-image请求有效。默认为 1:1。枚举:16:9、1:1、21:9、2:3、3:2、4:5、5:4、9:16、9:21。默认为 1:1。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • fidelity:(数字)输出图像风格与输入图像风格的相似度。范围:0 至 1。默认值:0.5。

  • style_preset – 引导图像模型生成特定风格的内容。枚举:3d-model、analog-film、anime、cinematic、comic-book、digital-art、enhance、fantasy-art、isometric、line-art、low-poly、modeling-compound、neon-punk、origami、photographic、pixel-art、tile-texture。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-style-guide-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "wide shot of modern metropolis" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/input.jpg" region = "us-east-1" model_id = "us.stability.stable-image-style-guide-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') params = { "image": image_base64, "prompt": "wide shot of modern metropolis" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示对输入图像,使用以下提示的 Style Guide 调用得到的输出图像:wide shot of modern metropolis

Input

Output

“Abstract Painting”,作者:Steven Johnson。根据 CC 获得许可

利用 Style Transfer,您可以将参考风格图像中的视觉特征应用于目标图像。Style Guide 服务从输入图像中提取风格元素,用来指导根据提示创建的输出图像,而 Style Transfer 则专门用于转换现有内容,同时保留原始构图。此工具可帮助在多个资产之间打造出一致风格的内容。

Style Transfer 具有以下必需参数:

  • init_image:(字符串)包含您想要重新塑造风格的主题的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

  • style_image:(字符串)包含您想要重新塑造的风格的 Base64 图像。图像的每个边都必须至少为 64 像素。总像素不能超过 9437184 个像素。宽高比必须在 1:2.5 到 2.5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • prompt:(字符串)您希望在输出图像中看到的内容。清晰定义元素、颜色和主题的有效描述性提示可带来更好的结果。要控制给定单词的权重,请使用格式(word:weight),其中 word 是您要控制权重的单词,权重是一个值。值 0 和 1.0 会降低单词的权重,1.1 和 2 之间的值会增加单词的权重。例如:天空很清晰(blue:0.3)和(green:1.8),表达的意思是天空是蓝绿色的,但更偏向于绿色。

  • negative_prompt:(字符串)描述您不希望在输出图像中看到内容的文本简介。这是一项高级功能。最多 10000 个字符。

  • seed:(数字)用于引导生成“随机性”的特定值。(省略此参数或输入 0 以使用随机种子。) 范围:0 至 4294967294。默认值:0。

  • output_format:(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认值:png。

  • composition_fidelity:(数字)输出图像风格与输入图像风格的相似度。范围介于 0 到 1 之间。默认值:0.9。

  • style_strength:(数字)该参数有时也称为去噪,用于控制 style_image 参数对生成图像的影响程度。值为 0 时将生成与输入图像完全相同的图像。值为 1 时相当于您没有传入任何图像。范围介于 0 到 1 之间。默认值:1。

  • change_strength:(数字)改变原始图像的程度。范围介于 0.1 到 1 之间。默认值:0.9。

API
import base64 import json import requests import io import os from PIL import Image image = "./content/input.jpg" style_image = "./content/style.jpg" region = "us-east-1" model_id = "us.stability.stable-style-transfer-v1:0" url = f"https://bedrock-runtime.{region}.amazonaws.com/model/{model_id}/invoke" api_key = os.getenv("AWS_BEARER_TOKEN_BEDROCK") # https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" } try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') with open(style_image, "rb") as style_image_file: style_image_base64 = base64.b64encode(style_image_file.read()).decode('utf-8') params = { "init_image": image_base64, "style_image": style_image_base64, "prompt": "statue" } response = requests.request("POST", url, json=params, headers=headers) response.raise_for_status() model_response = json.loads(response.text) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)
Python
import boto3 import base64 import io import json from PIL import Image image = "./content/cat_statue_512x512.jpg" style_image = "./content/glowbot_style.jpg" region = "us-east-1" model_id = "us.stability.stable-style-transfer-v1:0" bedrock = boto3.client("bedrock-runtime", region_name=region) try: with open(image, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') with open(style_image, "rb") as style_image_file: style_image_base64 = base64.b64encode(style_image_file.read()).decode('utf-8') params = { "init_image": image_base64, "style_image": style_image_base64, "prompt": "statue" } request = json.dumps(params) response = bedrock.invoke_model(modelId=model_id, body=request) model_response = json.loads(response["body"].read()) base64_image_data = model_response["images"][0] if not base64_image_data: raise ValueError("No image data found in model response.") image_data = base64.b64decode(base64_image_data) image = Image.open(io.BytesIO(image_data)) image.save("image.png") print("Successfully saved image.") except Exception as e: print(e)

下表显示 Style Transfer 调用的输入和输出图像。

Input

样式

Output

“Standing Woman Statue”,作者:Simon Berger。根据 CC 获得许可

“Blue Bright Lights“,作者:Pixabay。根据 CC0 获得许可