# Resize and padding

### Image size

Image can be upscaled and placed in any shape/canvas.

It is possible to set just one dimension, the other will be calculated automatically and image ratio will be preserved. Also padding parameter will be used in that case.

{% hint style="info" %}
Notice that Deep Image supports AI upscale up to 4x of the original resolution. Everything above that will be upscaled traditionally with bicubic supersampling.
{% endhint %}

Let's check some resize examples.

This is the input image.

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FpYZCAGpNZ2suwXGH8RqY%2Fimage.png?alt=media&#x26;token=135608d3-70f3-43e0-b5a7-9214e4c04d05" alt=""><figcaption><p>Input image: 640x427</p></figcaption></figure>

The destination image size can be controller with "width" and "height" parameters.

{% hint style="info" %}
Image can be also upscaled with "print\_size" and "dpi" parameters. More on that here in Image Processing / Print chapter.
{% endhint %}

<table><thead><tr><th width="244">Parameter name</th><th>Description</th></tr></thead><tbody><tr><td>width</td><td><ul><li>string f.e. - "200%" - it will upscale image by factor 2.</li><li>integer f.e. - 200 - it will upscale/downscale image to have width 200 pixels. If the height is not given it will be calculated to match image ratio.</li></ul></td></tr><tr><td>height</td><td><ul><li>string f.e. - "200%" - it will upscale image by factor 2.</li><li>integer f.e. - 200 - it will upscale/downscale image to have height 200 pixels. If the width is not given it will be calculated to match image ratio.</li></ul></td></tr><tr><td>min_length</td><td>integer f.e. - 200 - it will upscale/downscale image to have minimum 200 pixels width or height. That will also be applied to identified item/content when removing background.</td></tr><tr><td>generative_upscale</td><td>true/false - Turns on upscaling based on diffusion algorithms. Image is generated max in 2048x2048 resolution and then upscaled further using standard AI upscaling. More on that: <a href="https://documentation.deep-image.ai/image-processing/image-generation#generative-upscaling">https://documentation.deep-image.ai/image-processing/image-generation#generative-upscaling</a></td></tr><tr><td>upscale_parameters: type</td><td><ul><li>v1 - (default) - basic AI upscaling method</li><li>text_x4 - used to upscale images containing text, such as invoices or product photos with labels, optimizing clarity and readability of the text.</li></ul></td></tr></tbody></table>

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "upscale_parameters": {
        "type": "text_x4"
    }
}
```

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

```
curl --request POST \
     --url https://deep-image.ai/rest_api/process_result \
     --header 'content-type: application/json' \
     --header 'x-api-key: API_KEY' \
     --data '{
         "url": "https://deep-image.ai/api-example.png",
         "width": 1000
      }'
```

{% endtab %}

{% tab title="Python client" %}

```python
import os

import deep_image_ai_client

configuration = deep_image_ai_client.Configuration(
    host="https://deep-image.ai"
)

configuration.api_key['ApiKeyAuth'] = os.environ["API_KEY"]

with deep_image_ai_client.ApiClient(configuration) as api_client:
    api_instance = deep_image_ai_client.DefaultApi(api_client)
    process_payload = {"url": "https://deep-image.ai/api-example.jpg", "width": 1000}
    api_response = api_instance.rest_api_process_result_post(process_payload)

```

{% endtab %}
{% endtabs %}

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FewRyjwaINvliF2lhVuxj%2Fimage.png?alt=media&#x26;token=74364087-f136-4006-9bea-4d60e6776b67" alt=""><figcaption><p>Result image: 1000x667</p></figcaption></figure>

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "height": 1000
}
```

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

```
curl --request POST \
     --url https://deep-image.ai/rest_api/process_result \
     --header 'content-type: application/json' \
     --header 'x-api-key: API_KEY' \
     --data '{
         "url": "https://deep-image.ai/api-example.png",
         "height": 1000
      }'
```

