Amazon Titan Image Generator G1 モデル - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Titan Image Generator G1 モデル

Amazon Titan Image Generator G1 V1および Titan Image Generator G1 V2モデルは、モデル推論を実行するときに、次の推論パラメータとモデルレスポンスをサポートします。

リクエストとレスポンスの形式

Amazon Titan Image Generator モデルを使用して をInvokeModel呼び出す場合は、リクエストの bodyフィールドをユースケースに一致する形式に置き換えます。すべてのタスクは imageGenerationConfig オブジェクトを共有しますが、各タスクにはそのタスク固有のパラメータオブジェクトがあります。次のユースケースがサポートされています。

taskType タスクパラメータフィールド タスクの種類 定義
TEXT_IMAGE textToImageParams [Generation] (生成)

テキストプロンプトを使用して画像を生成します。

TEXT_IMAGE textToImageParams [Generation] (生成)

(イメージコンconditioning-V2 のみ) 追加の入力コンディショニングイメージとテキストプロンプトを指定して、コンディショニングイメージのレイアウトと構成に従うイメージを生成します。

INPAINTING inPaintingParams 編集

周囲の背景に合わせてマスクの内側を変更して画像を変更します。

OUTPAINTING outPaintingParams 編集 マスクで定義された領域をシームレスに拡張して画像を変更します。
IMAGE_VARIATION imageVariationParams 編集 元の画像のバリエーションを作成して画像を変更します。
COLOR_GUIDED_GENERATION (V2 only) colorGuidedGenerationParams [Generation] (生成) 16 進数カラーコードのリストと、カラーパレットに続く画像を生成するテキストプロンプトを指定します。
BACKGROUND_REMOVAL (V2 only) backgroundRemovalParams 編集 複数のオブジェクトを識別して背景を削除し、透明な背景のイメージを出力することで、イメージを変更します。

編集タスクでは、入力時に image フィールドを指定する必要があります。このフィールドは、画像内のピクセルを定義する文字列で構成されます。各ピクセルは 3 つのRGBチャネルで定義され、それぞれが 0~255 の範囲です (例えば、(255 255 0) は黄色を表します)。これらのチャンネルは Base64 でエンコードされます。

使用するイメージは JPEGまたは PNG形式である必要があります。

インペインティングやアウトペインティングを行う場合は、修正する画像の一部を定義するマスク、1 つまたは複数の領域も定義します。次の 2 つの方法のいずれかでマスクを定義できます。

  • maskPrompt — 画像のマスク対象部分を説明するテキストプロンプトを記述します。

  • maskImage — 入力画像内の各ピクセルに (0 0 0) または (255 255 255) とマークして、マスクされた領域を定義する Base64 でエンコードされた文字列を入力します。

    • (0 0 0) と定義されたピクセルは、マスクの内側のピクセルです。

    • (255 255 255) と定義されたピクセルは、マスクの外側のピクセルです。

    写真編集ツールを使用すると、マスクを描画できます。その後、出力JPEGまたはPNGイメージを base64 エンコードに変換して、このフィールドに入力できます。それ以外の場合は、代わりに maskPrompt フィールドを使用してモデルにマスクの推測を許可します。

タブを選択すると、さまざまな画像生成のユースケースとフィールドの説明のAPIリクエスト本文が表示されます。

Text-to-image generation (Request)

イメージを生成するテキストプロンプトは <= 512 文字である必要があります。長い方の解像度 <= 1,408 negativeText (オプション) – <= 512 文字のイメージに含めない内容を定義するテキストプロンプト。解像度の完全なリストについては、以下の表を参照してください。

{ "taskType": "TEXT_IMAGE", "textToImageParams": { "text": "string", "negativeText": "string" }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float, "seed": int } }

textToImageParams フィールドについて以下に説明します。

  • text (必須) — 画像を生成するためのテキストプロンプト。

  • negativeText (オプション) — イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

Inpainting (Request)

text (オプション) — マスクの内側の変更内容を定義するテキストプロンプト。このフィールドを含めないと、モデルはマスク領域全体の背景への置き換えを試行します。<= 512 文字である必要があります。 negativeText (オプション) – イメージに含めない内容を定義するテキストプロンプト。<= 512 文字である必要があります。入力イメージと入力マスクのサイズ制限は、イメージの長い側で <= 1,408 です。出力サイズは入力サイズと同じです。

