Background Removal

FREEMIUM
Verified
By API 4 AI | Updated 14 days ago | Visual Recognition
Popularity

9.6 / 10

Latency

9,582ms

Service Level

97%

Health Check

N/A

Followers: 15
Resources:
Product Website
API Creator:
Rapid account: API 4 AI
API 4 AI
api4ai
Log In to Rate API
Rating: 4.5 - Votes: 2

README

⭐️ Image Background Removal

Examples API4AI Telegram

This solution provides image analysis for foreground segmentation and background removal. It calculates a potential foreground object/area, segments it out and removes every other pixel.
You can use it in your image processing apps, quickly make collages with it, apply it to social media apps and more.

This API is created by API4AI. We build our APIs on a completely cloud technology stack which provides full operability, scalability and stable uptime. Our sole goal is to create out-of-the-box self-contained AI solutions that can easily be integrated into any application with just a few simple steps.

Image background removal

Features

👉 Features

  • ✅ Removes background from images.
  • ✅ Returns coordinates of bounding boxes of a foreground object.
  • ✅ Returns an image of a foreground object over a transparent background or black & white mask.
  • ✅ Optionally adds a realistic shadow.
  • ✅ Optionally blends results with a new background image.
  • ✅ Supports two alternative ways to pass images: as a binary file or via url to a public resource.

👉 Demos


👉 Contacts




🛠 Endpoints

METHOD URL DESCRIPTION
GET https://background-removal4.p.rapidapi.com/v1/version Get a service version.
GET https://background-removal4.p.rapidapi.com/v1/modes Get list of available modes.
POST https://background-removal4.p.rapidapi.com/v1/results Perform image processing and get results.

👉 Get version

Returns an actual version of the service in format vX.Y.Z where X is the version of API.

PROPERTY DESCRIPTION
Endpoint https://background-removal4.p.rapidapi.com/v1/version
Method GET
Query parameters
POST parameters

Example

curl -X 'GET' 'https://background-removal4.p.rapidapi.com/v1/version' \
     -H 'X-RapidAPI-Key: ...'
Click here to view the response
v1.3.0



👉 Get list of available modes

The service provides several options for background removal results:

  • fg-image – Image with foreground object.
  • fg-mask – Mask of foreground.
  • fg-image-shadow – Image with foreground object with shadow.
PROPERTY DESCRIPTION
Endpoint https://background-removal4.p.rapidapi.com/v1/modes
Method GET
Query parameters
POST parameters

Example

curl -X 'GET' 'https://background-removal4.p.rapidapi.com/v1/modes' \
     -H 'X-RapidAPI-Key: ...'
Click here to view the response
[
  "fg-image",
  "fg-image-shadow",
  "fg-mask"
]



👉 Analyze image and return results

Performs actual image analysis and responds with results.

PROPERTY DESCRIPTION
Endpoint https://background-removal4.p.rapidapi.com/v1/results
Method POST
Query parameters mode
POST parameters image, url, image-bg, url-bg

Passing image

Image can be passed by posting regular “multipart form data” in two alternative ways:

  • as binary file in image field
  • as URL to some public resource in url field

Image must be a regular JPEG or PNG image (with or without transparency).
Usually such images have extensions: .jpg, .jpeg, .png.
The service checks input file by MIME type and accepts the following types:

  • image/jpeg
  • image/png

The size of the image file must be less than 16Mb.

The maximum allowed resolution is 4096x4096.


Passing optional background image

If the background image is passed then it’s content will be blended below foreground object (aligned by the center). The final output image will have the same size as the main input image (not background image!). Meaningless if the mode is fg-mask.

Background image can be passed by posting regular “multipart form data” in two alternative ways:

  • as binary file in image-bg field
  • as URL to some public resource in url-bg field

Image must be a regular JPEG or PNG image (with or without transparency).
Usually such images have extensions: .jpg, .jpeg, .png.
The service checks input file by MIME type and accepts the following types:

  • image/jpeg
  • image/png

The size of the image file must be less than 16Mb.

The maximum allowed resolution is 4096x4096.


Specifying mode

Query parameter mode is optional and may be used to choose the format of the output image: foreground mask or foreground image. Also the foreground image could be returned with shadow under the object.
By default the service uses fg-image.