{% endtab %}

{% tab title="Python client" %}

```python
import os

import deep_image_ai_client

configuration = deep_image_ai_client.Configuration(
    host="https://deep-image.ai"
)

configuration.api_key['ApiKeyAuth'] = os.environ["API_KEY"]

with deep_image_ai_client.ApiClient(configuration) as api_client:
    api_instance = deep_image_ai_client.DefaultApi(api_client)
    process_payload = {"url": "https://deep-image.ai/api-example.jpg", "height": 1000}
    api_response = api_instance.rest_api_process_result_post(process_payload)

```

{% endtab %}
{% endtabs %}

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FMogXaGIYxeuNSBzDOBly%2Fimage.png?alt=media&#x26;token=e692deff-788e-42ab-bc4d-d075986d3eee" alt=""><figcaption><p>Result image: 1500x1000</p></figcaption></figure>

If width and height is specified together, image with original ratio will be upscaled and placed inside "width x height" canvas. Fitting parameter will be used accordingly.

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "height": 1000
}
```

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

```
curl --request POST \
     --url https://deep-image.ai/rest_api/process_result \
     --header 'content-type: application/json' \
     --header 'x-api-key: API_KEY' \
     --data '{
         "url": "https://deep-image.ai/api-example.png",
         "width": 1000,
         "height": 1000
      }'
```

{% endtab %}

{% tab title="Python client" %}

```python
import os

import deep_image_ai_client

configuration = deep_image_ai_client.Configuration(
    host="https://deep-image.ai"
)

configuration.api_key['ApiKeyAuth'] = os.environ["API_KEY"]

with deep_image_ai_client.ApiClient(configuration) as api_client:
    api_instance = deep_image_ai_client.DefaultApi(api_client)
    process_payload = {"url": "https://deep-image.ai/api-example.jpg", "width": 1000, "height": 1000}
    api_response = api_instance.rest_api_process_result_post(process_payload)

