API v2

(Click here for older API v1 docs)

Scalable and the fastest way to remove backgrounds automatically.


$ curl -H 'Authorization: YOUR_API_KEY'
       -F 'image_file=@/your/image/file.jpg'
       -f https://api.backgroundcut.co/v2/cut/
       -o output.png
  

Sample Code

Remove background from image file

$ curl -H 'Authorization: YOUR_API_KEY' \
        -F 'image_file=@/your/image/file.jpg' \
        -F 'quality=medium' \
        -f https://api.backgroundcut.co/v2/cut/ \
        -o output.png


import requests

response = requests.post(
    'https://api.backgroundcut.co/v2/cut/',
    files={'image_file': open('/path/to/file.jpg', 'rb')},
    data={'quality': 'high'},
    headers={'Authorization': 'YOUR_API_KEY'},
)

if response.status_code == requests.codes.ok:
    with open('output.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text)


// Requires "guzzle" to be installed (see guzzlephp.org)
// If you have problems with our SSL certificate with error 'Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)'
// follow these steps to use the latest cacert certificate for cURL: https://github.com/guzzle/guzzle/issues/1935#issuecomment-371756738

$client = new GuzzleHttp\Client();
$res = $client->post('https://api.backgroundcut.co/v2/cut/', [
    'multipart' => [
        [
            'name'     => 'image_file',
            'contents' => fopen('/path/to/file.jpg', 'r')
        ],
        [
            'name'     => 'quality',
            'contents' => 'medium'
        ]
    ],
    'headers' => [
        'Authorization' => 'YOUR_API_KEY'
    ]
]);

$fp = fopen("output.png", "wb");
fwrite($fp, $res->getBody());
fclose($fp);

                                

const request = require("request");
const fs = require("fs");

const settings = {
    url: "https://api.backgroundcut.co/v2/cut/",
    apiKey: "YOUR-API-KEY",
    sourceImagePath: "/path/to/file.jpg",
    outputImagePath: "output.png"
};

request.post(
    {
        url: settings.url,
        formData: { image_file: fs.createReadStream(settings.sourceImagePath), },
        headers: { "Authorization": settings.apiKey, },
        encoding: null,
    },
    function(error, response, body) {
        if (error) { console.log(error); return; }
        if (response.statusCode != 200) { console.log(body.toString('utf8')); return; }
        fs.writeFileSync(settings.outputImagePath, body);
    }
);
$ curl -H 'Authorization: Token YOUR_API_KEY'  \
        -F 'file_url=https://www.example.com/example.jpg'  \
        -f https://backgroundcut.co/api/v1/cut/ 
import requests

URL = "https://backgroundcut.co/api/v1/cut/"

data={
    'file_url': 'https://www.example.com/example.jpg',
}

headers = {'Authorization': 'Token YOUR_API_KEY'}
response = requests.post(URL, data=data, headers=headers)

print(response.json())

// use Guzzle
// https://docs.guzzlephp.org/en/stable/

post("https://slazzer.com/api/v1/remove_image_background", [
'multipart' => [
[
    'name' => 'source_image_file',
    'contents' => fopen('SOURCE_IMAGE_FILE_PATH', 'r')
]
],
'headers' => [
'API-KEY' => 'YOUR_API_KEY'
]
]);

$res_body = $res->getBody()->getContents();
echo $res_body;
?>

const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');

const imagePath = 'YOUR_IMAGE_PATH'

const settings = {
"endpointPath": "https://slazzer.com/api/v1/remove_image_background",
"apiKey": "YOUR_API_KEY"
}

function endpointHeader(apiKey, formdata){
return {
headers: {
    'Content-Type':'multipart/form-data; boundary=' + formdata.getBoundary(),
    'API-KEY': apiKey
}
}
}

const formdata = new FormData;
formdata.append('source_image_file', fs.createReadStream(imagePath));

axios.post(
settings.endpointPath, formdata, endpointHeader(settings.apiKey, formdata)
)
.then(response => console.log(response.data))
.catch(errors => console.log(errors.response.data));

Output Formats

You can request one of three formats via the "return_type" parameter:

Output Type Resolution Pros and cons
PNG Up to 12 Megapixels
  • + Simple integration
  • + Supports transparency
  • - Large file size
ZIP Up to 12 Megapixels
  • + Small file size
  • + Supports transparency
  • - Integration requires compositing
WEBP Up to 12 Megapixels
  • + Simple integration
  • + Small file size
  • - Not as widely used format
JPG Up to 12 Megapixels

How to use quality parameter for WEBP format

WebP is a modern image format that provides superior lossless and lossy compression for images. Depending on the level of compression, it can range from 1 to 100. For example, to get lossless output, you can send a "return_type" parameter as WEBP_100, and to set some compression, you can send the "return_type" parameter as WEBP_50.

API Reference

  • API Endpointhttps://api.backgroundcut.co/v2/cut/
  • MethodPOST
  • Request Bodymultipart/form-data
Authorization
  • Authorization (string)YOUR_API_KEY
Parameters
  • image_fileSource image file (binary). Supports only PNG, JPG, WEBP
  • image_file_b64Source image file (base64-encoded string).
  • max_resolutionMaximum output resolution (specified in pixels). Upto 12000000, i.e 12MP. For eg, 4000000 outputs image of maximum resolution of 4MP.
  • quality'High', 'Medium'(default), 'Low'. 'High' quality takes the most time while 'Low' takes the least time to process.
  • return_type'PNG'(default), 'ZIP' or 'WEBP'.
  • channelsRequest either the finalized image ("rgba", default) or an alpha mask ("alpha"). Note: Since BackgroundCut also applies RGB color corrections on edges, using only the alpha mask often leads to a lower final image quality. Therefore "rgba" is recommended when quality parameter is set to 'High'.
Response
  • Code : 200Successfully removed image background
  • Code: 400Error: Invalid parameters or unable to process input image file (No credit deducted)
  • Code: 401Error: API-KEY missing or invalid API-KEY or Denied API-KEY (no credits left)
  • Code: 405Error: Invalid request METHOD (Ensure the request is of type POST)
  • Code: 413Error: Request Entity Too Large. File size should not exceed 12MB when uploading.
  • Code: 429Error: API rate limit crossed (No credit deducted)
  • Code: 450Error: Invalid POST URL (Ensure the URL is https://api.backgroundcut.co/v2/cut/)
API Rate Limit
  • BackgroundCut implements rate limits dynamically. There is a default limit of 40 requests per minute for APIs. Depending on usage in the current minute, rate limits double the following minute up to a maximum of 500 requests per minute. The basic rate limit of 40 can be increased upon request.
Data Storage Policy
  • Neither uploaded images nor generated output images are stored by BackgroundCut. The output is returned as soon as the response is generated and is never stored on our servers. It is the user's responsibility to save outputs in one go.