apyhub
Back
▣ MODEL KIT · MEDIA KIT

Generate Image Foreground Mask API

What it does

The Image Mask API finds the main subject in an image and returns a grayscale mask of it. Use the mask to composite, clean up edges or analyse the subject in your own pipeline.

Send the image in image, as a public URL or base64 data, up to 20 MB. Set format to json (default) or png. JSON returns the mask as a data URI with its metadata. PNG returns the mask file. Set quality to best (default) or fast. Set crop to true to trim the mask to the subject, with padding (0 to 1000 pixels) of space around it. Use max_size to limit the output size.

The JSON response includes the width, height, bytes, the subject bbox in the original image and the foreground_ratio, the share of the image covered by the subject. If no subject is found, bbox is null.

Teams use the Image Mask API to build cutouts for product photos and to place subjects on new backgrounds in their own editor. Others check that an upload has a clear subject, or crop images to it.

To get the cut-out subject instead of the mask, use the Remove Image Background with Advanced Controls API. To read text from images, use the Image & PDF OCR to Markdown API.

POST
Get the subject mask
https://api.eu.apyhub.com/callable-labs/foreground-mask/v1/background/mask

QUICKSTART

GUIDE

Quickstart

Send a public image URL to remove its background and return mask metadata in JSON.

curl -X POST "https://api.eu.apyhub.com/callable-labs/foreground-mask/v1/background/mask" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "image": {
      "url": "https://assets.apyhub.com/samples/cat.jpg"
    }
  }'

What you'll get back

Returns a JSON object with these top-level fields: format and media_type are strings, width and height are output dimensions in pixels, bytes is the payload size, data_uri is a data:<media_type>;base64,... string, bbox is either an object with x, y, width, and height or null, foreground_ratio is a number between 0 and 1, and quality is "best" or "fast".

{
  "format": "json",
  "media_type": "image/png",
  "width": 800,
  "height": 600,
  "bytes": 124532,
  "data_uri": "data:image/png;base64,iVBORw0KGgoAAA...",
  "bbox": { "x": 120, "y": 45, "width": 540, "height": 510 },
  "foreground_ratio": 0.42,
  "quality": "best"
}
TRY ITLIVE · 600 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
MaskRequest*
Image source object. Provide exactly one of `url` or `base64`; see nested fields.
Image URL*
Public http(s) URL of the image. Must be ≤ 20 MB; private and internal addresses are refused.
Crop the result to the subject's bounding box. Default: `false`.
Output format. Allowed values: `json`, `png`. Default: `json`. `json` returns metadata plus a PNG data URI; `png` returns the 8-bit grayscale mask.
Pixels kept around the subject when `crop` is true. Default: `0`; minimum: `0`, maximum: `1000`.

About this endpoint

What it does

Generates a subject mask for an input image. The request accepts an image source and optional output controls, and the response returns mask metadata plus either a data URI or PNG-oriented output depending on the requested format.

Request Body

ParameterTypeDescription
cropBooleanCrop the result to the subject's bounding box. Default: false.
imageObjectImage source object. Contains url and/or base64; see nested fields.
image.urlStringPublic http(s) URL of the image. Must be ≤ 20 MB; private and internal addresses are refused.
image.base64StringThe image as base64, including a data: URI if desired. Must be ≤ 20 MB decoded.
formatENUMOutput format. Allowed values: json, png. Default: json. json returns metadata plus a PNG data URI; png returns the 8-bit grayscale mask.
paddingIntegerPixels kept around the subject when crop is true. Default: 0; minimum: 0, maximum: 1000.
qualityENUMOutput quality. Allowed values: best, fast. Default: best. best prioritizes detail; fast is suitable for previews.
max_sizeIntegerLongest side of the output in pixels. Default: original size; minimum: 64, maximum: 4096.

Response

Returns a JSON object with format, media_type, width, height, bytes, data_uri, bbox, foreground_ratio, and quality fields. bbox is either an object with x, y, width, and height, or null when nothing was found.

ParameterTypeDescription
bboxObjectSubject bounding box in the original image, or null when nothing was found.
bbox.xIntegerX coordinate of the bounding box.
bbox.yIntegerY coordinate of the bounding box.
bbox.widthIntegerWidth of the bounding box.
bbox.heightIntegerHeight of the bounding box.
bytesIntegerSize of the output in bytes.
widthIntegerOutput width in pixels.
formatStringEncoding of data_uri.
heightIntegerOutput height in pixels.
qualityENUMQuality used for the result. Allowed values: best, fast.
data_uriStringdata:<media_type>;base64,…
media_typeStringMedia type of the returned data URI.
foreground_ratioNumberShare of the original image covered by the subject, from 0 to 1.
▣ COMMON ERRORS

Errors any endpoint can return

400bad_request

Required parameter missing or malformed body.

401unauthorized

API key missing, revoked, or not authorized for this service.

429rate_limited

Your plan's per-second rate exceeded. Retry with exponential backoff.

503upstream_busy

Backend temporarily unavailable. Try again in a few seconds.