Prerequisites
- Python 3.7+
- The
openaiPython package (pip install openai) - An OpenAI API key
Make sure you’ve exported your API key as an environment variable:
1. Install and Authenticate
Begin by installing the client library and initializing your API key in Python:2. Prepare Your Images
You need three files:- Original image (with the object you want to replace)
- Base image (same image after removing the object)
- Mask image (a transparent PNG that marks the area to edit)
3. Perform the Edit
Use thecreate_edit endpoint to fill the masked area based on your prompt:
4. Generate Multiple Variations
If you’d like several options, increasen and iterate through the results:
Requesting a large number of edits or very high resolutions may incur higher usage costs. Monitor your API usage dashboard.
5. Parameter Reference
| Parameter | Type | Description |
|---|---|---|
image | File | The base image without the masked object (PNG or JPEG). |
mask | File | A transparent PNG marking the area to edit (white = mask). |
prompt | String | Textual description of what should appear in the masked area. |
n | Integer | Number of edits to generate (1–10). |
size | String | Output resolution: 256x256, 512x512, or 1024x1024. |