Background removal

The Background removal operation allows leaving only the object of interest on an image. The removed background turns into a transparent area. It reduces the time for editing images since you no longer need to remove the background manually.

The feature is currently available on the REST API v0.7 or higher via add-ons API.

How it works

Background removal works asynchronously through the REST API, unlike the image processing operations performed on the fly.

It generates encoded output as a separate file while the original file remains intact.

  1. Start a processing job via REST API. Send an input file UUID with necessary processing operations.
  2. Wait until the processing job status becomes done.
  3. The processed file can be addressed via its new UUID, as well as an URL with operations provided in a processing request.

This will create a new file with an image on a transparent background.

Example

To remove the background from an image, first get your API keys.

Execute background removal on a target UUID

$curl -X POST \
> -H 'Content-Type: application/json' \
> -H 'Accept: application/vnd.uploadcare-v0.7+json' \
> -H "Authorization: Uploadcare.Simple $YOUR_PUBLIC_KEY:$YOUR_SECRET_KEY" \
> "https://api.uploadcare.com/addons/remove_bg/execute/" \
> -d "{\"target\": \"$UUID\"}"

Check out REST API remove bg to see how to execute add-on on a target UUID.

Webhook event

To get the job result, you need to enable file.uploaded in the Webhook section of the dashboard.

After completing the processing job, the webhook will be sent to the endpoint you specified in the webhook settings.

File information in response:

1"initiator": {
2 "type": "addon",
3 "detail": {
4 "addon_name": "remove_bg",
5 "request_id": "d8985400-7ca9-4e1b-9751-2e238a104754",
6 "source_file_uuid": ":UUID"
7 }
8 }
9"hook": {
10 ...
11 "event": "file.uploaded",
12 "is_active": true,
13 "version": "0.7",
14 "created_at": "2023-10-26T09:32:40.114516Z",
15 "updated_at": "2023-10-26T09:32:40.114516Z"
16 },
17 "uuid": ":UUID",
18 "is_image": true,
19 "is_ready": true,
20 "metadata": {},
21 "appdata": {
22 "remove_bg": {
23 "data": {
24 "foreground_type": "product"
25 },
26 "version": "1.0",
27 "datetime_created": "2023-11-07T18:00:57.596Z",
28 "datetime_updated": "2023-11-07T18:00:57.596Z"
29 }
30 }

Check the execution status

If your application does not have a backend or uses a mobile version, you can submit the request yourself.

Use request ID returned by the add-on execution request described above.

$curl -X GET \
> -H 'Content-Type: application/json' \
> -H 'Accept: application/vnd.uploadcare-v0.7+json' \
> -H "Authorization: Uploadcare.Simple $YOUR_PUBLIC_KEY:$YOUR_SECRET_KEY" \
> "https://api.uploadcare.com/addons/remove_bg/execute/status/?request_id=$REQUEST_ID"

Check out REST API remove bg status to check the execution status.

As a request result, you will receive an UUID of the file when status is done.

1{
2 "result": {
3 "file_id": "b6d7b8f7-a982-4719-bc0b-3c00f2b56b06"
4 },
5 "status": "done"
6}

Use cases

If you need to set additional options when removing the background, such as a specific background color, the output request should look like this:

$curl -X POST \
> -H 'Content-Type: application/json' \
> -H 'Accept: application/vnd.uploadcare-v0.7+json' \
> -H "Authorization: Uploadcare.Simple $YOUR_PUBLIC_KEY:$YOUR_SECRET_KEY" \
> "https://api.uploadcare.com/addons/remove_bg/execute/" \
> -d "{\"target\": \"$UUID\", \"params\": {\"crop\": true, \"bg_color\": \"81d4fa77\"}}"

List of params

ParameterDescription
cropWhether to crop off all empty regions
crop_marginAdds a margin around the cropped subject e.g 30px or 30%
scaleScales the subject relative to the total image size e.g 80%
add_shadowWhether to add an artificial shadow to the result
type_levelClassification level of the detected foreground type
typeForeground type
semitransparencyWhether to have semi-transparent regions in the result
channelsRequest either the finalized image (‘rgba’ default) or an alpha mask (‘alpha’)
roiOnly the contents of this given rectangular area can be detected as foreground. Anything outside is considered background and will be removed
positionPositions the subject within the image canvas

Check out REST API remove bg to see full list of request body schema parameters.

Limitations

  • This operation is not available on the Free plan.
  • Removes the background from a JPG/PNG image.
  • File size: up to 12 MB.
  • Image сontent: any photo with a foreground (e.g., people, products, animals, cars, etc).
  • Output resolutions available: up to 25 megapixels. If the output image is larger than that, it will be automatically compressed to 25 megapixels.
  • Output file formats: PNG.

API integrations

You don’t have to code most of the low-level API integrations. We have high-level libraries for all popular platforms:

Billing

  • This feature is available on paid plans.
  • This operation uploads a new file.
  • Learn how we charge for this operation.