Stability AI Image Services - Amazon Bedrock

Stability AI Image Services

You can use Stability AI Image Services with Amazon Bedrock to access nine specialized image editing tools designed to accelerate professional creative workflows. With Stability AI Image Services you can generate images from a sketch, restructure and restyle an existing image, or remove and replace objects within an image.

This section describes how to make inference calls to Stability AI Image Services using the InvokeModel. This section also provides code examples in Python and examples of images before and after using Stability AI Image Services.

Stability AI Image Services are available in the following categories:

  • Edit ‐ AI-based image editing services, including inpainting with masks (generative fill), or with words. Includes tools for product placement and advertising, as well as basic tools such as background removal.

  • Control ‐ May take prompts, maps and other guides. These services leverage ControlNets and similar technologies built on Stable Diffusion models.

Note

Subscribing to any edit or control Stability AI Image Service automatically enrolls you in all nine available Stability AI Image Services.

Request and response

The request body is passed in the body field of a request to InvokeModel.

Model invocation request body field

When you make an InvokeModel call using Stability AI Image Services, fill the body field with a JSON object that looks like the below.

{ 'prompt': 'Create an image of a panda' }

Model invocation responses body field

When you make an InvokeModel call using Stability AI Image Services, the response looks like the below

{ 'seeds': [2130420379], 'finish_reasons': [null], 'images': ['...'] }
  • seeds – (string) List of seeds used to generate images for the model.

  • finish_reasons – Enum indicating whether the request was filtered or not. null will indicate that the request was successful. Current possible values: "Filter reason: prompt", "Filter reason: output image", "Filter reason: input image", "Inference error", null.

  • images – A list of generated images in base64 string format.

For more information, see https://platform.us.stability.ai/docs/api-reference#tag/v1generation.

Edit

The following section describes the edit Stability AI Image Services.

Inpaint intelligently modifies images by filling in or replacing specified areas with new content based on the content of a mask image.

Inpaint has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image to inpaint. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • style_preset ‐ (string) Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • mask ‐ (string) Controls the strength of the inpainting process on a per-pixel basis, either via a second image (passed into this parameter) or via the alpha channel of the image parameter.

    • Passing in a Mask ‐ The image passed to this parameter should be a black and white image that represents, at any pixel, the strength of inpainting based on how dark or light the given pixel is. Completely black pixels represent no inpainting strength while completely white pixels represent maximum strength. In the event the mask is a different size than the image parameter, it will be automatically resized.

    • Alpha Channel Support ‐ If you don't provide an explicit mask, one will be derived from the alpha channel of the image parameter. Transparent pixels will be inpainted while opaque pixels will be preserved. In the event an image with an alpha channel is provided along with a mask, the mask will take precedence.

  • grow_mask ‐ Grows the edges of the mask outward in all directions by the specified number of pixels. The expanded area around the mask will be blurred, which can help smooth the transition between inpainted content and the original image. Range between 0 and 20. Default 5. Try this parameter if you notice seams or rough edges around the inpainted content. Note that excessive growth may obscure fine details in the mask and/or merge nearby masked regions.

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)

The following table shows the input and output images of an Inpaint operation.

Input

Mask

Output

“Man in metropolis” generated by Stable Image Ultra, Prompts and edits by Sanwal Yousaf. Licensed under CC BY 4.0

Search and Recolor allows you to change the color of a specific object in an image using a prompt. This service is a specific version of inpainting that does not require a mask. It will automatically segment the object and recolor it using the colors requested in the prompt.

Search and Recolor has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image to recolor. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

  • select_prompt ‐ (string) Short description of what to search for in the image. Maximum 10000 characters.

The following parameters are optional:

  • style_preset ‐ (string) Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • grow_mask ‐ Grows the edges of the mask outward in all directions by the specified number of pixels. The expanded area around the mask will be blurred, which can help smooth the transition between inpainted content and the original image. Range between 0 and 20. Default 5. Try this parameter if you notice seams or rough edges around the inpainted content. Note that excessive growth may obscure fine details in the mask and/or merge nearby masked regions.

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)

