Remove Background API
The following code snippet demonstrates the usage of the Remove Background API provided by Picsart. This API allows users to remove the background from an image using a simple HTTP request. By specifying the desired output type, format, and providing the URL of the image, you can retrieve a cutout version of the image with the background removed.
Remove background from an image
curl -X 'POST' \
'https://api.picsart.io/tools/1.0/removebg' \
-H 'accept: application/json' \
-H 'x-picsart-api-key: <API KEY HERE>' \
-H 'Content-Type: multipart/form-data' \
-F 'output_type=cutout' \
-F 'format=PNG' \
-F 'image_url=https://cdn140.picsart.com/13902645939997000779.jpg'
const FormData = require('form-data');
const http = require("https");
const form = new FormData();
const fs = require('fs');
form.append('bg_blur', '0');
form.append('scale', 'fit');
form.append('image_url', 'https://cdn140.picsart.com/03328718565819952445.jpeg');
form.append('bg_image_url', 'https://cdn140.picsart.com/22183977156062232333.png');
form.append('format', 'JPG');
form.append('output_type', 'cutout');
const options = {
"method": "POST",
"hostname": "api.picsart.io",
"port": null,
"path": "/tools/1.0/removebg",
"headers": {
"accept": "application/json",
"X-Picsart-API-Key": "YOURAPIKEYHERE",
...form.getHeaders()
}
};
const req = http.request(options, function (res) {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.on("error", (e) => {
console.error(e);
});
form.pipe(req);
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("multipart/form-data");
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("bg_blur","0")
.addFormDataPart("scale","fit")
.addFormDataPart("image_url", "https://cdn140.picsart.com/03328718565819952445.jpeg")
.addFormDataPart("bg_image_url", "https://cdn140.picsart.com/22183977156062232333.png")
.addFormDataPart("format","JPG")
.addFormDataPart("output_type","cutout")
.build();
Request request = new Request.Builder()
.url("https://api.picsart.io/tools/1.0/removebg")
.method("POST", body)
.addHeader("accept", "application/json")
.addHeader("X-Picsart-API-Key", "YOURAPIKEYHERE")
.build();
Response response = client.newCall(request).execute();
import requests
url = "https://api.picsart.io/tools/1.0/removebg"
payload={
"bg_blur": "0",
"scale": "fit",
"image_url": "https://cdn140.picsart.com/03328718565819952445.jpeg",
"bg_image_url": "https://cdn140.picsart.com/22183977156062232333.png",
"format": "JPG",
"output_type": "cutout"}
files=[]
headers = {"accept": "application/json", "X-Picsart-API-Key": "YOURAPIKEY"}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
To remove the background from an image using the Remove Background API, make a POST request to https://api.picsart.io/tools/1.0/removebg with the following parameters:
accept
: Set the value to application/json to indicate the desired response format.x-picsart-api-key
: Replace with your actual API Key obtained from Picsart.Content-Type
: Set the value to multipart/form-data to indicate the request format.output_type
: Specify the desired output type, such as cutout.format
: Specify the desired format for the resulting image, such as PNG.image_url
: Provide the URL of the image you want to process.
Updated 4 days ago