{ "taskType": "INPAINTING", "inPaintingParams": { "image": "base64-encoded string", "text": "string", "negativeText": "string", "maskPrompt": "string", "maskImage": "base64-encoded string", "returnMask": boolean # False by default }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

inPaintingParams フィールドについて以下に説明します。マスクは画像の中で修正する部分を定義します。

  • image (必須) – 変更する JPEGまたは PNG イメージ。ピクセルのシーケンスを指定する文字列としてフォーマットされ、それぞれRGBが値で定義され、base64 でエンコードされます。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • 定義するには、次のいずれかのフィールド (両方ではない) を定義する必要があります。

    • maskPrompt – マスクを定義するテキストプロンプト。

    • maskImage – と同じサイズのピクセルシーケンスを指定してマスクを定義する文字列image。各ピクセルは、 (0 0 0) (マスク内のピクセル) または (255 255 255) (マスク外のピクセル) RGBの値になります。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • text (オプション) — マスクの内側の変更内容を定義するテキストプロンプト。このフィールドを含めないと、モデルはマスク領域全体の背景への置き換えを試行します。

  • negativeText (オプション) — イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

Outpainting (Request)

text (必須) — マスクの外側の変更内容を定義するテキストプロンプト。<= 512 文字である必要があります。 negativeText (オプション) – イメージに含めない内容を定義するテキストプロンプト。<= 512 文字である必要があります。入力イメージと入力マスクのサイズ制限は、イメージの長い側で <= 1,408 です。出力サイズは入力サイズと同じです。

{ "taskType": "OUTPAINTING", "outPaintingParams": { "text": "string", "negativeText": "string", "image": "base64-encoded string", "maskPrompt": "string", "maskImage": "base64-encoded string", "returnMask": boolean, # False by default "outPaintingMode": "DEFAULT | PRECISE" }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

outPaintingParams フィールドについて以下に説明します。マスクは画像の中で修正しない部分を定義します。生成によって、定義する領域がシームレスに拡張されます。

  • image (必須) – 変更する JPEGまたは PNG イメージ。ピクセルのシーケンスを指定する文字列としてフォーマットされ、それぞれRGBが値で定義され、base64 でエンコードされます。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • 定義するには、次のいずれかのフィールド (両方ではない) を定義する必要があります。

    • maskPrompt – マスクを定義するテキストプロンプト。

    • maskImage – と同じサイズのピクセルシーケンスを指定してマスクを定義する文字列image。各ピクセルは、 (0 0 0) (マスク内のピクセル) または (255 255 255) (マスク外のピクセル) RGBの値になります。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • text (必須) — マスクの外側の変更内容を定義するテキストプロンプト。

  • negativeText (オプション) — イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

  • outPaintingMode – マスク内のピクセルの変更を許可するかどうかを指定します。以下の値を指定できます。

    • DEFAULT - 再構築された背景との一貫性を保つために、マスク内のイメージを変更できるようにするには、このオプションを使用します。

    • PRECISE - マスク内のイメージが変更されないようにするには、このオプションを使用します。

Image variation (Request)

イメージバリエーションを使用すると、パラメータ値に基づいて元のイメージのバリエーションを作成できます。入力イメージのサイズ制限は、イメージの長い側で <= 1,408 です。解像度の完全なリストについては、以下の表を参照してください。

  • text (オプション) — 画像の内側の保存する内容と変更する内容を定義できるテキストプロンプト。<= 512 文字である必要があります。

  • negativeText (オプション) – イメージに含めない内容を定義するテキストプロンプト。<= 512 文字である必要があります。

  • text (オプション) — 画像の内側の保存する内容と変更する内容を定義できるテキストプロンプト。<= 512 文字である必要があります。

  • similarityStrength (オプション) – 生成されたイメージを入力イメージとどの程度類似しているかを指定します (小さい値を使用して生成にランダム性を追加します)。使用できる範囲は 0.2~1.0 (両方を含む) ですが、このパラメータがリクエストにない場合、デフォルトの 0.7 が使用されます。

{ "taskType": "IMAGE_VARIATION", "imageVariationParams": { "text": "string", "negativeText": "string", "images": ["base64-encoded string"], "similarityStrength": 0.7, # Range: 0.2 to 1.0 }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

imageVariationParams フィールドについて以下に説明します。

  • images (必須) — バリエーションを生成する画像のリスト。1~5 個のイメージを含めることができます。イメージは、base64 でエンコードされたイメージ文字列として定義されます。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • text (オプション) — 画像の内側の保存する内容と変更する内容を定義できるテキストプロンプト。

  • similarityStrength (オプション) — 生成されたイメージを入力イメージとどの程度類似させるかを指定します (複数可)。0.2 から 1.0 の範囲では、ランダム性を高めるために使用される値が小さくなります。

  • negativeText (オプション) — イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

Conditioned Image Generation (Request) V2 only

条件付きイメージ生成タスクタイプを使用すると、「条件付きイメージ」を提供することで text-to-image 生成を強化し、生成されたイメージをよりきめ細かく制御できます。

  • Canny エッジ検出

  • セグメンテーションマップ

イメージを生成するテキストプロンプトは <= 512 文字である必要があります。長い方の解像度 <= 1,408 negativeText (オプション) は、イメージに含めない内容を定義するテキストプロンプトで、<= 512 文字です。解像度の完全なリストについては、以下の表を参照してください。

{ "taskType": "TEXT_IMAGE", "textToImageParams": { "text": "string", "negativeText": "string", "conditionImage": "base64-encoded string", # [OPTIONAL] base64 encoded image "controlMode": "string", # [OPTIONAL] CANNY_EDGE | SEGMENTATION. DEFAULT: CANNY_EDGE "controlStrength": float # [OPTIONAL] weight given to the condition image. DEFAULT: 0.7 }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float, "seed": int } }
  • text (必須) — 画像を生成するためのテキストプロンプト。

  • negativeText (オプション) – イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

  • conditionImage (オプション - V2 のみ) - 生成された画像のレイアウトと構成をガイドする単一の入力コンディショニングイメージ。イメージは、base64 でエンコードされたイメージ文字列として定義されます。イメージを base64 にエンコードし、base64 でエンコードされた文字列をデコードしてイメージに変換する方法の例については、「」を参照してください。

  • controlMode (オプション - V2 のみ) - 使用するコンディショニングモードのタイプを指定します。CANNY_EDGE と の 2 種類の条件モードがサポートされていますSEGMENTATION。デフォルト値は CANNY_ ですEDGE。

  • controlStrength (オプション - V2 のみ) — 生成されたイメージのレイアウトと構成が とどの程度類似しているかを指定しますconditioningImage。0 から 1.0 の範囲では、ランダム性を高めるために使用される値が小さくなります。デフォルト値は 0.7 です。

注記

controlMode または controlStrength が指定されている場合は、 conditionImage も提供する必要があります。

Color Guided Content (Request) V2 only

16 進数カラーコードのリストと、カラーパレットに続く画像を生成するテキストプロンプトを指定します。イメージを生成するには、テキストプロンプトが <= 512 文字である必要があります。最大解像度は、長い側で 1,408 です。生成されたイメージに色を指定するには 1~10 の 16 進数カラーコードのリストが必要です。 negativeText オプション イメージに含めない内容を定義するテキストプロンプト <= 512 文字。追加のリファレンスイメージ referenceImage はオプションで、生成イメージのカラーパレットをガイドします。ユーザーがアップロードしたRGBリファレンスイメージのサイズ制限は、長い方で <= 1,408 です。

{ "taskType": "COLOR_GUIDED_GENERATION", "colorGuidedGenerationParams": { "text": "string", "negativeText": "string", "referenceImage" "base64-encoded string", # [OPTIONAL] "colors": ["string"] # list of color hex codes }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float, "seed": int } }

colorGuidedGenerationParams フィールドについては、以下で説明します。このパラメータは V2 専用であることに注意してください。

  • text (必須) — 画像を生成するためのテキストプロンプト。

  • colors (必須) – 生成されたイメージの色を指定する最大 10 進数のカラーコードのリスト。

  • negativeText (オプション) — イメージに含めない内容を定義するテキストプロンプト。

    注記

    negativeText プロンプトには否定語を使わないでください。例えば、画像に鏡を含めない場合は、negativeText プロンプトに「mirrors」と入力します。no mirrors とは入力しないでください。

  • referenceImage (オプション) — 生成されたイメージの色パレットをガイドする単一の入力リファレンスイメージ。イメージは、base64 でエンコードされたイメージ文字列として定義されます。

Background Removal (Request)

バックグラウンド削除タスクタイプは、入力イメージ内の複数のオブジェクトを自動的に識別し、バックグラウンドを削除します。出力イメージの背景は透明です。

リクエスト形式

{ "taskType": "BACKGROUND_REMOVAL", "backgroundRemovalParams": { "image": "base64-encoded string" } }

レスポンス形式

{ "images": [ "base64-encoded string", ... ], "error": "string" }

backgroundRemovalParams フィールドの説明を以下に示します。

  • image (必須) – 変更する JPEGまたは PNG イメージ。ピクセルのシーケンスを指定する文字列としてフォーマットされ、それぞれRGBが値で定義され、base64 でエンコードされます。

Response body
{ "images": [ "base64-encoded string", ... ], "error": "string" }

レスポンスの本文は、以下のフィールドのいずれかを含むストリーミングオブジェクトです。

  • images — リクエストが正常に終了すると、それぞれが生成画像を定義する base64 エンコード後の文字列のリストであるこのフィールドが返されます。各イメージは、ピクセルのシーケンスを指定する文字列としてフォーマットされ、それぞれがRGB値で定義され、base64 でエンコードされます。画像を Base64 にエンコードし、Base64 でエンコードされた文字列をデコードして画像に変換する方法の例については、コード例を参照してください。

  • error — 以下の状況のいずれかでリクエストがコンテンツモデレーションポリシーに違反すると、このフィールドにメッセージが返されます。

    • 入力テキスト、画像、またはマスク画像がコンテンツモデレーションポリシーによってフラグ付けされている場合。

    • 少なくとも 1 つの出力画像がコンテンツモデレーションポリシーによってフラグ付けされている場合

共有およびオプションの imageGenerationConfig には、次のフィールドが含まれています。このオブジェクトを含めない場合は、デフォルト設定が使用されます。

  • numberOfImages (オプション) - 生成するイメージの数。

    最小値 最大値 デフォルト値
    1 5 1
  • cfgScale (オプション) — 生成されたイメージがプロンプトに従う強度を指定します。低い値を使用すると、生成時のランダム性が高くなります。

    最小値 最大値 デフォルト値
    1.1 10.0 8.0
  • 以下のパラメータは、出力画像に必要なサイズを定義します。画像サイズ別の料金の詳細については、「Amazon Bedrock の料金」を参照してください。

    • height (オプション) - 画像の高さ (ピクセル単位)。デフォルト値は 1408 です。

    • width (オプション) - 画像の幅 (ピクセル単位)。デフォルト値は 1408 です。

    以下のサイズが許容されます。

    高さ アスペクト比 料金が同じサイズ
    1024 1024 1:1 1024 x 1024
    768 768 1:1 512 x 512
    512 512 1:1 512 x 512
    768 1152 2:3 1024 x 1024
    384 576 2:3 512 x 512
    1152 768 3:2 1024 x 1024
    576 384 3:2 512 x 512
    768 1280 3:5 1024 x 1024
    384 640 3:5 512 x 512
    1280 768 5:3 1024 x 1024
    640 384 5:3 512 x 512
    896 1152 7:9 1024 x 1024
    448 576 7:9 512 x 512
    1152 896 9:7 1024 x 1024
    576 448 9:7 512 x 512
    768 1408 6:11 1024 x 1024
    384 704 6:11 512 x 512
    1408 768 11:6 1024 x 1024
    704 384 11:6 512 x 512
    640 1408 5:11 1024 x 1024
    320 704 5:11 512 x 512
    1408 640 11:5 1024 x 1024
    704 320 11:5 512 x 512
    1152 640 9:5 1024 x 1024
    1173 640 16:9 1024 x 1024
  • seed (オプション) — 結果の制御と再現に使用します。初期ノイズ設定を指定します。前回の実行と同じシードと設定を使用して推論を行えば、類似の画像を作成できます。

    最小値 最大値 デフォルト値
    0 2,147,483,646 0

コードの例

次の例は、Python でオンデマンドスループットを使用して Amazon Titan Image Generator モデルを呼び出す方法を示していますSDK。各ユースケースの例を表示するには、タブを選択してください。各例の最後に画像が表示されます。

Text-to-image generation
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image from a text prompt with the Amazon Titan Image Generator G1 model (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator G1" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator G1 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator G1 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator G1 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator G1 example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' prompt = """A photograph of a cup of coffee from the side.""" body = json.dumps({ "taskType": "TEXT_IMAGE", "textToImageParams": { "text": prompt }, "imageGenerationConfig": { "numberOfImages": 1, "height": 1024, "width": 1024, "cfgScale": 8.0, "seed": 0 } }) try: image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator G1 model {model_id}.") if __name__ == "__main__": main()
Inpainting
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to use inpainting to generate an image from a source image with the Amazon Titan Image Generator G1 model (on demand). The example uses a mask prompt to specify the area to inpaint. """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator G1" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator G1 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator G1 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator G1 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator G1 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "INPAINTING", "inPaintingParams": { "text": "Modernize the windows of the house", "negativeText": "bad quality, low res", "image": input_image, "maskPrompt": "windows" }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator G1 model {model_id}.") if __name__ == "__main__": main()
Outpainting
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to use outpainting to generate an image from a source image with the Amazon Titan Image Generator G1 model (on demand). The example uses a mask image to outpaint the original image. """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator G1" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator G1 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator G1 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator G1 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator G1 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image and mask image from file and encode as base64 strings. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') with open("/path/to/mask_image", "rb") as mask_image_file: input_mask_image = base64.b64encode( mask_image_file.read()).decode('utf8') body = json.dumps({ "taskType": "OUTPAINTING", "outPaintingParams": { "text": "Draw a chocolate chip cookie", "negativeText": "bad quality, low res", "image": input_image, "maskImage": input_mask_image, "outPaintingMode": "DEFAULT" }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } } ) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator G1 model {model_id}.") if __name__ == "__main__": main()
Image variation
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image variation from a source image with the Amazon Titan Image Generator G1 model (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator G1" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator G1 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator G1 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator G1 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator G1 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "IMAGE_VARIATION", "imageVariationParams": { "text": "Modernize the house, photo-realistic, 8k, hdr", "negativeText": "bad quality, low resolution, cartoon", "images": [input_image], "similarityStrength": 0.7, # Range: 0.2 to 1.0 }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator G1 model {model_id}.") if __name__ == "__main__": main()
Image conditioning (V2 only)
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate image conditioning from a source image with the Amazon Titan Image Generator G1 V2 model (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator V2" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator V2 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator V2 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator V2 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator V2 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v2:0' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "TEXT_IMAGE", "textToImageParams": { "text": "A robot playing soccer, anime cartoon style", "negativeText": "bad quality, low res", "conditionImage": input_image, "controlMode": "CANNY_EDGE" }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator V2 model {model_id}.") if __name__ == "__main__": main()
Color guided content (V2 only)
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image from a source image color palette with the Amazon Titan Image Generator G1 V2 model (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator V2" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator V2 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator V2 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator V2 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator V2 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v2:0' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "COLOR_GUIDED_GENERATION", "colorGuidedGenerationParams": { "text": "digital painting of a girl, dreamy and ethereal, pink eyes, peaceful expression, ornate frilly dress, fantasy, intricate, elegant, rainbow bubbles, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration", "negativeText": "bad quality, low res", "referenceImage": input_image, "colors": ["#ff8080", "#ffb280", "#ffe680", "#ffe680"] }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator V2 model {model_id}.") if __name__ == "__main__": main()
Background removal (V2 only)
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image with background removal with the Amazon Titan Image Generator G1 V2 model (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by Amazon Titan Image Generator V2" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using Amazon Titan Image Generator V2 model on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info( "Generating image with Amazon Titan Image Generator V2 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) base64_image = response_body.get("images")[0] base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Image generation error. Error is {finish_reason}") logger.info( "Successfully generated image with Amazon Titan Image Generator V2 model %s", model_id) return image_bytes def main(): """ Entrypoint for Amazon Titan Image Generator V2 example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v2:0' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "BACKGROUND_REMOVAL", "backgroundRemovalParams": { "image": input_image, } }) image_bytes = generate_image(model_id=model_id, body=body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating image with Amazon Titan Image Generator V2 model {model_id}.") if __name__ == "__main__": main()