本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
穩定性 AI 映像服務
您可以將穩定性 AI Image Services 與 Amazon Bedrock 搭配使用,以存取九種專門的影像編輯工具,旨在加速專業創意工作流程。透過穩定性 AI Image Services,您可以從草圖產生映像、重組和重新建構現有映像,或移除和取代映像中的物件。
本節說明如何使用 InvokeModel 對穩定性 AI Image Services 進行推論呼叫。本節也提供 Python 中的程式碼範例,以及使用穩定性 AI Image Services 前後的影像範例。
穩定性 AI Image Services 提供下列類別:
訂閱任何編輯或控制穩定性 AI Image Service 會自動將您註冊到所有九個可用的穩定性 AI Image Services。
請求和回應
請求內文會在請求的 body
欄位中傳遞給 InvokeModel。
模型調用請求內文欄位
當您使用穩定性 AI Image Services 進行 InvokeModel 呼叫時,請以如下所示的 JSON 物件填入內文欄位。
{
'prompt': 'Create an image of a panda'
}
模型調用回應內文欄位
當您使用穩定性 AI Image Services 進行 InvokeModel 呼叫時,回應如下所示
{
'seeds': [2130420379],
'finish_reasons': [null],
'images': ['...']
}
seeds – (字串) 用於為模型產生映像的種子清單。
-
finish_reasons – Enum 指出請求是否已篩選。 null
會指出請求成功。目前可能的值:"Filter reason: prompt", "Filter reason: output image", "Filter reason: input image", "Inference error", null
。
-
images – base64 字串格式的產生映像清單。
如需詳細資訊,請參閱 https://https://platform.us.stability.ai/docs/api-reference#tag/v1generation。
編輯
下一節說明編輯穩定性 AI Image Services。
Inpaint 會根據遮罩影像的內容,將指定的區域填入或取代為新內容,以智慧方式修改影像。
Inpaint 具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您要控制權重的單字,而權重是值。值 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_preset ‐ (字串) 將影像模型導向特定樣式。Enum: 3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、等角、線藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。列舉:jpeg、png、Webp。預設 png。
mask ‐ (字串) 透過第二個影像 (傳遞至此參數) 或透過影像參數的 Alpha 通道,以每個像素為基礎控制注入程序的強度。
傳入遮罩 - 傳遞至此參數的影像應該是黑色和白色影像,代表根據指定像素的暗度或亮度,在任何像素上所繪製的強度。完全黑色像素表示沒有注入強度,而完全白色像素表示最大強度。如果遮罩的大小與影像參數不同,則會自動調整大小。
Alpha 通道支援 - 如果您未提供明確的遮罩,則會從映像參數的 Alpha 通道衍生。透明像素會進行繪製,而不透明像素則會保留。如果具有 Alpha 頻道的影像與遮罩一起提供,則遮罩將優先。
grow_mask - 依指定的像素數,從各個方向向外生長遮罩邊緣。遮罩周圍的展開區域將會模糊,這有助於平滑上色內容與原始影像之間的轉換。範圍介於 0 到 20 之間。預設 5。如果您注意到繪製內容周圍的接縫或粗略邊緣,請嘗試此參數。請注意,過度成長可能會遮蔽遮罩和/或合併附近的遮罩區域中的精細細節。
- 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 產生的「Man in metropolis」,Swal Yousaf 提示和編輯。根據 CC BY 4.0 授權
|
|
|
搜尋和重新著色可讓您使用提示變更影像中特定物件的顏色。此服務是不需要遮罩的特定注入版本。它會自動分割物件,並使用提示中請求的顏色來重新著色物件。
搜尋和重新著色具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您要控制權重的單字,而權重是值。值 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_prompt ‐ (字串) 影像中要搜尋內容的簡短描述。最多 10000 個字元。
下列是選用參數:
style_preset ‐ (字串) 將影像模型導向特定樣式。Enum: 3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、等角、線藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。列舉:jpeg、png、Webp。預設 png。
grow_mask - 依指定的像素數,從各個方向向外生長遮罩邊緣。遮罩周圍的展開區域將會模糊,這有助於平滑上色內容與原始影像之間的轉換。範圍介於 0 到 20 之間。預設 5。如果您注意到繪製內容周圍的接縫或粗略邊緣,請嘗試此參數。請注意,過度成長可能會遮蔽遮罩和/或合併附近的遮罩區域中的精細細節。
- 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 產生的「穿著泡泡夾克的男性」,Swal Yousaf 提示和編輯。根據 CC BY 4.0 授權
|
|
搜尋和取代可讓您使用搜尋提示,以簡單語言識別要取代的物件。此服務會自動分割物件,並將其取代為提示中請求的物件,而不需要遮罩。
搜尋和取代具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您想要控制權重的單字,而權重是值。值 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_prompt ‐ (字串) 影像中要填入內容的簡短描述。最多 10000 個字元。
下列是選用參數:
style_preset ‐ (字串) 將影像模型導向特定樣式。Enum:3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、對稱、線條藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
grow_mask - 以指定的像素數,從各個方向向外生長遮罩邊緣。遮罩周圍的展開區域將會模糊,這有助於平滑上色內容與原始影像之間的轉換。範圍介於 0 到 20 之間。預設 5。如果您注意到已上色內容周圍的接縫或粗略邊緣,請嘗試此參數。請注意,過度成長可能會遮蔽遮罩和/或合併附近的遮罩區域中的精細細節。
- 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)
下表顯示使用下列提示搜尋和取代操作的輸入和輸出影像:夾克。
輸入
|
輸出
|
|
|
由 Stable Image Ultra 產生的「戴著下降毛線的女性模型」。Sanwal Yousaf 的提示和編輯。根據 CC BY 4.0 授權
|
|
清除可讓您使用影像遮罩移除不需要的元素,同時智慧地維持背景一致性。
清除具有下列必要參數:
下列是選用參數:
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
mask ‐ (字串) 透過第二個影像 (傳遞至此參數) 或透過影像參數的 Alpha 通道,以每個像素為基礎控制注入程序的強度。
傳入遮罩 - 傳遞至此參數的影像應該是黑色和白色影像,代表根據指定像素的暗度或亮度,在任何像素上所繪製的強度。完全黑色像素表示沒有注入強度,而完全白色像素表示最大強度。如果遮罩的大小與影像參數不同,則會自動調整大小。
Alpha 通道支援 - 如果您未提供明確的遮罩,則會從影像參數的 Alpha 通道衍生。透明像素將繪製,而不透明像素將保留。如果具有 Alpha 頻道的影像與遮罩一起提供,則遮罩將優先。
grow_mask - 以指定的像素數,從各個方向向外生長遮罩邊緣。遮罩周圍的展開區域將會模糊,這有助於平滑上色內容與原始影像之間的轉換。範圍介於 0 到 20 之間。預設 5。如果您注意到已上色內容周圍的接縫或粗略邊緣,請嘗試此參數。請注意,過度成長可能會遮蔽遮罩和/或合併附近的遮罩區域中的精細細節。
為了獲得最佳清除結果,請確定您的遮罩準確定義要移除的區域。如果未提供明確的遮罩,服務將使用輸入影像的 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 授權
|
|
|
移除背景可讓您精確地隔離主體與背景。
移除背景具有下列必要參數:
下列是選用參數:
- 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 產生的「穿著 fall 毛線衣的女性模型」。Sanwal Yousaf 的提示和編輯。根據 CC BY 4.0 授權
|
|
控制項
下一節說明控制穩定性 AI Image Services。
使用精確的控制,將粗略的手動繪製草圖升級至精細的輸出。對於非草圖影像,Control Sketch 允許透過利用影像中的等高線線條和邊緣來詳細處理最終外觀。
Control Sketch 具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您想要控制權重的單字,而權重是值。值 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 ‐ (number) 影像對世代的影響或控制程度。以介於 0 和 1 之間的浮點數表示,其中 0 是最小影響,1 是最大值。預設 0.7。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
style_preset - 將影像模型導向特定樣式。Enum:3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、對稱、線性藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
- 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 呼叫的輸入和輸出影像:背景為山巒和河流的房屋在附近流動。
輸入
|
輸出
|
|
|
作者為 Sanwal Yousaf 的 "House, mountains, and River sketch"。根據 CC BY 4.0 授權
|
|
控制結構可讓您產生影像,同時維持輸入影像的結構。這對於進階內容建立案例特別有用,例如從模型重新建立場景或轉譯角色。
控制結構具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您想要控制權重的單字,而權重是值。值 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 ‐ (number) 影像對世代的影響或控制程度。以介於 0 和 1 之間的浮點數表示,其中 0 是最小影響,1 是最大值。預設 0.7。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
style_preset - 將影像模型導向特定樣式。Enum:3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、對稱、線性藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
- 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)
下表顯示使用下列提示的控制結構操作的輸入和輸出影像:具有動作產生的超現實結構會照亮場景。
樣式指南可讓您從輸入影像擷取風格元素,並根據提示使用它來引導建立輸出影像。結果是與輸入影像具有相同樣式的新影像。
樣式指南具有下列必要參數:
提示 - 您希望在輸出映像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您想要控制權重的單字,而權重是值。值 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_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 ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
保真度 ‐ (數字) 輸出影像的樣式與輸入影像的樣式有多接近。範圍 0 到 1。預設 0.5。
style_preset - 將影像模型導向特定樣式。Enum:3d-model、類比影片、anime、電影、漫畫書、數位藝術、增強、幻想藝術、對稱、線性藝術、低聚、建模複合、霓虹像素、origami、相片、像素藝術、圖磚紋理。
- 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 服務從輸入影像中擷取風格元素,並使用它們根據提示來引導建立輸出影像,但 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。
下列是選用參數:
prompt ‐ (字串) 您希望在輸出影像中看到的內容。強烈的描述性提示,可清楚定義元素、顏色和主題,進而獲得更好的結果。若要控制指定單字的權重,請使用 格式 (word:weight),其中單字是您想要控制權重的單字,而權重是值。值 0 和 1.0 取消強調單字,而值介於 1.1 和 2 之間,則強調單字 。例如:天空是清晰的 (藍色:0.3),而 (綠色:1.8) 會傳達藍色和綠色,但比藍色更綠色的天空。
negative_prompt ‐ (字串) 說明您不希望在輸出影像中看到內容的文字模糊。這是進階功能。最多 10000 個字元。
seed ‐ (number) 用來引導產生之「隨機度」的特定值。(省略此參數或傳遞 0 以使用隨機種子。) 範圍從 0 到 4294967294。預設 0。
output_format - (字串) 口述所產生影像的內容類型。Enum:jpeg、png、Webp。預設 png。
composition_fidelity ‐ (number) 輸出影像樣式與輸入影像樣式的相似度。範圍介於 0 到 1 之間。預設 0.9。
style_strength ‐ (number) 有時稱為雜訊消除,此參數會控制 style_image 參數對產生影像的影響程度。值 0 會產生與輸入相同的影像。值 1 就像您完全沒有傳入影像一樣。範圍介於 0 到 1 之間。預設 1。
change_strength ‐ (number) 原始映像應該變更多少。範圍介於 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 呼叫的輸入和輸出映像。