Available modes and expected content of the resulting image:

  • fg-image (default) – the image with the foreground object. Here the result is a 4-channel (RGBA) PNG image where background is transparent.
    • Areas recognized as foreground will have the same color values at output image as the original input image.
    • Areas recognized as background will be transparent at output image.
    • Pixels of output image near the foreground borders may be semi transparent.
  • fg-mask – the mask of foreground object. In this case the result is a single-channel grayscale PNG image.
    • Areas recognized as foreground will have white color.
    • Areas recognized as background will be black at output image.
    • Pixels of output image near to foreground borders may have gray values: the closer
      to black, the more likely the pixel is the background.
  • fg-image-shadow – the image with the foreground object and realistic shadow below. Image format is as in case of just the foreground object.

Response schema

For responses with 200 HTTP code the type of response is JSON object with the following schema:

{
  "results": [
    {
      "status": {
        "code": ...,
        "message": ...
      },
      "name": ...,
      "md5": ...,
      "page": ...,
      "width": ...,
      "height": ...,
      "entities": [
        {
          "kind": "image",
          "name": ...,
          "image": ...,
          "format": "PNG",
          "representation": "base64"
        },
        {
          "kind": "objects",
          "name": "objects",
          "objects": [...]
        }
      ]
    }
  ]
}

Primary fields:

Name Type Description
results[].status.code string Status code of image processing: ok or failure.
results[].status.message string Human readable explanation for the status of image processing.
results[].name string Original image name passed in request (e.g. my_image.jpg).
results[].md5 string MD5 sum of original image passed in request.
results[].page int Optional page number (presented for multipage inputs only).
results[].width int Optimal image width (presented for valid inputs only).
results[].height int Optional image height (presented for valid inputs only).
results[].entities[].name string The name of the output entity.
results[].entities[].image string PNG result image encoded as base64.
results[].entities[].format string Image format.
  • Entity name. It depends on mode. Examples:

    • general-fg-image if the output is a foreground object.
    • general-fg-image-shadow if the output is a foreground object with shadow.
    • general-fg-mask if the output is a foreground mask.
  • The most important part of the response is results[].entities[].image field.

    • Image. The resulting image is a PNG image with transparency.
      Pixels of the original image that are recognized as background will be transparent in the result image.
    • Mask. The result image is a single-channel grayscale PNG image.
      Pixels of the original image that are recognized as background will be black in the resulting image and foreground will be white.

The output image in results[].entities[].image field is encoded as base64.

Other fields that are not described above always have the same values.




📌 Examples


👉 The original image



