How to beginner · 3 min read

How to edit image with OpenAI DALL-E

Quick answer
Use the OpenAI Python SDK's client.images.edits.create method with your original image, an optional mask image, and a prompt describing the edit. This sends a request to the DALL-E image editing endpoint to generate the edited image.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • An original image file and optional mask image file

Setup

Install the OpenAI Python SDK and set your API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key_here' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key_here" (Windows).
bash
pip install openai

Step by step

This example edits an image by providing the original image, a mask image to specify the editable area, and a prompt describing the desired change.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Paths to your images
original_image_path = "./original.png"
mask_image_path = "./mask.png"

response = client.images.edits.create(
    model="dall-e-3",
    image=open(original_image_path, "rb"),
    mask=open(mask_image_path, "rb"),
    prompt="Add a red hat to the person",
    n=1,
    size="1024x1024"
)

edited_image_url = response.data[0].url
print("Edited image URL:", edited_image_url)
output
Edited image URL: https://openai-generated-image-url.com/edited-image.png

Common variations

  • Change model to other DALL-E versions if available.
  • Adjust size to "256x256" or "512x512" for smaller images.
  • Use asynchronous calls with asyncio for non-blocking requests.
  • Generate multiple edits by increasing n.
python
import os
import asyncio
from openai import OpenAI

async def edit_image_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.images.edits.acreate(
        model="dall-e-3",
        image=open("./original.png", "rb"),
        mask=open("./mask.png", "rb"),
        prompt="Add sunglasses",
        n=2,
        size="512x512"
    )
    for i, data in enumerate(response.data):
        print(f"Edited image {i+1} URL:", data.url)

asyncio.run(edit_image_async())
output
Edited image 1 URL: https://openai-generated-image-url.com/edited1.png
Edited image 2 URL: https://openai-generated-image-url.com/edited2.png

Troubleshooting

  • If you get a FileNotFoundError, verify your image paths are correct.
  • If the API returns an authentication error, check your OPENAI_API_KEY environment variable.
  • For InvalidRequestError, ensure your images meet size and format requirements (PNG, JPG).
  • Network errors may require retry logic or checking your internet connection.

Key Takeaways

  • Use client.images.edits.create with original image, mask, and prompt to edit images with DALL-E.
  • Always provide a mask image to specify editable regions for precise edits.
  • Use asynchronous calls for efficient image editing in concurrent applications.
  • Check environment variables and file paths to avoid common errors.
  • Adjust model, size, and n parameters to customize output.
Verified 2026-04 · dall-e-3
Verify ↗