Image Background Removal

Remove backgrounds from images effortlessly using Runware's low-cost image editing API.

Introduction

Background removal is a powerful image processing technique used to isolate subjects from their backgrounds, resulting in images with transparent backgrounds. This capability is essential for enhancing product images, creating professional portraits, or integrating subjects into various design compositions seamlessly.

Request

Our API always accepts an array of objects as input, where each object represents a specific task to be performed. The structure of the object varies depending on the type of the task. For this section, we will focus on the parameters related to the background removal task.

The following JSON snippets shows the basic structure of a request object. All properties are explained in detail in the next section.

[
  {
    "taskType": "imageBackgroundRemoval",
    "taskUUID": "19abad0d-6ec5-40a6-b7af-203775fa5b7f",
    "inputImage": "fd613011-3872-4f37-b4aa-0d343c051a27",
    "outputType": "URL",
    "outputFormat": "JPG",
    "rgba": [255, 255, 255, 0],
    "postProcessMask": true,
    "returnOnlyMask": false,
    "alphaMatting": true,
    "alphaMattingForegroundThreshold": 240,
    "alphaMattingBackgroundThreshold": 10,
    "alphaMattingErodeSize": 10
  }
]

taskType

stringrequired

The type of task to be performed. For this task, the value should be imageBackgroundRemoval.

taskUUID

stringrequiredUUID v4

When a task is sent to the API you must include a random UUID v4 string using the taskUUID parameter. This string is used to match the async responses to their corresponding tasks.

If you send multiple tasks at the same time, the taskUUID will help you match the responses to the correct tasks.

The taskUUID must be unique for each task you send to the API.

inputImage

stringrequired

Specifies the input image to be processed. The image can be specified in one of the following formats:

  • An UUID v4 string of a previously uploaded image or a generated image.
  • A data URI string representing the image. The data URI must be in the format data:<mediaType>;base64, followed by the base64-encoded image. For example: data:image/png;base64,iVBORw0KGgo....
  • A base64 encoded image without the data URI prefix. For example: iVBORw0KGgo....
  • A URL pointing to the image. The image must be accessible publicly.

Supported formats are: PNG, JPG and WEBP.

outputType

"base64Data" | "dataURI" | "URL"Default: URL

Specifies the output type in which the image is returned. Supported values are: dataURI, URL, and base64Data.

  • base64Data: The image is returned as a base64-encoded string using the imageBase64Data parameter in the response object.
  • dataURI: The image is returned as a data URI string using the imageDataURI parameter in the response object.
  • URL: The image is returned as a URL string using the imageURL parameter in the response object.

outputFormat

"JPG" | "PNG" | "WEBP"Default: PNG

Specifies the format of the output image. Supported formats are: PNG, JPG and WEBP.

includeCost

booleanDefault: false

If set to true, the cost to perform the task will be included in the response object.

rgba

[integer, integer, integer, float]

An array representing the [red, green, blue, alpha] values that define the color of the removed background. The alpha channel controls transparency.

postProcessMask

booleanDefault: false

Flag indicating whether to post-process the mask. Controls whether the mask should undergo additional post-processing. This step can improve the accuracy and quality of the background removal mask.

returnOnlyMask

booleanDefault: false

Flag indicating whether to return only the mask. The mask is the opposite of the image background removal.

alphaMatting

booleanDefault: false

Flag indicating whether to use alpha matting. Alpha matting is a post-processing technique that enhances the quality of the output by refining the edges of the foreground object.

alphaMattingForegroundThreshold

integerMin: 1Max: 255Default: 240

Threshold value used in alpha matting to distinguish the foreground from the background. Adjusting this parameter affects the sharpness and accuracy of the foreground object edges.

alphaMattingBackgroundThreshold

integerMin: 1Max: 255Default: 10

Threshold value used in alpha matting to refine the background areas. It influences how aggressively the algorithm removes the background while preserving image details. The higher the value, the more computation is needed and therefore the more expensive the operation is.

alphaMattingErodeSize

integerMin: 1Max: 255Default: 10

Specifies the size of the erosion operation used in alpha matting. Erosion helps in smoothing the edges of the foreground object for a cleaner removal of the background.

Response

Results will be delivered in the format below.

{
  "data": [
    {
      "taskType": "imageBackgroundRemoval",
      "taskUUID": "19abad0d-6ec5-40a6-b7af-203775fa5b7f",
      "imageUUID": "aa418b0f-4b83-4c3d-96c5-30abf4699a4d",
      "inputImageUUID": "fd613011-3872-4f37-b4aa-0d343c051a27",
      "imageURL": "https://im.runware.ai/image/ii/aa418b0f-4b83-4c3d-96c5-30abf4699a4d.jpg"
      "cost": 0006,
    }
  ]
}

taskUUID

stringUUID v4

The API will return the taskUUID you sent in the request. This way you can match the responses to the correct tasks.

imageUUID

stringUUID v4

The unique identifier of the image.

imageURL

string

If outputType is set to URL, this parameter contains the URL of the image to be downloaded.

If outputType is set to base64Data, this parameter contains the base64-encoded image data.

If outputType is set to dataURI, this parameter contains the data URI of the image.

cost

float

if includeCost is set to true, the response will include a cost field for each task object. This field indicates the cost of the request in USD.