👉 Just remove the background

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg'
Click here to view the response
{
  "results": [
    {
      "status": {
        "code": "ok",
        "message": "Success"
      },
      "name": "img.jpg",
      "md5": "f2d13d0242b98aae82bc7a6dc76e1ea9",
      "width": 1024,
      "height": 768,
      "entities": [
        {
          "kind": "image",
          "name": "general-fg-image",
          "image": "iVBORw0KGgoAAAA...YII=",
          "format": "PNG",
          "representation": "base64"
        },
        {
          "kind": "objects",
          "name": "objects",
          "objects": [
            {
              "box": [
                0.138,
                0.37,
                0.6613333333333333,
                0.456
              ],
              "entities": [
                {
                  "kind": "classes",
                  "name": "classes",
                  "classes": {
                    "opaque-content": 1
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}



👉 Get objects’s mask

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results?mode=fg-mask' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg'
Click here to view the response
{
  "results": [
    {
      "status": {
        "code": "ok",
        "message": "Success"
      },
      "name": "img.jpg",
      "md5": "f2d13d0242b98aae82bc7a6dc76e1ea9",
      "width": 1024,
      "height": 768,
      "entities": [
        {
          "kind": "image",
          "name": "general-fg-mask",
          "image": "iVBORw0KGgoAAAA...YII=",
          "format": "PNG",
          "representation": "base64"
        },
        {
          "kind": "objects",
          "name": "objects",
          "objects": [
            {
              "box": [
                0.138,
                0.37,
                0.6613333333333333,
                0.456
              ],
              "entities": [
                {
                  "kind": "classes",
                  "name": "classes",
                  "classes": {
                    "opaque-content": 1
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}



👉 Drop shadow under the object and use custom background

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results?mode=fg-image-shadow' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg' \
     -F 'image-bg=@bg.png'
Click here to view the response
{
  "results": [
    {
      "status": {
        "code": "ok",
        "message": "Success"
      },
      "name": "img.jpg",
      "md5": "f2d13d0242b98aae82bc7a6dc76e1ea9",
      "width": 1024,
      "height": 768,
      "entities": [
        {
          "kind": "image",
          "name": "general-fg-image-shadow",
          "image": "iVBORw0KGgoAAAA...YII=",
          "format": "PNG",
          "representation": "base64"
        },
        {
          "kind": "objects",
          "name": "objects",
          "objects": [
            {
              "box": [
                0.138,
                0.37,
                0.6613333333333333,
                0.456
              ],
              "entities": [
                {
                  "kind": "classes",
                  "name": "classes",
                  "classes": {
                    "opaque-content": 1
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}




❗️ Possible errors


👉 The service can not process an image

When a client sends an image that can not be processed for some reason(s), the service responds with 200 code and returns a JSON object in the same format as the format for successful analysis. In this case, the results[].status.code will have failure value and results[].status.message will contain relevant explanation.

Example of possible reasons for the issue:

  • Unsupported MIME type
  • Image resolution exceeds limits
  • Corrupted image

Example response for image with unsupported MIME type:

{
  "results": [
    {
      "status": {
        "code": "failure",
        "message": "Can not load image."
      },
      "name": "file.txt",
      "md5": "d41d8cd98f00b204e9800998ecf8427e",
      "entities": []
    }
  ]
}

Example response for image with the resolution too big to process:

{
  "results": [
    {
      "status": {
        "code": "failure",
        "message": "Resolution is too big: 6000x4000. Max allowed resolution: 4096x4096."
      },
      "name": "24mpx.jpg",
      "md5": "a24c7cd77e21b303a1211889cea7527b",
      "entities": []
    }
  ]
}

👉 Request size is too big

Request size is limited by approximately 32Mb.
When a client sends a request that exceeds this limit, the service responds with 413 code.

The typical reason for exceeding this limit is an overly large image.
Taking into account additional HTTP overhead, we strongly recommend not passing image files of size more than 16Mb.

Example response for overly big image:

Error: Request Entity Too Large

Your client issued a request that was too large.

👉 Missing image

When a client sends a request without an image, the service responds with 422 code and returns a JSON object.

Example response for request with missing image:

{"detail":"Missing image or url field."}




❓ F.A.Q.


👉 How to pass an image as binary file?

Post a file content as a “multipart form data” field named image.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg'

👉 How to pass an image as URL?

Post a URL to file as a “multipart form data” field named url.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'url=https://storage.googleapis.com/api4ai-static/rapidapi/background-removal/shoes.jpg'

👉 How to get a objects’s segmentation mask?

Use mode=fg-mask query parameter.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results?mode=fg-mask' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg'

👉 How to drop shadow under the object?

Use mode=fg-image-shadow query parameter.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results?mode=fg-image-shadow' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg'

👉 How to pass a custom background image as binary file?

Post a file content as a “multipart form data” field named image-bg.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg' \
     -F 'image-bg=@bg.jpg'

👉 How to pass a custom background image as URL?

Post a file as a “multipart form data” field named url-bg.

curl -X 'POST' 'https://background-removal4.p.rapidapi.com/v1/results' \
     -H 'X-RapidAPI-Key: ...' \
     -F 'image=@img.jpg' \
     -F 'url-bg=https://storage.googleapis.com/api4ai-static/rapidapi/background-removal/bg.png'

👉 I’ve got JSON response. Where is the resulting image???

The resulting png image is a part of JSON response and encoded as base64 string. Property path: results[].entities[].image

{
  "results": [
    {
      ...
      "entities": [
        {
          "kind": "image",
          "image": RESULTING-PNG-IMAGE-ENCODED-AS-BASE64-STRING,
          ...
        }
        ...
      ]
    }
  ]
}

👉 Where to find code examples?

Code examples in Python, C#, JavaScript, Swift and other popular programming languages: https://gitlab.com/api4ai/examples/img-bg-removal-general


👉 Where to try live demo?


👉 I need support

Feel free to contact API4AI team if you have any questions.