Remove bg
This guide shows how to remove image backgrounds using the Remove bg model via the Leonardo.AI REST API.
Sample Request
curl --location 'https://cloud.leonardo.ai/api/rest/v2/generations' \
--header 'authorization: Bearer {api-key}' \
--header 'Content-Type: application/json' \
--data '{
"model": "remove-bg",
"public": false,
"parameters": {
"size": "auto",
"type": "auto",
"format": "png",
"guidances": {
"image_reference": [
{
"image": {
"id": "YOUR_IMAGE_ID",
"type": "UPLOADED"
},
"strength": "MID"
}
]
}
}
}'API Request Endpoint, Headers, Parameters
Endpoint
https://cloud.leonardo.ai/api/rest/v2/generationsHeaders
--header "accept: application/json" \
--header "authorization: Bearer <YOUR_API_KEY>" \
--header "content-type: application/json"Body Parameters
| Parameter | Type | Definition |
|---|---|---|
| model | string | Required. Set to remove-bg. |
| parameters.bg_color | string | Optional. Adds a solid background colour to the cutout, as a hex code or colour name. The output is no longer transparent — pair with the jpg format when possible. |
| parameters.channels | string | Optional. Set to rgba to return the cutout image, or alpha to return only the greyscale alpha mask. Defaults to rgba. |
| parameters.crop | boolean | Optional. Set to true to crop the output image to the bounding box of the subject. Defaults to false. |
| parameters.crop_margin | string | Optional. Margin to add around the cropped subject, as an absolute value (30px) or relative to the subject size (10%). Accepts one, two, or four values. Only applies when crop is enabled. |
| parameters.format | string | Optional. Output image format. Set to png, jpg, or webp. PNG and WebP preserve transparency, JPG does not. PNG output is capped at 10 megapixels, so pair larger size values with webp to keep transparency. Defaults to png. |
| parameters.guidances.image_reference | array | Required. Array of exactly 1 image to remove the background from. Each item specifies an image.id, an image.type (INIT, GENERATION, UPLOADED, GENERATED, or VARIATION), and a strength (LOW, MID, or HIGH). strength defaults to MID. |
| parameters.position | string | Optional. Position of the subject within the output canvas, as original, center, a single percentage, or two percentages for horizontal and vertical placement. |
| parameters.priority | integer | Optional. Priority level for job queue routing. |
| parameters.quantity | integer | Optional. Number of outputs to generate. Fixed at 1. Defaults to 1. |
| parameters.roi | string | Optional. Region of interest to search for the subject, given as two x/y coordinate pairs in pixels (0px 0px 100px 100px) or percentages (0% 0% 100% 100%). Anything outside this rectangle is treated as background. |
| parameters.scale | string | Optional. Scale of the subject relative to the output canvas, as original or a percentage between 10% and 100%. |
| parameters.semitransparency | boolean | Optional. Preserves semi-transparent regions such as glass, smoke, and veils. Defaults to true. |
| parameters.shadow_opacity | integer | Optional. Opacity of the artificial shadow, from 0 to 100. Only applies when a shadow is enabled. |
| parameters.shadow_type | string | Optional. Style of artificial shadow to render under the subject. Set to none, drop, 3d, or car. Defaults to none. |
| parameters.size | string | Optional. Maximum output image resolution. Set to auto, preview, full, or 50MP. PNG output is capped at 10 megapixels regardless of this setting. Defaults to auto. |
| parameters.type | string | Optional. Foreground type hint to improve cutout quality. Set to auto, person, product, car, animal, graphic, transportation, or other. Defaults to auto. |
| parameters.type_level | string | Optional. How specifically the subject should be classified. Set to none to skip classification, 1 for a coarse category, 2 for a specific category, or latest for the most detailed classification available. |
| public | boolean | Optional. Set to false to keep result private. |
Updated about 10 hours ago
Did this page help you?
