稳定性 AI 图像服务 - Amazon Bedrock

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

稳定性 AI 图像服务

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

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

稳定性 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 图像服务。

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

Inpaint 具有以下必需的参数:

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

  • image −(字符串)要修复的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • style_p reset-(字符串)引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • m@@ ask −(字符串)通过第二张图像(传递到此参数)或通过图像参数的 alpha 通道控制每像素修复过程的强度。

    • 传入蒙版 ——传递给此参数的图像应为黑白图像,该图像在任何像素上都表示基于给定像素的暗度或亮度的修复强度。全黑像素表示没有修复强度,而全白像素代表最大强度。如果遮罩的大小与图像参数的大小不同,则会自动调整其大小。

    • Alpha Channel Supp ort-如果您不提供显式蒙版,则将从图像参数的 Alpha 通道派生出一个显式蒙版。透明像素将被取消绘制,而不透明像素将被保留。如果提供了带有 Alpha 通道的图像和蒙版,则蒙版将优先。

  • g@@ row_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)

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

输入

遮罩

输出

由 Stable Image Ultra 生成的《大都市中的人》、Sanwal Yousaf 的提示和编辑。在 CC BY 4.0 下获得许可

“搜索和重新着色” 允许您使用提示更改图像中特定对象的颜色。此服务是修复的特定版本,不需要蒙版。它将自动分割对象,并使用提示中要求的颜色对其进行重新着色。

“搜索和重新着色” 具有以下必需参数:

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

  • image −(字符串)要重新着色的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

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

以下参数可选:

  • style_p reset-(字符串)引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • g@@ row_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)

下表显示了使用以下提示进行搜索和重新着色操作的输入和输出图像:粉色夹克

输入

输出

由 Stable Image Ultra 生成的 “穿河豚夹克的男人”、Sanwal Yousaf 的提示和编辑。在 CC BY 4.0 下获得许可

“搜索和替换” 允许您使用搜索提示以简单语言识别要替换的对象。该服务将自动对对象进行分段,并将其替换为提示中请求的对象,而无需使用掩码。

“搜索和替换” 具有以下必需参数:

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

  • image −(字符串)要重新着色的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

  • search_pro mpt −(字符串)对要在图像中进行补漆的内容的简短描述。最多 10000 个字符。

以下参数可选:

  • style_p reset-(字符串)引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • g@@ row_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)

下表显示了使用以下提示进行搜索和替换操作的输入和输出图像:jacke t

输入

输出

由 Stable Image Ultra 生成的 “穿秋季毛衣的女模特”。由 Sanwal Yousaf 进行提示和编辑。在 CC BY 4.0 下获得许可

Erase 允许您使用图像蒙版删除不需要的元素,同时智能地保持背景一致性。

擦除具有以下必需参数:

  • image −(字符串)要从中删除的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • m@@ ask −(字符串)通过第二张图像(传递到此参数)或通过图像参数的 alpha 通道控制每像素修复过程的强度。

    • 传入蒙版 ——传递给此参数的图像应为黑白图像,该图像在任何像素上都表示基于给定像素的暗度或亮度的修复强度。全黑像素表示没有修复强度,而全白像素代表最大强度。如果遮罩的大小与图像参数的大小不同,则会自动调整其大小。

    • Alpha Channel Supp ort-如果您不提供显式蒙版,则将从图像参数的 Alpha 通道派生出一个显式蒙版。透明像素将被取消绘制,而不透明像素将被保留。如果提供了带有 Alpha 通道的图像和蒙版,则蒙版将优先。

  • g@@ row_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)

下表显示了 “擦除” 操作的输入和输出图像。

输入

遮罩

输出

由 Stable Image Ultra 生成的 “学生办公桌”。由 Sanwal Yousaf 进行提示和编辑。在 CC BY 4.0 下获得许可

“移除背景” 允许您精确地将拍摄对象与背景隔离开来。

“移除背景” 具有以下必需参数:

  • image −(字符串)要从中移除背景的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举: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)

下表显示了 “移除背景” 操作的输入和输出图像。

输入

输出

由 Stable Image Ultra 生成的 “穿秋季毛衣的女模特”。由 Sanwal Yousaf 进行提示和编辑。在 CC BY 4.0 下获得许可

控件

以下部分描述了控制稳定性 AI 图像服务。

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

Control Sketch 具有以下必需参数:

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

  • image −(字符串)草图的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • control_strength −(数字)图像对生成的影响或控制力有多大。表示为 0 和 1 之间的浮点数,其中 0 表示影响最小,1 表示最大影响。默认值 0.7。

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • style_pr eset-引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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 调用的输入和输出图像:一栋以山脉和河流为背景的房屋,附近有山脉和河流

输入

输出

桑瓦尔·优素福的《房子、山脉和河流素描》。在 CC BY 4.0 下获得许可

控制结构允许您在保持输入图像结构的同时生成图像。这对于高级内容创作场景(例如重新创建场景或渲染模型中的角色)特别有用。

控制结构具有以下必需参数:

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

  • image −(字符串)草图的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • control_strength −(数字)图像对生成的影响或控制力有多大。表示为 0 和 1 之间的浮点数,其中 0 表示影响最小,1 表示最大影响。默认值 0.7。

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • style_pr eset-引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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)

下表使用以下提示显示了控制结构操作的输入和输出图像:超现实的结构和产生的动作火花照亮场景

输入

输出

Pawel L 的《坐在棕色盒子里的人》。 根据 CC 许可

Style Guide 允许您从输入图像中提取风格元素,并使用它来指导根据提示创建输出图像。结果是一张与输入图像风格相同的新图像。

风格指南具有以下必需参数:

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

  • image −(字符串)草图的 Base64 图像。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

  • aspect_rat io −(字符串)控制生成图像的纵横比。此参数仅对 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_promp t-(字符串)描述你不希望在输出图像中看到的内容的文字简介。这是一项高级功能。最多 10000 个字符。

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举:jpeg、png、webp。默认 png。

  • fidelity −(数字)输出图像的样式与输入图像的样式有多相似。范围 0 到 1。默认值 0.5。

  • style_pr eset-引导图像模型朝向特定的样式。枚举:3D 模型、模拟电影、动漫、电影、漫画书、数字艺术、增强、幻想艺术、等距、线条艺术、低多边形、建模化合物、霓虹朋克、折纸、摄影、像素艺术、瓷砖纹理。

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 的输入和输出图像:现代大都市的广角镜头

输入

输出

史蒂芬·约翰逊《抽象画》。根据 CC 许可

Style Transfer 允许您将参考样式图像中的视觉特征应用于目标图像。当 Style Guide 服务从输入图像中提取风格元素并使用它们来指导根据提示创建输出图像时,Style Transfer 会专门转换现有内容,同时保留原始构图。此工具可帮助在多个资产中创建一致的内容。

Style Transfer 具有以下必需参数:

  • init_image-(字符串)一张 Base64 图像,其中包含你想要重新设计的主题。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

  • style_image-(字符串)一张 Base64 图像,其中包含你想要重新设计的主题。图像的每一面都必须至少为 64 像素。总像素数不能超过 9,437,184 像素。图像长宽比必须介于 1:2.5 和 2. 5:1 之间。支持的格式:jpeg、png、webp。

以下参数可选:

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

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

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

  • ou@@ tput_forma t-(字符串)决定生成图像的内容类型。枚举: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 调用的输入和输出图像。

输入

样式

输出

西蒙·伯杰《站立的女人雕像》。根据 CC 许可

Pixab ay 的《蓝色亮光》。根据 CC 0 获得许可