# Positioning

The goal of the Image Editing API is to give you as much control as possible on how to position your subject in the result image, while also making it as easy as possible to implement the most common use cases.

## General Principle

By default, the Image Editing API positioning system will `fit` the [`referenceBox`](#reference-box) inside the [`targetArea`](#target-area).

<table data-header-hidden data-full-width="true"><thead><tr><th align="center"></th><th align="center"></th></tr></thead><tbody><tr><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FKABpq2m4YsOKneYKLimd%2FGroup.png?alt=media&#x26;token=a45ad243-e819-435f-9b17-86a82015e78a" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2Fg8FHxtGXCqyyBQIrqqVe%2FGroup%202.png?alt=media&#x26;token=05345fce-8557-4d30-acf5-ea4c02f0a9d8" alt=""></td></tr><tr><td align="center"><em>Original Image</em></td><td align="center"><em>Result Image</em></td></tr></tbody></table>

The next sections of this page will go into the details of how this system works and how you can control it to get exactly the result that you need.

## Target Area

The `targetArea` (green square in the picture) represents the space available inside the result image to display the subject.

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2F1vFCCGLnWALnIlDr2owg%2Ftarget%20area.png?alt=media&#x26;token=bd912b5b-5636-40c5-b8e1-ec1f6de4010c" alt="" width="375"><figcaption></figcaption></figure>

By default, the `targetArea` will take all the space available in the result image, and the subject will be:

* resized to `fit` the `targetArea`
* centered inside the `targetArea`

If you prefer for the subject to fill as much space as possible inside the `targetArea`, this is possible by setting the argument `scaling` to `fill`:

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FgdFpfh1huXpsyq3fW1hh%2FScreenshot%202024-03-26%20at%2014.40.40.png?alt=media&#x26;token=2b77d28a-01c6-4a0e-8c3d-ac0f4a037438" alt="" width="375"><figcaption></figcaption></figure>

If you prefer for the subject to be positioned somewhere else than at the center of the `targetArea`, this is also possible, please see the section [Horizontal and Vertical Alignments](#horizontal-and-vertical-alignments) for more details.

## Padding

If you want to add some space between the target area and the borders of the result image, this can be controlled through the argument `padding`.

The API supports two formats to specify the amount `padding`:

* relative values, expressed in percentages (e.g., `0.2` or `20%`)
* absolute values, expressed in pixels (e.g., `100px`)

In this example, `padding` has been set to `0.1` (10%):

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FGZIMcdgUEVc4RjCcHOVR%2FScreenshot%202024-03-26%20at%2014.28.50.png?alt=media&#x26;token=7d46bd32-39e7-44f2-9189-a786f6cbc387" alt="" width="375"><figcaption></figcaption></figure>

This results in the addition of empty space between each edge of the result image and the target area.&#x20;

Here, the size of this empty space is equal to 10% of the size of the corresponding side of the result image (i.e. 10% of the width for the left and right edges, and 10% of the height for the top and bottom edges).

If needed, a different amount of padding can be set for each edge of the result image, trough the arguments `paddingLeft`, `paddingRight`, `paddingTop` and `paddingBottom`.

In this example, the global `padding` is still set to `0.1` (10%), but additionally:

* `paddingTop` has been overriden and set to `0.2` (20%)&#x20;
* `paddingLeft` has also been overriden and set to `0.2` (20%)

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2F3FlTm0F2Z239JXtKrm3p%2FScreenshot%202024-03-26%20at%2014.54.29.png?alt=media&#x26;token=682b7272-9d62-468d-bdbc-c233fed12ef2" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="warning" %}
Padding is computed with respect to the width and height of the result image.

This means that a `paddingLeft` of 20% will be equal to 20% of the width of the result image.

This also means that if the percentage of padding on two opposing sides is greater than or equal to 100%, it will lead to an error because it means that there's no space left to position the subject.

(Because of this, since the argument `padding` applies to all sides, its value must be strictly less than `0.5`)
{% endhint %}

## Cropped Subject

In the previous sections, we saw that, by default, the Image Editing API will cut out the main subject of the input image, position it at the center of the result image, and apply the specified padding around it:

<div align="center"><figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2Ft7BqEU3M2ecQ9lsDnQWj%2FScreenshot%202024-03-26%20at%2015.32.06.png?alt=media&#x26;token=994dde1a-0f24-4748-82b9-964985501988" alt="" width="375"><figcaption></figcaption></figure></div>

However, if some of the sides of the subject are cropped, then by default the API will not apply any padding to these sides.

The goal of this behavior is to avoid having empty whitespace between the cropped edges of the subject and the edges of the result image:

<div align="center"><figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FZlDBVfNShE4xKdrVctPu%2FScreenshot%202024-03-26%20at%2015.14.17.png?alt=media&#x26;token=10d6492d-e9fb-4662-b96d-2b54d5951244" alt="" width="375"><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Snapping cropped edges to the side of the result image will only happen when a single edge has been cropped for a given direction.

Here are some examples:

* if the left edge of the subject is cropped:
  * snapping will happen for the horizontal direction and the subject will be snapped to the left edge of the result image
  * snapping will not happen for the vertical direction and the subject will be centered vertically in the result image
* if the left and right edges of the subject are cropped
  * snapping will not happen for the horizontal dimension and the subject will be centered horizontally in the result image
  * snapping will not happen for the vertical dimension and the subject will be centered vertically in the result image
* if the top and bottom edges of the subject are cropped:
  * snapping will not happen for the horizontal dimension and the subject will be centered horizontally in the result image
  * snapping will not happen for the vertical dimension and the subject will be centered vertically in the result image
* if the left and top edges of the subject are cropped:
  * snapping will happen for the horizontal direction and the subject will be snapped to the left edge of the result image
  * snapping will happen for the vertical direction and the subject will be snapped to the top edge of the result image
* if the left, top and right edges of the subject are cropped:
  * snapping will not happen for the horizontal dimension and the subject will be centered horizontally in the result image
  * snapping will happen for the vertical direction and the subject will be snapped to the top edge of the result image
    {% endhint %}

If you want, you can disable this behavior by setting the parameter `ignorePaddingAndSnapOnCroppedSides` to `false` in your API call, which will produce this result:

<div align="center"><figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FaMfqWvEooqoNHbUHLPNn%2FScreenshot%202024-03-26%20at%2015.14.42.png?alt=media&#x26;token=e15e7adc-c24d-44d2-903d-bcb266024aff" alt="" width="375"><figcaption></figcaption></figure></div>

## Margin

In the previous section, we saw that, by default, padding will not be applied to cropped edges and that it's also possible to disable this behavior.

However, there might be situations where you want to keep this behavior, but you also need to always have some amount of empty space between the subject and the border of the result image.

A typical use case would when a background or an overlay will be added to the result image and you want to make sure that this background or overlay will not go over the subject.

This behavior can be implemented through the argument `margin`.

As with `padding`, two formats are supported to specify the amount `margin`:

* relative values, expressed in percentages (e.g., `0.2` or `20%`)&#x20;
* absolute values, expressed in pixels (e.g., `100px`)

Here's an example where `padding` has been set to `0.1` and `margin` set to `0.05`:

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FQLY3WKT0sRI3DXuxaAph%2FScreenshot%202024-03-26%20at%2015.01.35.png?alt=media&#x26;token=60ac89e8-c7e0-4c51-acdc-c75192796f55" alt="" width="375"><figcaption></figcaption></figure>

And just like with the padding, it's also possible to set different values for each edge, through the arguments `marginLeft`, `marginRight`, `marginTop`, and `marginBottom`.

In this example, `margin` is still set to `0.1` and `marginBottom` has been set to `0.2`:

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FmXe6Qm3lE34gVYbgzQpO%2FScreenshot%202024-03-26%20at%2015.04.45.png?alt=media&#x26;token=14e04275-a721-4ad4-b9f3-9c305a697b50" alt="" width="375"><figcaption></figcaption></figure>

## Reference Box

The `referenceBox` corresponds to either:

* the `subjectBox`, which is the rectangle around the cutout subject
* the `originalImage`, which is the entire input image

<figure><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2Fe7cK739ARVvB18E0gibb%2FGroup-2.png?alt=media&#x26;token=dde3ced7-a814-41ca-8c8a-47991f4e5e76" alt="" width="375"><figcaption></figcaption></figure>

By default, the Image Editing API will:

* set the `referenceBox` to the `subjectBox`
* `fit` the `referenceBox` inside the `targetArea`

However, if you want the cutout subject to retain the position it had in the original image, you can do it by setting the argument `referenceBox` to `originalImage`.

<table data-header-hidden data-full-width="true"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th></tr></thead><tbody><tr><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FFVXYG0QJLpY32xw2xe3v%2FGroup.png?alt=media&#x26;token=ec13df36-7b22-4982-8ff1-38def21b1b61" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FL6VPzcyF5VeZZikzudrl%2FGroup%202.png?alt=media&#x26;token=eac31746-3e19-4987-a383-8167b35da812" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FZ4FJpfLU1VYwwsRLLcZS%2FGroup%203.png?alt=media&#x26;token=42e9924a-3bbb-41d4-b92d-662f1a5c80e2" alt=""></td></tr><tr><td align="center"><em>(original image)</em></td><td align="center"><code>referenceBox=subjectBox</code><br><em>(default value)</em></td><td align="center"><code>referenceBox=originalImage</code></td></tr></tbody></table>

{% hint style="info" %}
Retaining the original position of the subject can be useful when replacing an existing background by a new [AI Background](https://docs.photoroom.com/image-editing-api-plus-plan/ai-backgrounds).
{% endhint %}

## Horizontal and Vertical Alignments

If needed, it's possible to manually control both the horizontal and vertical alignments of the subject.

{% hint style="info" %}
Manually controlling the alignment is very useful when you want to position subjects with different aspect ratios is a harmonious way, like here where the subjects have been vertically aligned at the bottom:

![](https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FuTYrXOFuQBHxKnPpjreq%2FGroup.png?alt=media\&token=787aaa30-24d9-451f-9f87-535efdae3a94)
{% endhint %}

### Controlling the Horizontal Alignment

To control the horizontal alignment, you need to set the argument `horizontalAlignment` with one of the following values: `left`, `center` or `right`.

<table data-header-hidden data-full-width="true"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th></tr></thead><tbody><tr><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FNpZeHXitkCUTUngwlDoQ%2Fleft.jpg?alt=media&#x26;token=88cd56cc-2ee0-4db3-80ce-be17d7333772" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FfHgruBmkgljfxayp4XWP%2Fcenter.jpg?alt=media&#x26;token=0d2ad759-5e51-4d18-a572-db1d3062f5e3" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FoHuCcpyeGdy5GDHjzGCZ%2Fright.jpg?alt=media&#x26;token=dfcebf57-112f-4dde-bb4e-e318429ce945" alt=""></td></tr><tr><td align="center"><code>horizontalAlignment=left</code></td><td align="center"><code>horizontalAlignment=center</code></td><td align="center"><code>horizontalAlignment=right</code></td></tr></tbody></table>

### Controlling the Vertical Alignment

To control the horizontal alignment, you need to set the argument `verticalAlignment` with one of the following values: `top`, `center` or `bottom`.

<table data-header-hidden data-full-width="true"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th></tr></thead><tbody><tr><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2FbPpHe5D6INWpTmsgpllv%2Ftop.jpg?alt=media&#x26;token=724feb7b-745b-4cc8-9f5c-11325be8dc50" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2Ff1yFgbunoGGvmDP8kkqI%2Fcenter.jpg?alt=media&#x26;token=832afb8c-fd9b-4861-b1c6-ea2a9b599b14" alt=""></td><td align="center"><img src="https://2855892273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1SYxn7dWbQYsNtUdJE3f%2Fuploads%2F4cST9ka0C46jMTHVKSIS%2Fbottom.jpg?alt=media&#x26;token=622ad413-394b-48a9-bdb6-d08e4da3b98c" alt=""></td></tr><tr><td align="center"><code>verticalAlignment=top</code></td><td align="center"><code>verticalAlignment=center</code></td><td align="center"><code>verticalAlignment=bottom</code></td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.photoroom.com/image-editing-api-plus-plan/positioning.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
