How to download the segmentation mask

By default, the Remove Background API will return the subject that's been cutout from the input image:

Input Image

Result of the API call

However, it's also possible for the Remove Background API to return the segmentation mask instead.

For this, you'll need to set the argument channels with the value alpha.

Here's the result it will produce:

Input Image

Result of the API call

Because the mask is only in grayscale, in most cases it will be faster to download than the cutout subject.

However, there's one important caveat to be aware of:

  • When returning the cutout subject, the Remove Background API will apply some matting to improve the edges of the subject.

  • When returning the segmentation mask, this matting process will not be applied. So when the mask is applied manually, the edges of the cutout subject will be less precise.

Here's an example of what it looks like:

With matting applied by the API

Without matting applied by the API

If you want to give it a try, here's a code example that's configured to download the mask:

curl --request POST \
  --url https://sdk.photoroom.com/v1/segment \
  --header 'x-api-key: YOUR_API_KEY' \
  --form image_file=@/absolute/path/to/image.jpg \
  --form channels=alpha \
  --output segmentation-mask.png

Last updated