> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/openai/openai-python/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit Images

Creates an edited or extended image given one or more source images and a prompt.

This endpoint supports GPT Image models (`gpt-image-1.5`, `gpt-image-1`, `gpt-image-1-mini`, and `chatgpt-image-latest`) and `dall-e-2`.

## Method Signature

```python theme={null}
client.images.edit(
    image=open("source.png", "rb"),
    prompt="Add sunglasses to the person",
    mask=open("mask.png", "rb"),
    model="gpt-image-1.5",
    size="1024x1024"
)
```

## Parameters

<ParamField path="image" type="file" required>
  The image(s) to edit. Must be a supported image file or an array of images.

  **For GPT image models:**

  * Each image should be a `png`, `webp`, or `jpg` file less than 50MB
  * You can provide up to 16 images
  * `chatgpt-image-latest` follows the same input constraints

  **For `dall-e-2`:**

  * You can only provide one image
  * Must be a square `png` file less than 4MB
</ParamField>

<ParamField path="prompt" type="string" required>
  A text description of the desired image(s). The maximum length is:

  * 32000 characters for GPT image models
  * 1000 characters for `dall-e-2`
</ParamField>

<ParamField path="mask" type="file">
  An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. If there are multiple images provided, the mask will be applied on the first image. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
</ParamField>

<ParamField path="model" type="string">
  The model to use for image generation. Options:

  * `gpt-image-1.5` (default)
  * `gpt-image-1`
  * `gpt-image-1-mini`
  * `chatgpt-image-latest`
  * `dall-e-2`
</ParamField>

<ParamField path="n" type="integer">
  The number of images to generate. Must be between 1 and 10.
</ParamField>

<ParamField path="size" type="string">
  The size of the generated images:

  * GPT image models: `1024x1024`, `1536x1024` (landscape), `1024x1536` (portrait), or `auto` (default)
  * `dall-e-2`: `256x256`, `512x512`, or `1024x1024`
</ParamField>

<ParamField path="response_format" type="string">
  The format in which the generated images are returned. Must be one of `url` or `b64_json`.

  Only supported for `dall-e-2` (default is `url`). GPT image models always return base64-encoded images.
</ParamField>

<ParamField path="quality" type="string">
  The quality of the image that will be generated for GPT image models:

  * `auto` (default)
  * `standard`
  * `low`
  * `medium`
  * `high`
</ParamField>

<ParamField path="background" type="string">
  Allows to set transparency for the background of the generated image(s). Only supported for GPT image models:

  * `transparent`
  * `opaque`
  * `auto` (default) - model automatically determines the best background

  If `transparent`, the output format needs to support transparency (`png` or `webp`).
</ParamField>

<ParamField path="input_fidelity" type="string">
  Control how much effort the model will exert to match the style and features, especially facial features, of input images. Only supported for `gpt-image-1` and `gpt-image-1.5` and later models, unsupported for `gpt-image-1-mini`:

  * `low` (default)
  * `high`
</ParamField>

<ParamField path="output_format" type="string">
  The format in which the generated images are returned. Only supported for GPT image models:

  * `png` (default)
  * `jpeg`
  * `webp`
</ParamField>

<ParamField path="output_compression" type="integer">
  The compression level (0-100%) for the generated images. Only supported for GPT image models with `webp` or `jpeg` output formats. Defaults to 100.
</ParamField>

<ParamField path="stream" type="boolean">
  Edit the image in streaming mode. Defaults to `false`. See the [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
</ParamField>

<ParamField path="partial_images" type="integer">
  The number of partial images to generate. Used for streaming responses. Value must be between 0 and 3. When set to 0, the response will be a single image sent in one streaming event.
</ParamField>

<ParamField path="user" type="string">
  A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
</ParamField>

## Returns

Returns an `ImagesResponse` object containing the edited images.

<ResponseField name="created" type="integer">
  The Unix timestamp (in seconds) of when the image was created.
</ResponseField>

<ResponseField name="data" type="array">
  The list of generated images.

  <Expandable title="Image object">
    <ResponseField name="url" type="string">
      The URL of the generated image (for `dall-e-2` with `response_format=url`).
    </ResponseField>

    <ResponseField name="b64_json" type="string">
      The base64-encoded JSON of the generated image (for GPT image models or `dall-e-2` with `response_format=b64_json`).
    </ResponseField>
  </Expandable>
</ResponseField>

## Examples

### Edit with mask (DALL-E 2)

```python theme={null}
from openai import OpenAI
client = OpenAI()

response = client.images.edit(
    model="dall-e-2",
    image=open("sunlit_lounge.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="A sunlit indoor lounge area with a pool containing a flamingo",
    n=1,
    size="1024x1024"
)

image_url = response.data[0].url
print(f"Edited image URL: {image_url}")
```

### Edit without mask (GPT Image model)

```python theme={null}
import base64
from openai import OpenAI

client = OpenAI()

response = client.images.edit(
    model="gpt-image-1.5",
    image=open("original.png", "rb"),
    prompt="Make the image look like it was taken at sunset",
    quality="high",
    size="1024x1024"
)

# GPT image models always return base64
image_data = base64.b64decode(response.data[0].b64_json)
with open("edited.png", "wb") as f:
    f.write(image_data)
```

### Edit multiple images

```python theme={null}
from openai import OpenAI
client = OpenAI()

response = client.images.edit(
    model="gpt-image-1",
    image=[
        open("image1.png", "rb"),
        open("image2.png", "rb"),
        open("image3.png", "rb")
    ],
    prompt="Apply a vintage filter to these images",
    input_fidelity="high"
)

import base64
for i, image in enumerate(response.data):
    image_data = base64.b64decode(image.b64_json)
    with open(f"edited_{i}.png", "wb") as f:
        f.write(image_data)
```

### Edit with transparent background

```python theme={null}
import base64
from openai import OpenAI

client = OpenAI()

response = client.images.edit(
    model="gpt-image-1.5",
    image=open("product.png", "rb"),
    prompt="Remove the background and keep only the product",
    background="transparent",
    output_format="png"
)

image_data = base64.b64decode(response.data[0].b64_json)
with open("product_no_bg.png", "wb") as f:
    f.write(image_data)
```
