Amazon Titan 模型是一系列多功能生成式 AI 模型的一部分,其中的一些模型支持文本到图像(text-to-image)和图像修复(inpainting)功能。这些功能利用先进的生成技术,可以将文字转换为图像或对图像进行修复和编辑。
本节我们使用Titan模型生成图片
参考Provider的字段:

Text-to-Image 是根据输入的文本描述生成对应的图像。
用户提供自然语言描述(prompt),模型会生成与该描述相关联的图像。
python代码:
import boto3
import json
import base64
client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
# 配置图片生成参数,包括任务类型和具体参数
stability_image_config = json.dumps({
    "taskType": "TEXT_IMAGE",  # 指定任务类型为文本生成图像
    "textToImageParams": {
        "text": "cat on a mat on a country hillside",  # 生成图片所需的文本描述
    },
    "imageGenerationConfig": {
        "numberOfImages": 1,  # 指定生成图片的数量
        "height": 512,        # 图片高度(像素)
        "width": 512,         # 图片宽度(像素)
        "cfgScale": 8.0,      # 配置比例(控制图片生成质量与文本描述的一致性)
    }
})
# 调用 AWS Bedrock 模型生成图片
response = client.invoke_model(
    body=stability_image_config,          # 请求主体,包含图片生成配置
    modelId="amazon.titan-image-generator-v1",  # 使用的模型 ID
    accept="application/json",           # 指定返回内容类型为 JSON
    contentType="application/json"       # 指定请求内容类型为 JSON
)
response_body = json.loads(response.get("body").read())  # 读取并解析响应体
base64_image = response_body.get("images")[0]           # 获取返回的 Base64 编码图片数据
# 将 Base64 编码的图片解码为二进制数据
base_64_image = base64.b64decode(base64_image)
# 将解码后的图片保存为文件
file_path = "cat.png"
with open(file_path, "wb") as f:
    f.write(base_64_image)
print(f"图片已成功保存为: {file_path}")
结果:

Inpainting 功能用于对已有图像进行局部修改或修复。用户提供一张图像和一个遮罩(mask),模型会根据输入的描述填充遮罩区域。
测试:原来生成了两只猫打架的图片,使用inpainting功能对它进行颜色更新

python代码:
import boto3
import json
import base64
client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
# 配置图片生成参数,包括任务类型和具体参数
def get_configuration(inputImage: str):
    return json.dumps({
    "taskType": "INPAINTING",
    "inPaintingParams": {
        "text": "Make the twos cat black and blue",
        "negativeText": "bad quality, low res",
        "image": inputImage,
        "maskPrompt": "cat"
    },
    "imageGenerationConfig": {
        "numberOfImages": 1,
        "height": 512,
        "width": 512,
        "cfgScale": 8.0,
    }
})
with open('cat.png','rb') as f:
    base_image=base64.b64encode(f.read()).decode('utf-8')
# 调用 AWS Bedrock 模型生成图片
response = client.invoke_model(
    body=get_configuration(base_image),          # 请求主体,包含图片生成配置
    modelId="amazon.titan-image-generator-v1",  # 使用的模型 ID
    accept="application/json",           # 指定返回内容类型为 JSON
    contentType="application/json"       # 指定请求内容类型为 JSON
)
response_body = json.loads(response.get("body").read())  # 读取并解析响应体
base64_image = response_body.get("images")[0]           # 获取返回的 Base64 编码图片数据
# 将 Base64 编码的图片解码为二进制数据
base_64_image = base64.b64decode(base64_image)
# 将解码后的图片保存为文件
file_path = "cat.png"
with open(file_path, "wb") as f:
    f.write(base_64_image)
print(f"图片已成功保存为: {file_path}")
结果:

文档参考:
https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html