# Image edits

Edit an image by instruction or region, replace its background, change its aspect ratio, recolor it, or replace its text.

Source: https://scifig.ai/docs/api/image-edits

## Operations

`POST /images/edits` with a JSON body. Required scope: `figures:generate`. `image` is a `file_id` (an upload or an earlier output); `operation` selects the edit and defaults to `instruct`. The same tools are on the website’s result toolbar.

| operation | What it does | Fields | Credits |
| --- | --- | --- | --- |
| instruct | Edits the whole image as instructed; with a mask, redraws only the masked region | `instruction` (1–2,000 characters), optional `mask`, `quality` | 50 / 75 / 100 by quality; with a mask 100 |
| white_background | Replaces the background with solid white (not transparency) | `quality` | 50 / 75 / 100 |
| aspect_ratio | Reframes the whole figure to a new ratio, extending the background | `aspect_ratio` (`1:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `16:9`, `21:9`), `quality` | 50 / 75 / 100 |
| recolor | Applies a palette to the artwork, keeping label colours | optional `palette` (1–8 `#RRGGBB`; without it a palette is chosen), `quality` | 50 / 75 / 100 |
| replace_text | Changes text labels in place, keeping font and position | `text_detection`, `replacements` | 100 |

A mask is a PNG uploaded with purpose `mask`, the same size as the image, white where the image may change. A region edit always uses the premium model, whatever `quality` says. `replace_text` does not take `quality`: it always uses the text-editing model and is priced at the premium level.

## Replacing text

- Call [`POST /images/text-detections`](/docs/api/text-detections) on the same image. It is free and returns an `id` and the `texts` with their `index`.
- Send `operation: "replace_text"`, `text_detection: <id>` and `replacements: [{"index": 0, "text": "Figure 1"}]`. Texts you do not list stay as they are.
- An index the detection does not have returns `400 INVALID_REQUEST` with `details.reason = TEXT_INDEX_OUT_OF_RANGE` and charges nothing.

## Credits

Credits are reserved when the job is created, charged once when it succeeds and returned in full when it fails. The response is a job; see [Jobs and bounded waiting](/docs/api/jobs) for its fields and statuses. Errors are listed under [Errors](/docs/api/errors).

## cURL

```bash
curl -X POST https://scifig.ai/api/v1/images/edits \
  -H "Authorization: Bearer $SCIFIG_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Prefer: wait=25" \
  -H "Content-Type: application/json" \
  -d '{
    "image": "<file_id>",
    "operation": "aspect_ratio",
    "aspect_ratio": "16:9"
  }'
```

## JavaScript

```javascript
const response = await fetch('https://scifig.ai/api/v1/images/edits', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SCIFIG_API_KEY}`,
    'Idempotency-Key': crypto.randomUUID(),
    Prefer: 'wait=25',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "image": "<file_id>",
    "operation": "aspect_ratio",
    "aspect_ratio": "16:9"
  }),
});
const result = await response.json();
console.log(response.status, result);
```

## Python

```python
import os
import uuid

import requests

headers = {
    "Authorization": f"Bearer {os.environ['SCIFIG_API_KEY']}",
    "Idempotency-Key": str(uuid.uuid4()),
    "Prefer": "wait=25",
}
response = requests.post(
    "https://scifig.ai/api/v1/images/edits",
    headers=headers,
    json={
        "image": "<file_id>",
        "operation": "aspect_ratio",
        "aspect_ratio": "16:9"
    },
    timeout=60,
)
print(response.status_code, response.json())
```
