Background Removal and Create mask Endpoint
Overview
This endpoint removes background from image and create mask.
Request
--request POST 'https://modelslab.com/api/v6/image_editing/removebg_mask' \
Make a POST
request to https://modelslab.com/api/v6/image_editing/removebg_mask endpoint and pass the required parameters as a request body to the endpoint.
Body Attributes
Parameter | Description | Values |
---|---|---|
key | Your API Key used for request authorization | string |
image | Link or valid base64 data following the format data:image/jpeg;base64,{your_base64_string} for the image you want your generations to resemble | URL, base64 string |
alpha_matting | Whether to perform alpha matting. It accepts true or false . Default is false | boolean |
post_process_mask | Whether to post-process the mask image. It accepts true or false . Default is false | boolean |
only_mask | Whether to return only the mask image. It accepts true or false . Default is false | boolean |
seed | The seed used to reproduce results. The same seed will give you the same image again. Pass null for a random number. | integral value |
alpha_matting_foreground_threshold | The threshold for alpha matting foreground. The default is 240 | integer (0 to 240) |
alpha_matting_background_threshold | The threshold for alpha matting background. The default is 20 | integer (0 to 20) |
alpha_matting_erode_size | The erode size for alpha matting. The default is 5 | integer (0 to 5) |
webhook | Provide a URL to receive a POST API call once the image generation is complete | URL |
track_id | This ID is returned in the response to the webhook API call and will be used to identify the webhook request. | integral value |
Example
Body
Body
{
"key":"",
"seed":12345,
"image":"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"post_process_mask": false,
"only_mask": false,
"alpha_matting":false,
"webhook": null,
"track_id": null
}
Request
- JS
- PHP
- NODE
- PYTHON
- JAVA
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"key":"",
"seed":12345,
"image":"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"post_process_mask": false,
"only_mask": false,
"alpha_matting":false,
"webhook": null,
"track_id": null
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://modelslab.com/api/v6/image_editing/removebg_mask", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
<?php
$payload = [
"key" =>"",
"seed" =>12345,
"image" => "https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"post_process_mask" => false,
"only_mask" => false,
"alpha_matting" =>false,
"webhook" => null,
"track_id" => null
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://modelslab.com/api/v6/image_editing/removebg_mask',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
var request = require('request');
var options = {
'method': 'POST',
'url': 'https://modelslab.com/api/v6/image_editing/removebg_mask',
'headers': {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"key":"",
"seed":12345,
"image":"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"post_process_mask": false,
"only_mask": false,
"alpha_matting":false,
"webhook": null,
"track_id": null
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
import requests
import json
url = "https://modelslab.com/api/v6/image_editing/removebg_mask"
payload = json.dumps({
"key":"",
"seed":12345,
"image":"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"post_process_mask": false,
"only_mask": false,
"alpha_matting":false,
"seed": None,
"webhook": None,
"track_id": None
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"key\":\"\",\n \"seed\":12345,\n \"image\":\"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png\",\n \"post_process_mask\": false,\n \"only_mask\": false,\n \"alpha_matting\":false,\n \"webhook\": null,\n \"track_id\": null\n}");
Request request = new Request.Builder()
.url("https://modelslab.com/api/v6/image_editing/removebg_mask")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
Response
Example Response
{
"status": "success",
"generationTime": 4.713963508605957,
"id": 96708,
"output": [
"https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/generations/fcc2d175-1272-4ed6-a6b9-02a8f26af08c.png"
],
"proxy_links": [
"https://cdn2.stablediffusionapi.com/generations/fcc2d175-1272-4ed6-a6b9-02a8f26af08c.png"
],
"meta": {
"alpha_matting": "0",
"alpha_matting_background_threshold": 20,
"alpha_matting_erode_size": 5,
"alpha_matting_foreground_threshold": 240,
"base64": "no",
"file_prefix": "fcc2d175-1272-4ed6-a6b9-02a8f26af08c",
"image_url": "https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/livewire-tmp/2ILk0g1tgpk7wFFlc72hyAtdmHjxAj-metaZjNkNzVjMWQtMTI1OS00NDA4LTg0YmQtNzA0NTVmMTljZjcwLTAucG5n-.png",
"n_samples": 1,
"only_mask": "0",
"outdir": "out",
"post_process_mask": "0",
"seed": 1,
"temp": "no"
}
}