Skip to main content
Layer decomposition automatically splits the subjects, background, text and other content of one image into one base image and several independently editable layers. Each layer is a PNG with an alpha channel and comes with its position on the base image, ready for further editing and compositing in a design tool or your own application. Set layer_decomposition to true to enable it.
When the prompt does not say how to decompose the image, or is unclear, the model plans the decomposition from max_images and the complexity of the input image. A larger max_images generally gives a finer decomposition.

Example

The example separates every animal of an illustration into its own layer and leaves everything else in the base image. With max_images: 40, the response returns 33 images: one base image and 32 animal layers.
For display, the layer overview combines the 32 output layers into one image. It is not an image generated by the model.
Some of the original transparent PNG layers (the numbers are the z_index values of the response):
Download the official full example package (ZIP, about 67 MB) with the base image, the 32 transparent animal layers and an interactive preview page: extract it, open preview.html in a browser and select the layer cards to show or hide layers and inspect the reconstructed image.

Request

$DREAMTO_API_KEY is your API Key.

Rules

  • One image to decompose only: image is a string or an array with a single element (png / jpeg, 262,144–36,000,000 total pixels, aspect ratio 1/16–16, up to 30 MB). Several images, or none, return 400.
  • prompt is optional: without it the model identifies the main elements of the image and separates them into layers; with it the model follows the intent of the prompt. To mark exact regions, give the coordinates of the elements to separate with <bbox>left top right bottom</bbox> in the prompt.
  • size takes a resolution tier only: 1K / 1.5K / 2K or auto (default), not WIDTHxHEIGHT. The base image is output at the resolution given by size with the aspect ratio of the source image; each layer at a resolution close to size with its own aspect ratio in the source image. auto follows the source size: sizes between 1280x720 and 2048x2048 × 1.1025 are kept, smaller ones are output at 1K and larger ones at 2K.
  • output_format only sets the base image: layers are always returned as png.
  • background is not accepted: layer decomposition always returns transparent PNG layers and passing it returns 400.
  • No partial success: if any layer fails, the whole request fails and is not charged.
  • If the prompt asks for more layers than the limit, some layer information may be lost.

Response

The first item of data[] is the base image (z_index 0; it covers the whole canvas and has no bounding_box); each following item is a layer:

Restore the layers

On the output base image, use bounding_box.absolute. For example [225, 442, 796, 1414] means the top-left corner of the layer is (225, 442) and the bottom-right corner is (796, 1414):
  • Top-left position of the layer: x = left, y = top;
  • Width occupied by the layer: w = right - left; height: h = bottom - top;
  • Scale the output layer to w × h and place it at (x, y) on the base image.
On any canvas (W × H), use bounding_box.normalized. For example [220, 432, 777, 1000] means the left edge of the layer is 22.0% of the width from the left, the top edge 43.2% of the height from the top, the right edge at 77.7% and the bottom edge on the bottom of the canvas:
  • x = left / 1000 × W, y = top / 1000 × H;
  • w = (right - left) / 1000 × W, h = (bottom - top) / 1000 × H;
  • Normalized coordinates are integers, so rounding errors may occur.
Stack several layers in ascending z_index.

Billing and balance check

The base image and every layer are billed by their own actual size (data[].size), plus one input image for the image being decomposed; one request can contain both price tiers. Before the call the balance is checked for max_images (default 30) images; with an insufficient balance the request returns 402 and the model is not called. Only the images actually returned are charged, so a fitting max_images lowers the balance needed for the check. A request with many layers can take several minutes; set the client read timeout to at least 300 seconds.