Hi everyone,

I’m currently experimenting with the OpenAI Images API and GPT Image 2, specifically the image editing/inpainting functionality using masks.

My goal is to build an application where a user can select a specific area of an already generated image and request a localized modification.

For example:

  • The user selects one or more cushions in an interior design image.
  • The user asks: “Change the orange cushions to green.”
  • Only the selected cushions should be modified.
  • Everything outside the selection should remain unchanged.
  • Ideally, the selected object itself should also remain as intact as possible, with only the requested property (e.g. color/texture) being changed.

I’m currently testing this using the images.edit API with a PNG mask containing an alpha channel.

My basic implementation looks like this:

from openai import OpenAI
import base64
import os

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

result = client.images.edit(
    model="gpt-image-2",
    image=open("base.png", "rb"),
    mask=open("mask-api.png", "rb"),
    prompt="Change the texture of the orange cushions to green."
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("composition-image.png", "wb") as f:
    f.write(image_bytes)

The mask has the same dimensions as the original image and uses transparency to indicate the editable area.

However, I’m getting some unexpected results.

Test results

In one of my tests, I selected a specific region containing the orange cushions.

The expected behavior was:

Only the orange cushions inside the selected/masked area should be changed to green, while everything else should remain exactly as close as possible to the original image.

Instead, the generated image sometimes:

  1. Changes parts of the image outside the masked area.
  2. Changes objects that were not part of the requested edit.
  3. Leaves parts of the masked area transparent/empty instead of preserving the original content.
  4. Re-generates more of the selected region than necessary, even though I only requested a color/texture change.

I’ve also tried making the prompt more explicit, for example:

Only modify the orange cushions inside the editable area.

Change their fabric to green.

Do not modify any other object.
Keep the sofa, table, floor, walls, lighting, perspective, composition,
shadows and all other elements unchanged.

However, I still see changes outside the intended area.

base.png:

mask-api.png (I want to edit only de the selected tranparent area):

composition-image-2.png (result):