```

{% endtab %}
{% endtabs %}

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FntQpTTIBmleeecoMN698%2Fimage.png?alt=media&#x26;token=58e657c9-1021-403f-938b-0480bd524237" alt=""><figcaption><p>Result image: 1000x1000</p></figcaption></figure>

### Fitting and padding

According to specified width and height image can either cropped to fill given canvas or placed fully into the given canvas. When image is cropped checking the image content can be performed.

How the image is placed into destination canvas (with width and height parameters) is controlled by "fit" parameter.

<table><thead><tr><th width="189">Name</th><th>Description</th></tr></thead><tbody><tr><td>canvas</td><td><strong>(default)</strong> - whole image is placed into width x height canvas, missing space is filled with background color. Additionally "outpainting" might be specified - more on that <a href="../inpainting-and-outpainting-uncrop#outpainting-uncrop">here</a> </td></tr><tr><td>crop</td><td>Image is cropped to match destination width x height canvas. Crop is content aware by default. It can be changed to be on center as well.</td></tr><tr><td>bounds</td><td>image is upscaled to fit specified width x height.</td></tr><tr><td>cover</td><td> image is upscaled to fully cover specified width x height.</td></tr></tbody></table>

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

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "height": 1000,
    "fit": "canvas"
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2Fesc1EHlid6BAI53nadEm%2Fimage.png?alt=media&#x26;token=2a371f2b-fa75-425d-bbd1-7b3fa828cc36" alt=""><figcaption><p>Result image: 1000x1000</p></figcaption></figure>
{% endtab %}

{% tab title="Crop" %}

```json
{
    "url": "https://deep-image.ai/api-example.png",
    "width": 1000,
    "height": 1000,
    "fit": {
         "crop": "content"
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FEwRc4oFSKNWzb4mc5nh5%2Fimage.png?alt=media&#x26;token=47f8c15a-bf5b-4407-bb2c-7ed60d5389f9" alt=""><figcaption><p>Result image: 1000x1000</p></figcaption></figure>
{% endtab %}
{% endtabs %}

### Types of crop

Image cropping can be performed from the image center or from the center of identified content.

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FRi8qRJfCXkvIOUdYDPLI%2F153285278_c61b21cf63_o.jpg?alt=media&#x26;token=8aa00394-f36e-44a1-89e6-58cde7c32a65" alt=""><figcaption><p>Input</p></figcaption></figure>

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

```json
{
    "url": "https://deep-image.ai/api-example2.jpg",
    "width": 1000,
    "height": 1000,
    "fit": {
         "crop": "center"
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FKLcdXT2Ami8e8rv2Sc1U%2Fimage.png?alt=media&#x26;token=67c327e1-86f3-4610-8b4f-88559410be8b" alt=""><figcaption><p>Result</p></figcaption></figure>
{% endtab %}

{% tab title="Content" %}

```json
{
    "url": "https://deep-image.ai/api-example2.jpg",
    "width": 1000,
    "height": 1000,
    "fit": {
         "crop": "content"
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2F4cvCIKuuVJeInmbEYS3d%2Fimage.png?alt=media&#x26;token=f57882eb-6bdd-415a-89ff-7e351eef8084" alt=""><figcaption><p>Result</p></figcaption></figure>

{% endtab %}

{% tab title="Item" %}

```json
{
    "url": "https://deep-image.ai/api-example2.jpg",
    "width": 1000,
    "height": 1000,
    "fit": {
         "crop": "item"
    }
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FlLaDhaCBVbgLQ8jTSE5g%2Fapi_example2-result.jpg?alt=media&#x26;token=c739df50-8fc9-47cf-9ed4-fb76044efc86" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

Crop "item" type is not particularly useful without padding and removing background. But it's very useful for "product" type manipulations. More on that at [Remove background and item cropping](https://documentation.deep-image.ai/background-removal-and-generation#item-cropping).

Content aware cropping prevents objects being cut by the desired canvas proportions. Let's see some examples.

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FLIU2HuyvWmhhBvApGOBs%2Fbird-7653386_1920.jpg?alt=media&#x26;token=2979f2ae-8844-4bdd-8543-e026931a9201" alt=""><figcaption><p>Input image</p></figcaption></figure>

```json
{
    "width": 1000,
    "height": 1000,
    "fit": "crop"
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FMyL8qy8oo8bINgk7zK5y%2Fbird-7653386_1920.jpg?alt=media&#x26;token=ea2bbc83-61f2-4aad-aa7b-bf16d996aeb3" alt=""><figcaption></figcaption></figure>

When margin is needed, parameter padding can be used for that. It can be specified either in pixels  or in percentages.

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

```json
{
    "url": "https://deep-image.ai/api-example.jpg",
    "width": 1000,
    "height": 1000,
    "fit": "canvas",
    "padding": "20%"
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2Fluyt2SF3JBXGi6Xi2TTT%2Fimage.png?alt=media&#x26;token=ea6a2535-e6d9-4adb-a900-8ff80787b7d8" alt=""><figcaption><p>Result image: 1000x1000, content: 800x533, padding 200 pixels</p></figcaption></figure>
{% endtab %}

{% tab title="Crop" %}

```json
{
    "url": "https://deep-image.ai/api-example.jpg",
    "width": 1000,
    "height": 1000,
    "fit": "crop",
    "padding": "20%"
}
```

<figure><img src="https://2652559519-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3i5YcUkcXyIsWHIhRO2d%2Fuploads%2FN6ynMDkTLzuOQG2vEhaT%2Fimage.png?alt=media&#x26;token=4f56338a-601d-4bfc-b577-8e75d67d08eb" alt=""><figcaption><p>Result image: 1000x1000, content: 800x800, padding: 200</p></figcaption></figure>
{% endtab %}
{% endtabs %}

<br>
