apyhub
Back
▣ ARTIFICIAL INTELLIGENCE · IMAGE PROCESSING

Generate Image Foreground Mask API

What it does

Generate Image Foreground Mask API lets you isolate a subject from an image and return either a mask or a background-removed image. Send a public image URL or base64-encoded image data, and choose whether you want JSON metadata or image bytes back.

Use the subject mask endpoint when you need a grayscale cutout for compositing, edge cleanup, or downstream image analysis. The response includes the output format, media type, width, height, byte size, data URI, subject bounding box, foreground ratio, and the quality setting used. If nothing is found, bbox is null.

Use the background-removal endpoint when you need a ready-to-use subject image. You can crop to the subject's bounding box, add padding, limit the output size, and pick best or fast quality. Output can be json, png, webp, or jpeg; JPEG requires an opaque background color in #RRGGBB form. This is a good fit for product photos, profile pictures, asset pipelines, and any workflow that needs transparent backgrounds or a consistent subject crop.

Both endpoints accept images up to 20 MB and return a data URI alongside the image metadata, so you can pass results straight into your storage, preview, or rendering pipeline.

POST
Get the subject mask
https://api.eu.apyhub.com/callable-labs/background-removal/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/background-removal/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.