Overview
The Image Edit API allows you to edit specific parts of an image by providing the original image, a mask, and a text prompt. This functionality is particularly suitable for:- 🎨 Local Modifications: Modify only specific regions of the image
- 🔄 Content Replacement: Replace certain elements in the image
- ✨ Creative Editing: Add new elements while maintaining overall style
- 🖼️ Image Restoration: Repair or improve specific parts of the image
How the API Works
Image editing requires three key elements:- Original Image: The base image to be edited
- Mask Image: Specifies the regions to edit (transparent areas will be edited)
- Prompt: Describes what you want to generate in the edit area
Transparent areas (alpha channel 0) in the mask image will be identified as parts needing editing. Fully opaque areas will remain unchanged.
Quick Start
Basic Configuration
Basic Edit Example
API Parameters
Request Parameters
Response Format
Creating Mask Images
Using Python PIL to Create Masks
Using Circular Mask
Complete Usage Example
Python Complete Example
Node.js Example
Practical Application Scenarios
1. Background Replacement
2. Object Removal
3. Clothing Change
4. Image Restoration
Advanced Techniques
1. Feathered Mask
Create mask with feathered edges for more natural edits:2. Multi-Region Editing
Edit multiple regions simultaneously:3. Smart Prompt Construction
Best Practices
1. Image Preparation
- Format Requirements: Supports PNG, WebP, and JPG formats
- File Size: Less than 50MB per image
- Batch Processing: gpt-image-1 supports up to 16 images
- Resolution Recommendation: Use 1024x1024 for best results
2. Mask Design
- Precise Mask: More precise mask yields better edit results
- Edge Treatment: Consider using feathered edges for more natural edits
- Region Size: Avoid overly large edit regions which may affect results
3. Prompt Optimization
4. Error Handling
FAQ
Q: Why doesn’t the edit result match expectations?
A: Check if mask is correct, ensure transparent areas accurately cover parts to edit. Also optimize prompt with more specific descriptions.Q: What image formats are supported?
A: Supports PNG, WebP, and JPG formats, less than 50MB per image. gpt-image-1 supports up to 16 images.Q: How to achieve more natural edit results?
A: Use feathered masks, emphasize “natural blending” and “matching surrounding environment” in prompts.Q: Can multiple regions be edited simultaneously?
A: Yes, simply mark multiple transparent areas in the mask.Q: What are the limitations for editing large images?
A: Each image file must be less than 50MB. gpt-image-1 supports batch processing of up to 16 images.Related Resources
- OpenAI Official API Documentation:
platform.openai.com/docs/api-reference/images/createEdit - OpenAI Image Edit Guide:
platform.openai.com/docs/guides/images/edits - PIL/Pillow Documentation:
pillow.readthedocs.io - APIYI Console
The Image Edit API is particularly suitable for scenarios requiring precise control of edit areas. With well-designed masks and optimized prompts, professional-grade image editing results can be achieved.