# Save the image to a file
with open("composition.png", "wb") as f:
    f.write(image_bytes)
```

```javascript
import fs from "fs";
import OpenAI, { toFile } from "openai";

const client = new OpenAI();

const rsp = await client.images.edit({
  model: "gpt-image-1",
  image: await toFile(fs.createReadStream("sunlit_lounge.png"), null, {
    type: "image/png",
  }),
  mask: await toFile(fs.createReadStream("mask.png"), null, {
    type: "image/png",
  }),
  prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
});

// Save the image to a file
const image_base64 = rsp.data[0].b64_json;
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("lounge.png", image_bytes);
```

```bash
curl -s -D >(grep -i x-request-id >&2) \
  -o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \
  -X POST "https://api.openai.com/v1/images/edits" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F "model=gpt-image-1" \
  -F "mask=@mask.png" \
  -F "image[]=@sunlit_lounge.png" \
  -F 'prompt=A sunlit indoor lounge area with a pool containing a flamingo'
```

| Image                                                                                | Mask                                                                           | Output                                                                                                                              |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| ![A pink room with a pool](https://cdn.openai.com/API/docs/images/sunlit_lounge.png) | ![A mask in part of the pool](https://cdn.openai.com/API/docs/images/mask.png) | ![The original pool with an inflatable flamigo replacing the mask](https://cdn.openai.com/API/docs/images/sunlit_lounge_result.png) |

Prompt: a sunlit indoor lounge area with a pool containing a flamingo

#### Mask requirements

The image to edit and mask must be of the same format and size (less than 50MB
in size).

The mask image must also contain an alpha channel. If you're using an image
editing tool to create the mask, make sure to save the mask with an alpha
channel.

Add an alpha channel to a black and white mask

You can modify a black and white image programmatically to add an alpha channel.

```python
from PIL import Image
from io import BytesIO