The following table shows the input and output images of a Search and Recolor operation using the following prompt: pink jacket.

Input

Output

“Man wearing puffer jacket” generated by Stable Image Ultra, Prompts and edits by Sanwal Yousaf. Licensed under CC BY 4.0

Search and Replace allows you to use a search prompt to identify an object in simple language to be replaced. The service will automatically segment the object and replace it with the object requested in the prompt without requiring a mask.

Search and Replace has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image to recolor. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

  • search_prompt ‐ (string) Short description of what to inpaint in the image. Maximum 10000 characters.

The following parameters are optional:

  • style_preset ‐ (string) Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • grow_mask ‐ Grows the edges of the mask outward in all directions by the specified number of pixels. The expanded area around the mask will be blurred, which can help smooth the transition between inpainted content and the original image. Range between 0 and 20. Default 5. Try this parameter if you notice seams or rough edges around the inpainted content. Note that excessive growth may obscure fine details in the mask and/or merge nearby masked regions.

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)

The following table shows the input and output images of a Search and Replace operation using the following prompt: jacket.

Input

Output

“Female model wearing fall sweater" generated by Stable Image Ultra. Prompts and edits by Sanwal Yousaf. Licensed under CC BY 4.0

Erase allows you to remove unwanted elements using image masks, while intelligently maintaining background consistency.

Erase has the following required parameters:

  • image ‐ (string) The Base64 image to erase from. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • mask ‐ (string) Controls the strength of the inpainting process on a per-pixel basis, either via a second image (passed into this parameter) or via the alpha channel of the image parameter.

    • Passing in a Mask ‐ The image passed to this parameter should be a black and white image that represents, at any pixel, the strength of inpainting based on how dark or light the given pixel is. Completely black pixels represent no inpainting strength while completely white pixels represent maximum strength. In the event the mask is a different size than the image parameter, it will be automatically resized.

    • Alpha Channel Support ‐ If you don't provide an explicit mask, one will be derived from the alpha channel of the image parameter. Transparent pixels will be inpainted while opaque pixels will be preserved. In the event an image with an alpha channel is provided along with a mask, the mask will take precedence.

  • grow_mask ‐ Grows the edges of the mask outward in all directions by the specified number of pixels. The expanded area around the mask will be blurred, which can help smooth the transition between inpainted content and the original image. Range between 0 and 20. Default 5. Try this parameter if you notice seams or rough edges around the inpainted content. Note that excessive growth may obscure fine details in the mask and/or merge nearby masked regions.

Note

For optimal erase results, ensure your mask accurately defines the areas to be removed. If no explicit mask is provided, the service will use the alpha channel of the input image. The mask will take precedence if both are provided.

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)

The following table shows the input and output images of an Erase operation.

Input

Mask

Output

“Students Desk" generated by Stable Image Ultra. Prompts and edits by Sanwal Yousaf. Licensed under CC BY 4.0

Remove Background allows you to isolate subjects from the background with precision.

Remove Background has the following required parameters:

  • image ‐ (string) The Base64 image to remove the background from. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

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)

The following table shows the input and output images of a Remove Background operation.

Input

Output

“Female model wearing fall sweater" generated by Stable Image Ultra. Prompts and edits by Sanwal Yousaf. Licensed under CC BY 4.0

Control

The following section describes the control Stability AI Image Services.

Upgrade rough hand-drawn sketches to refined outputs with precise control. For non-sketch images, Control Sketch allows detailed manipulation of the final appearance by leveraging the contour lines and edges within the image.

Control Sketch has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image of the sketch. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • control_strength ‐ (number) How much influence, or control, the image has on the generation. Represented as a float between 0 and 1, where 0 is the least influence and 1 is the maximum. Default 0.7.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • style_preset ‐ Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

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)

The following table shows the input and output images of a Control Sketch call using the following prompt: a house with background of mountains and river flowing nearby.

Input

Output

