Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai python Images Edit

From Leeroopedia
Knowledge Sources
Domains Computer_Vision, Image_Editing
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for editing images using text prompts and optional masks provided by the OpenAI Python SDK.

Description

The Images.edit() method modifies source images based on text descriptions. It supports single or multiple input images, optional transparency masks (DALL-E 2), configurable fidelity levels, and streaming edit previews.

Usage

Call client.images.edit() with source image(s) and an edit prompt.

Code Reference

Source Location

  • Repository: openai-python
  • File: src/openai/resources/images.py
  • Lines: L460-522 (sync impl), L1350-1412 (async impl)

Signature

class Images(SyncAPIResource):
    def edit(
        self,
        *,
        image: Union[FileTypes, List[FileTypes]],
        prompt: str,
        mask: FileTypes | NotGiven = NOT_GIVEN,
        model: Union[str, ImageModel, None] | NotGiven = NOT_GIVEN,
        n: Optional[int] | NotGiven = NOT_GIVEN,
        size: Optional[str] | NotGiven = NOT_GIVEN,
        quality: Optional[str] | NotGiven = NOT_GIVEN,
        background: Optional[Literal["transparent", "opaque", "auto"]] | NotGiven = NOT_GIVEN,
        input_fidelity: Optional[Literal["high", "low"]] | NotGiven = NOT_GIVEN,
        stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
        user: str | NotGiven = NOT_GIVEN,
    ) -> ImagesResponse | Stream[ImageEditStreamEvent]:
        """
        Creates an edited image given a source image and prompt.
        """

Import

from openai import OpenAI
# Access via client.images.edit()

I/O Contract

Inputs

Name Type Required Description
image FileTypes or list[FileTypes] Yes Source image(s) (PNG, max 25MB)
prompt str Yes Edit description
mask FileTypes No Transparency mask (DALL-E 2 only)
model str No Model selection
input_fidelity str No "high" or "low" source fidelity
stream bool No Enable streaming edit previews

Outputs

Name Type Description
response ImagesResponse Edited image(s) with URL or base64
stream Stream[ImageEditStreamEvent] Streaming edit events

Usage Examples

Basic Edit

from openai import OpenAI

client = OpenAI()
response = client.images.edit(
    model="gpt-image-1",
    image=open("photo.png", "rb"),
    prompt="Add a hat to the person in the image",
)
print(response.data[0].url)

With Mask (DALL-E 2)

response = client.images.edit(
    model="dall-e-2",
    image=open("room.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="A sunlit indoor lounge area with a pool",
    size="512x512",
)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment