Skip to content

Titan Images

Unstable API

0.7.0

@project-lakechain/bedrock-image-generators

TypeScript Icon

The Bedrock image generators package enables developers to use image generation models hosted on Amazon Bedrock, and create images at scale within their pipelines. The Amazon Titan image generator is part of this package and makes it possible to generate images from text, realize image inpainting and outpainting, as well as image-to-image variation.


πŸ–ΌοΈ Text-to-Image

To generate images as part of your pipelines from a text prompt, you can use the TitanImageGenerator construct. This construct either takes the content of input text document as a prompt for generating an image, allowing you to nicely chain documents together, or can also take a user-provided prompt.

ℹ️ The below example demonstrates how to use the Titan image generator to create images from an arbitrary prompt every 5 minutes, using the Scheduler Trigger.

import * as scheduler from '@aws-cdk/aws-scheduler-alpha';
import { SchedulerEventTrigger } from '@project-lakechain/scheduler-event-trigger';
import { SdxlImageGenerator } from '@project-lakechain/bedrock-image-generators';
class Stack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string) {
const trigger = new SchedulerEventTrigger.Builder()
.withScope(this)
.withIdentifier('Trigger')
.withScheduleExpression(
scheduler.ScheduleExpression.rate(cdk.Duration.minutes(5))
)
.build();
// Every 5 minutes, generate a new image.
trigger.pipe(new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('TitanImageGenerator')
.withCacheStorage(cache)
.withSource(trigger)
.withTask(new TextToImageTask.Builder()
.withPrompt('A beautiful sunset over the ocean.')
.withNegativePrompt('low resolution, low quality')
.build()
)
.build());
}
}



πŸ§‘β€πŸŽ¨ Image Inpainting

The Titan model supports image inpainting by modifying a specific area of an image delimited by a mask, with an AI generated image. To implement inpainting within a pipeline, you can use the TitanImageGenerator by specifying a source image and a mask prompt. Mask prompts identify the area to be painted in natural language, such as β€œhouse” or β€œwindow”.

πŸ’ In the below example we create a pipeline that triggers when an image is uploaded to a source S3 bucket, and replace any β€œhouse” detected in the image by a β€œModern house”.


Original ImageInpainted Image
Original ImageInpainted Image


Example

import { TitanImageGenerator, ImageInpaintingTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('ImageGenerator')
.withCacheStorage(cache)
.withSource(source) // πŸ‘ˆ Specify a data source.
.withTask(new ImageInpaintingTask.Builder()
.withTextPrompt('Modern house')
.withMaskPrompt('house')
.build()
)
.build()



🎨 Image Outpainting

The Amazon Titan image model also supports outpainting, relative to a mask prompt. Image outpainting refers to painting an existing image outside of the area delimited by the mask image.

πŸ’ In the below example we create a pipeline that triggers when an image is uploaded to a source S3 bucket, and replaces the environment outside of a β€œhouse” with a β€œbeautiful garden and swimming pool”.


Original ImageOutpainted Image
Original ImageOutpainted Image


Example

import { TitanImageGenerator, ImageOutpaintingTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('ImageGenerator')
.withCacheStorage(cache)
.withSource(source) // πŸ‘ˆ Specify a data source.
.withTask(new ImageOutpaintingTask.Builder()
.withTextPrompt('Beautiful garden and swimming pool')
.withMaskPrompt('house')
.build()
)
.build();



✨ Image Variation

The Titan model also supports taking an image as an input, and transforming that image using a textual prompt into a new image. To perform image variation, you use the TitanImageGenerator and provide it with a ImageVariationTask.

Variation Process

Example

import { TitanImageGenerator, ImageVariationTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('ImageGenerator')
.withCacheStorage(cache)
.withSource(trigger)
.withSource(source) // πŸ‘ˆ Specify a data source.
.withTask(new ImageVariationTask.Builder()
.withTextPrompt('A cat smiling')
// Reference the current document as the image to transform.
.withImage(r.reference(r.document()))
.build()
)
.build();



🌐 Region Selection

You can specify the AWS region in which you want to invoke Amazon Bedrock using the .withRegion API.

πŸ’ By default, the middleware will use the current region in which it is deployed.

import { TitanImageGenerator } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('ImageGenerator')
.withCacheStorage(cache)
.withSource(trigger)
.withRegion('us-east-1') // πŸ‘ˆ Alternate region
.withTask(task)
.build();



βš™οΈ Model Parameters

For the different tasks supported by the TitanImageGenerator, you can specify additional parameters to customize the generation of the images. You pass additional parameters using the .withImageGenerationParameters API on the task to execute.

import {
TitanImageGenerator,
ImageOutpaintingTask,
ImageGenerationParameters
} from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder()
.withScope(this)
.withIdentifier('ImageGenerator')
.withCacheStorage(cache)
.withSource(source)
.withTask(new ImageOutpaintingTask.Builder()
.withTextPrompt('Beautiful garden and swimming pool')
.withMaskPrompt('house')
.withImageGenerationParameters(new ImageGenerationParameters.Builder()
.withQuality('premium')
.withNumberOfImages(10)
.build()
)
.build()
)
.build();

Parameters

Below are the parameters you can pass to the image generation tasks. See the official Titan documentation to understand the effect of available parameters.

ParameterDescriptionDefault
numberOfImagesThe number of images to generate.1
qualityThe quality of the generated images.standard
cfgScaleSpecifies how strongly the generated image should adhere to the prompt.8.0
heightThe height of the generated image.1024
widthThe width of the generated image.1024
seedDetermines the initial noise setting.0



πŸ—οΈ Architecture

This middleware is based on a Lambda compute and interacts with the Amazon Bedrock service in the specified region to generate images.

Architecture



🏷️ Properties


Supported Inputs
Mime TypeDescription
text/plainText document
image/pngImage document
image/jpegImage document
application/json+schedulerScheduler event
Supported Outputs
Mime TypeDescription
image/pngImage document
Supported Compute Types
TypeDescription
CPUThis middleware only supports CPU compute.


πŸ“– Examples