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.