

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Model Amazon Titan Image Generator G1
<a name="model-parameters-titan-image"></a>

Amazon Titan Image Generator G1 V1 dan Titan Image Generator G1 V2 model mendukung parameter inferensi dan respons model berikut saat melakukan inferensi model.

**Topics**
+ [Parameter inferensi](#model-parameters-titan-image-api)
+ [Contoh](#model-parameters-titan-image-code-examples)

## Parameter inferensi
<a name="model-parameters-titan-image-api"></a>

Saat Anda melakukan [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html)panggilan menggunakan model Amazon Titan Image Generator, ganti `body` bidang permintaan dengan format yang cocok dengan kasus penggunaan Anda. Semua tugas berbagi `imageGenerationConfig` objek, tetapi setiap tugas memiliki objek parameter khusus untuk tugas itu. Kasus penggunaan berikut didukung.


****  

| taskType | Bidang parameter tugas | Jenis tugas | Definisi | 
| --- | --- | --- | --- | 
| TEXT\_IMAGE | textToImageParams | Generasi | Hasilkan gambar menggunakan prompt teks. | 
| TEXT\_IMAGE | textToImageParams | Generasi | (Hanya pengkondisian gambar-v2) Berikan gambar pengkondisian input tambahan bersama dengan prompt teks untuk menghasilkan gambar yang mengikuti tata letak dan komposisi gambar pengkondisian.  | 
| INPAINTING | inPaintingParams | Mengedit | Ubah gambar dengan mengubah bagian dalam *topeng* agar sesuai dengan latar belakang sekitarnya. | 
| OUTPAINTING | outPaintingParams | Mengedit | Ubah gambar dengan memperluas wilayah yang ditentukan oleh topeng secara mulus. | 
| IMAGE\_VARIATION | imageVariationParams | Mengedit | Memodifikasi gambar dengan menghasilkan variasi gambar asli. | 
| COLOR\_GUIDED\_GENERATION (V2 only) | colorGuidedGenerationParams | Generasi | Berikan daftar kode warna hex bersama dengan prompt teks untuk menghasilkan gambar yang mengikuti palet warna. | 
| BACKGROUND\_REMOVAL (V2 only) | backgroundRemovalParams | Mengedit | Ubah gambar dengan mengidentifikasi beberapa objek dan menghapus latar belakang, mengeluarkan gambar dengan latar belakang transparan. | 

Tugas pengeditan membutuhkan `image` bidang di input. Bidang ini terdiri dari string yang mendefinisikan piksel dalam gambar. Setiap piksel ditentukan oleh 3 saluran RGB, yang masing-masing berkisar dari 0 hingga 255 (misalnya, (255 255 0) akan mewakili warna kuning). Saluran ini dikodekan dalam base64.

Gambar yang Anda gunakan harus dalam format JPEG atau PNG.

Jika Anda melakukan inpainting atau outpainting, Anda juga menentukan *mask*, Region atau Region yang menentukan bagian gambar yang akan dimodifikasi. Anda dapat mendefinisikan topeng dengan salah satu dari dua cara.
+ `maskPrompt`— Tulis prompt teks untuk menggambarkan bagian gambar yang akan disamarkan.
+ `maskImage`— Masukkan string berenkode base64 yang mendefinisikan Daerah bertopeng dengan menandai setiap piksel dalam gambar input sebagai (0 0 0) atau (255 255 255).
  + Piksel yang didefinisikan sebagai (0 0 0) adalah piksel di dalam topeng.
  + Piksel yang didefinisikan sebagai (255 255 255) adalah piksel di luar topeng.

  Anda dapat menggunakan alat pengeditan foto untuk menggambar topeng. Anda kemudian dapat mengonversi output gambar JPEG atau PNG menjadi pengkodean base64 untuk dimasukkan ke dalam bidang ini. Jika tidak, gunakan `maskPrompt` bidang sebagai gantinya untuk memungkinkan model menyimpulkan topeng.

Pilih tab untuk melihat badan permintaan API untuk kasus penggunaan pembuatan gambar yang berbeda dan penjelasan bidang.

------
#### [ Text-to-image generation (Request) ]

Prompt teks untuk menghasilkan gambar harus <= 512 karakter. Resolusi <= 1,408 di sisi yang lebih panjang. negativeText (Opsional) - Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar yang <= 512 karakter. Lihat tabel di bawah ini untuk daftar lengkap resolusi.

```
{
    "taskType": "TEXT_IMAGE",
    "textToImageParams": {
        "text": "string",      
        "negativeText": "string"
    },
    "imageGenerationConfig": {
        "quality": "standard" | "premium",
        "numberOfImages": int,
        "height": int,
        "width": int,
        "cfgScale": float,
        "seed": int
    }
}
```

`textToImageParams`Bidang dijelaskan di bawah ini.
+ **text** (Wajib) — Sebuah prompt teks untuk menghasilkan gambar.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.

------
#### [ Inpainting (Request) ]

text (Opsional) — Sebuah prompt teks untuk menentukan apa yang harus diubah di dalam topeng. Jika Anda tidak menyertakan bidang ini, model mencoba mengganti seluruh area topeng dengan latar belakang. Harus <= 512 karakter. negativeText (Opsional) - Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter. Batas ukuran untuk gambar input dan masukan mask adalah <= 1.408 pada sisi gambar yang lebih panjang. Ukuran output sama dengan ukuran input.

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

`inPaintingParams`Bidang dijelaskan di bawah ini. *Topeng* mendefinisikan bagian dari gambar yang ingin Anda modifikasi.
+ **image** (Required) - Gambar JPEG atau PNG untuk dimodifikasi, diformat sebagai string yang menentukan urutan piksel, masing-masing didefinisikan dalam nilai RGB dan dikodekan dalam base64. [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ Anda harus menentukan salah satu bidang berikut (tetapi tidak keduanya) untuk menentukan.
  + **MaskPrompt** — Sebuah prompt teks yang mendefinisikan topeng.
  + **MaskImage** — String yang mendefinisikan topeng dengan menentukan urutan piksel yang ukurannya sama dengan. `image` Setiap piksel diubah menjadi nilai RGB (0 0 0) (piksel di dalam topeng) atau (255 255 255) (piksel di luar topeng). [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ **text** (Opsional) — Sebuah prompt teks untuk menentukan apa yang harus diubah di dalam topeng. Jika Anda tidak menyertakan bidang ini, model mencoba mengganti seluruh area topeng dengan latar belakang.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.

------
#### [ Outpainting (Request) ]

text (Required) — Sebuah prompt teks untuk menentukan apa yang harus diubah di luar topeng. Harus <= 512 karakter. negativeText (Opsional) - Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter. Batas ukuran untuk gambar input dan masukan mask adalah <= 1.408 pada sisi gambar yang lebih panjang. Ukuran output sama dengan ukuran input. 

```
{
    "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": {
        "quality": "standard" | "premium",
        "numberOfImages": int,
        "height": int,
        "width": int,
        "cfgScale": float
    }
}
```

`outPaintingParams`Bidang didefinisikan di bawah ini. *Topeng* mendefinisikan Wilayah dalam gambar yang tidak ingin Anda modifikasi. Generasi dengan mulus memperluas Wilayah yang Anda tentukan.
+ **image** (Required) - Gambar JPEG atau PNG untuk dimodifikasi, diformat sebagai string yang menentukan urutan piksel, masing-masing didefinisikan dalam nilai RGB dan dikodekan dalam base64. [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ Anda harus menentukan salah satu bidang berikut (tetapi tidak keduanya) untuk menentukan.
  + **MaskPrompt** — Sebuah prompt teks yang mendefinisikan topeng.
  + **MaskImage** — String yang mendefinisikan topeng dengan menentukan urutan piksel yang ukurannya sama dengan. `image` Setiap piksel diubah menjadi nilai RGB (0 0 0) (piksel di dalam topeng) atau (255 255 255) (piksel di luar topeng). [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ **text** (Required) — Sebuah prompt teks untuk menentukan apa yang harus diubah di luar topeng.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.
+ **outPaintingMode**- Menentukan apakah untuk memungkinkan modifikasi piksel di dalam topeng atau tidak. Nilai-nilai berikut dimungkinkan.
  + DEFAULT - Gunakan opsi ini untuk memungkinkan modifikasi gambar di dalam topeng agar tetap konsisten dengan latar belakang yang direkonstruksi.
  + PRECISE — Gunakan opsi ini untuk mencegah modifikasi gambar di dalam topeng.

------
#### [ Image variation (Request) ]

Variasi gambar memungkinkan Anda untuk membuat variasi gambar asli Anda berdasarkan nilai parameter. Batas ukuran untuk gambar input adalah <= 1.408 pada sisi gambar yang lebih panjang. Lihat tabel di bawah ini untuk daftar lengkap resolusi. 
+ teks (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar. Harus <= 512 karakter.
+ NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter.
+ teks (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar. Harus <= 512 karakter.
+ SimilarityStrength (Opsional) - Menentukan seberapa mirip gambar yang dihasilkan seharusnya dengan gambar input Gunakan nilai yang lebih rendah untuk memperkenalkan lebih banyak keacakan dalam pembuatan. Rentang yang diterima adalah antara 0,2 dan 1,0 (keduanya inklusif), sedangkan default 0,7 digunakan jika parameter ini tidak ada dalam permintaan.

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

`imageVariationParams`Bidang didefinisikan di bawah ini.
+ **gambar** (Wajib) - Daftar gambar untuk menghasilkan variasi. Anda dapat menyertakan 1 hingga 5 gambar. Gambar didefinisikan sebagai string gambar yang dikodekan base64. [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ **teks** (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar.
+ **SimilarityStrength** (Opsional) - Menentukan seberapa mirip gambar yang dihasilkan harus dengan gambar masukan (s). Rentang 0,2 hingga 1,0 dengan nilai yang lebih rendah digunakan untuk memperkenalkan lebih banyak keacakan.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.

------
#### [ Conditioned Image Generation (Request) V2 only ]

Jenis tugas pembuatan gambar yang dikondisikan memungkinkan pelanggan untuk menambah text-to-image generasi dengan menyediakan “gambar kondisi” untuk mencapai kontrol yang lebih halus atas gambar yang dihasilkan.
+ Deteksi tepi canny
+ Peta segmentasi

Prompt teks untuk menghasilkan gambar harus <= 512 karakter. Resolusi <= 1,408 di sisi yang lebih panjang. negativeText (Opsional) adalah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar dan <= 512 karakter. Lihat tabel di bawah ini untuk daftar lengkap resolusi.

```
{
    "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": {
        "quality": "standard" | "premium",
        "numberOfImages": int,
        "height": int,
        "width": int,
        "cfgScale": float,
        "seed": int
    }
}
```
+ **text** (Wajib) — Sebuah prompt teks untuk menghasilkan gambar.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.
+ **ConditionImage** (Optional-V2 only) - Gambar pengkondisian input tunggal yang memandu tata letak dan komposisi gambar yang dihasilkan. Gambar didefinisikan sebagai string gambar yang dikodekan base64. Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar.
+ **ControlMode** (Optional-V2 saja) - Menentukan bahwa jenis mode pengkondisian harus digunakan. Dua jenis mode pengkondisian didukung: CANNY\_EDGE dan SEGMENTATION. Nilai default adalah CANNY\_EDGE.
+ **ControlStrength** (Optional-V2 only) - Menentukan seberapa mirip tata letak dan komposisi gambar yang dihasilkan harus dengan ConditioningImage. Rentang dalam 0 hingga 1.0 dengan nilai yang lebih rendah digunakan untuk memperkenalkan lebih banyak keacakan. Nilai default adalah 0,7.

**catatan**  
Jika ControlMode atau ControlStrength disediakan, maka ConditionImage juga harus disediakan.

------
#### [ Color Guided Content (Request) V2 only ]

Berikan daftar kode warna hex bersama dengan prompt teks untuk menghasilkan gambar yang mengikuti palet warna. Sebuah prompt teks diperlukan untuk menghasilkan gambar harus <= 512 karakter. Resolusi maksimum adalah 1.408 di sisi yang lebih panjang. Daftar kode warna 1 hingga 10 hex diperlukan untuk menentukan warna dalam gambar yang dihasilkan, NegativeText Opsional Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar <= 512 karakter ReferenceImage opsional gambar referensi tambahan untuk memandu palet warna dalam menghasilkan gambar. Batas ukuran untuk gambar referensi RGB yang diunggah pengguna adalah <= 1.408 di sisi yang lebih panjang. 

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

Bidang colorGuidedGeneration Params dijelaskan di bawah ini. Perhatikan bahwa parameter ini hanya untuk V2.
+ **text** (Wajib) — Sebuah prompt teks untuk menghasilkan gambar.
+ **warna** (Wajib) - Daftar hingga 10 kode warna hex untuk menentukan warna dalam gambar yang dihasilkan.
+ **NegativeText** (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.
**catatan**  
Jangan gunakan kata-kata negatif dalam `negativeText` prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan **mirrors** `negativeText` prompt. Jangan masuk**no mirrors**.
+ **ReferenceImage** (Opsional) - Gambar referensi input tunggal yang memandu palet warna gambar yang dihasilkan. Gambar didefinisikan sebagai string gambar yang dikodekan base64.

------
#### [ Background Removal (Request) ]

Jenis tugas penghapusan latar belakang secara otomatis mengidentifikasi beberapa objek dalam gambar input dan menghapus latar belakang. Gambar output memiliki latar belakang transparan. 

**Format permintaan**

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

**Format respons**

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

 backgroundRemovalParams Bidang dijelaskan di bawah ini.
+ **image** (Required) - Gambar JPEG atau PNG untuk dimodifikasi, diformat sebagai string yang menentukan urutan piksel, masing-masing didefinisikan dalam nilai RGB dan dikodekan dalam base64.

------
#### [ Response body ]

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

Badan respons adalah objek streaming yang berisi salah satu bidang berikut.
+ `images`— Jika permintaan berhasil, ia mengembalikan bidang ini, daftar string yang dikodekan base64, masing-masing mendefinisikan gambar yang dihasilkan. Setiap gambar diformat sebagai string yang menentukan urutan piksel, masing-masing didefinisikan dalam nilai RGB dan dikodekan dalam base64. [Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.](#model-parameters-titan-image-code-examples)
+ `error`Jika permintaan melanggar kebijakan moderasi konten dalam salah satu situasi berikut, pesan akan dikembalikan di bidang ini.
  + Jika teks input, gambar, atau gambar topeng ditandai oleh kebijakan moderasi konten.
  + Jika setidaknya satu gambar keluaran ditandai oleh kebijakan moderasi konten

------

Yang dibagikan dan opsional `imageGenerationConfig` berisi bidang berikut. Jika Anda tidak menyertakan objek ini, konfigurasi default akan digunakan.
+ **kualitas** — Kualitas gambar. Nilai default-nya adalah `standard`. Untuk detail harga, lihat [Harga Amazon Bedrock](https://aws.amazon.com/bedrock/pricing/).
+ **numberOfImages**(Opsional) — Jumlah gambar yang akan dihasilkan.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/id_id/bedrock/latest/userguide/model-parameters-titan-image.html)
+ **CFGScale** (Opsional) - Menentukan seberapa kuat gambar yang dihasilkan harus mematuhi prompt. Gunakan nilai yang lebih rendah untuk memperkenalkan lebih banyak keacakan dalam generasi.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/id_id/bedrock/latest/userguide/model-parameters-titan-image.html)
+ Parameter berikut menentukan ukuran gambar keluaran yang Anda inginkan. Untuk detail selengkapnya tentang harga berdasarkan ukuran gambar, lihat [harga Amazon Bedrock](https://aws.amazon.com/bedrock/pricing/).
  + **tinggi** (Opsional) - Tinggi gambar dalam piksel. Nilai defaultnya adalah 1408.
  + **lebar** (Opsional) - Lebar gambar dalam piksel. Nilai defaultnya adalah 1408.

  Ukuran berikut diperbolehkan.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/id_id/bedrock/latest/userguide/model-parameters-titan-image.html)
+ **seed** (Opsional) — Gunakan untuk mengontrol dan mereproduksi hasil. Menentukan pengaturan kebisingan awal. Gunakan seed yang sama dan pengaturan yang sama seperti proses sebelumnya untuk memungkinkan inferensi membuat gambar serupa.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/id_id/bedrock/latest/userguide/model-parameters-titan-image.html)

## Contoh
<a name="model-parameters-titan-image-code-examples"></a>

Contoh berikut menunjukkan cara menjalankan model Amazon Titan Image Generator dengan throughput sesuai permintaan di Python SDK. Pilih tab untuk melihat contoh untuk setiap kasus penggunaan. Setiap contoh menampilkan gambar di akhir.

------
#### [ 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()
```

------