# Background removal and generation

Background of the image can be removed with "remove" parameter of background options.

Also removed background can be replaced with generated one.&#x20;

### Background removal

```json
"background": {
    "remove": "auto",
    "color": "#FFFFFF"
}
```

Description of "type" parameter of remove background function.

<table><thead><tr><th width="149">Parameter</th><th>Description</th></tr></thead><tbody><tr><td>remove</td><td><p>Parameter that controls background removal algorithm.</p><ul><li>"auto" - (default) automatically picks remove background type based on image</li><li>"v2" - new remove background algorithm</li><li>"human" - background removal optimized for photos of people</li><li>"item" - background removal optimized for photos of items</li></ul></td></tr><tr><td>color</td><td><p>Parameter that controls image background.</p><p>If given, image alpha channel will be composited with it.</p><ul><li>auto - API first tries to identify frame around the object (like in typical ecommerce product photos) and uses that color. The fallback is white color.</li><li>"#FFFFFF" - RGB color in hexadecimal format.</li><li>"transparent" - background will be empty for image formats that allows that, f.e. PNG or WEBP.</li></ul></td></tr><tr><td>replace</td><td>URL for the simple background image replacement - no shadows or reflections.</td></tr></tbody></table>

Let's check some examples.

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2F0CNQXXg8GXWXIpDmpbBx%2Fperfume-678828_1920.jpg?alt=media&#x26;token=79efe194-5480-422e-9722-57e52a3b7d53" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Click on the result image to see the difference.
{% endhint %}

{% tabs %}
{% tab title="transparent" %}

```json
"remove_background": {
    "type": "auto",
    "background": "transparent"
}
```

{% endtab %}

{% tab title="white" %}

```
"remove_background": {
    "type": "item",
    "background": "#FFFFFF"
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2Fwei0JPrMSdDAYdSQr14J%2Fperfume-678828_1920-background_removed.jpg?alt=media&#x26;token=27cc1955-4c78-4278-93de-1e42eb53893a" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="replace" %}

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "background": {
    "remove": "auto",
    "replace": "https://images.pexels.com/photos/628281/pexels-photo-628281.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FL3VgYIHYuWBrTAH4wAA5%2Fimage.png?alt=media&#x26;token=ebcf7a0e-96ba-4b15-941b-aa609855be57" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FbnqM07KpMUwjEePo3Alr%2Fimage.png?alt=media&#x26;token=31918344-d128-4042-866c-be6208ab7f35" alt=""><figcaption></figcaption></figure>

### Item cropping

Let's combine background removal, padding and cropping. With those combinations we are able to put object with any proportions in a desired canvas frame at the center with distance specified as padding option.

```json
{
    "url": "https://deep-image.ai/api-example2.jpg",
    "width": 1000,
    "height": 1000,
    "fit": {
         "crop": "item"
    },
    "background": {
        "remove": "auto",
        "color": "#FFFFFF"
    },
    "padding": 100
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2F5eETwowUrmRBEKva7MhC%2Fimage.png?alt=media&#x26;token=f08232b2-94ff-458b-afc6-a9a9b5734be0" alt=""><figcaption><p>Result</p></figcaption></figure>

### Background generation

Generating background is fantastic way of improving product photo look. It can be even solution for creating product pack shots without need of manually taking expensive commercial photo shoots.

Let's check what we can do.

<table><thead><tr><th width="218">Parameter</th><th>Description</th></tr></thead><tbody><tr><td>description</td><td>Text prompt describing the scene</td></tr><tr><td>item_area_percentage</td><td>Parameter between 0 and 1 that controls size of the object which is placed in the middle of final image. So, 0.85 is 85%</td></tr><tr><td>sample_num</td><td>Seed for random generator. Basically is "id" of generated image. When not specified, image will randomly different every time.</td></tr><tr><td>color</td><td>When color is specified, generated background is converted to b&#x26;w and then to specified color. It works best for prompts such as: "item standing on plain white background". Color has to be RGB array - f.e. [255,255,255]</td></tr><tr><td>background_url</td><td>Url to image which will be blended together with generated background - it is useful for creating product images that require the same background every time.</td></tr></tbody></table>

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "background": {
        "generate": {
            "description": "item standing on sand with beach in background",
            "item_area_percentage": 0.65,
            "sample_num": 12663
        }
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FAfYP0WsoIKZWb7t2h07S%2Fimage.png?alt=media&#x26;token=125acc8d-50c9-456f-bdd9-a674453bcd46" alt=""><figcaption></figcaption></figure>

Without sample\_num:

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "background": {
        "generate": {
            "description": "item standing on sand with beach in background",
            "item_area_percentage": 0.65
        }
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2F3Y72IJ2hPMiTU50LuGtY%2Fimage.png?alt=media&#x26;token=88d89113-f716-41df-8c31-4143b9e659bf" alt=""><figcaption></figcaption></figure>

We can specify also image resolution:

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "height": 1000,
    "background": {
        "generate": {
            "description": "item standing on sand with beach in background",
            "item_area_percentage": 0.65
        }
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FmT80tURSZ5FNxnxn0J6u%2Fimage.png?alt=media&#x26;token=8fb25907-b7de-4ee3-a6a1-77dbef2e6ddc" alt=""><figcaption></figcaption></figure>

When specifying a color:

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "height": 1000,
    "background": {
        "generate": {
            "description": "item positioned on plain white background",
            "item_area_percentage": 0.65,
            "color": [217,179,190]
        }
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FfhIFa737oWq8UTaS2Eqo%2Fimage.png?alt=media&#x26;token=e4fbe6ad-e169-4bc3-82a8-215751d51750" alt=""><figcaption></figcaption></figure>