"House, mountains, and river sketch" by Sanwal Yousaf. Licensed under CC BY 4.0

Control Structure allows you to generate images while maintaining the structure of an input image. This is especially valuable for advanced content creation scenarios such as recreating scenes or rendering characters from models.

Control Structure has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image of the sketch. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • control_strength ‐ (number) How much influence, or control, the image has on the generation. Represented as a float between 0 and 1, where 0 is the least influence and 1 is the maximum. Default 0.7.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • style_preset ‐ Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

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)

The following table shows the input and output images of a Control Structure operation using the following prompt: surreal structure with motion generated sparks lighting the scene.

Input

Output

“Person sitting on brown box” by Pawel L. Licensed under CC

Style Guide allows you to extract stylistic elements from an input image and use it to guide the creation of an output image based on the prompt. The result is a new image in the same style as the input image.

Style Guide has the following required parameters:

  • prompt ‐ What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue. Minimum 0 and Maximum 10000 characters.

  • image ‐ (string) The Base64 image of the sketch. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • aspect_ratio ‐ (string) Controls the aspect ratio of the generated image. This parameter is only valid for text-to-image requests. Default 1:1. Enum: 16:9, 1:1, 21:9, 2:3, 3:2, 4:5, 5:4, 9:16, 9:21. Default 1:1.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • fidelity ‐ (number) How closely the output image's style resembles the input image's style. Range 0 to 1. Default 0.5.

  • style_preset ‐ Guides the image model towards a particular style. Enum: 3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture.

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)

The following table shows the input and output images of a Style Guide call using the following prompt: wide shot of modern metropolis.

Input

Output

“Abstract Painting” by Steven Johnson. Licensed under CC

Style Transfer allows you to apply visual characteristics from reference style images to target images. While the Style Guide service extracts stylistic elements from an input image and uses them to guide the creation of an output image based on the prompt, Style Transfer specifically transforms existing content while preserving the original composition. This tool helps create consistent content across multiple assets.

Style Transfer has the following required parameters:

  • init_image ‐ (string) A Base64 image containing the subject you wish to restyle. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

  • style_image ‐ (string) A Base64 image containing the subject you wish to restyle. Every side of the image must be at least 64 pixels. The total pixel count cannot exceed 9,437,184 pixels. Image aspect ratio must be between 1:2.5 and 2.5:1. Supported formats: jpeg, png, webp.

The following parameters are optional:

  • prompt ‐ (string) What you wish to see in the output image. A strong, descriptive prompt that clearly defines elements, colors, and subjects will lead to better results. To control the weight of a given word use the format (word:weight), where word is the word you'd like to control the weight of and weight is a value. A value 0 and 1.0 de-emphasized the word and a value between 1.1 and 2 emphasized the word . For example: The sky was a crisp (blue:0.3) and (green:1.8) would convey a sky that was blue and green, but more green than blue.

  • negative_prompt ‐ (string) A blurb of text describing what you do not wish to see in the output image. This is an advanced feature. Maximum 10000 characters.

  • seed ‐ (number) A specific value that is used to guide the 'randomness' of the generation. (Omit this parameter or pass 0 to use a random seed.) Range 0 to 4294967294. Default 0.

  • output_format ‐ (string) Dictates the content-type of the generated image. Enum: jpeg, png, webp. Default png.

  • composition_fidelity ‐ (number) How closely the output image's style resembles the input image's style. Range between 0 and 1. Default 0.9.

  • style_strength ‐ (number) Sometimes referred to as denoising, this parameter controls how much influence the style_image parameter has on the generated image. A value of 0 would yield an image that is identical to the input. A value of 1 would be as if you passed in no image at all. Range between 0 and 1. Default 1.

  • change_strength ‐ (number) How much the original image should change. Range between 0.1 and 1. Default 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)

The following table shows the input and output images of a Style Transfer call.

Input

Style

Output

"Standing Woman Statue" by Simon Berger. Licensed under CC

"Blue Bright Lights" by Pixabay. Licensed under CC0