Topic 2 of 6 · 4 min read
Editing images with masks and reference images
Goal: Write an edit request with the right mask and reference images so only the intended region of a product photo changes.
An edit request has three jobs: say which pixels may change (the mask), say what to change them into (the instruction), and say what must stay the same (the reference images and the constraints).
Check yourself · 1 of 3
You want a new background behind a product and the product left alone. What should the mask cover?
Why it matters
Product teams rarely need a new picture. They need the same bottle on a beach instead of a white table, or the same jacket in another colour. If the model redraws the whole image, the logo warps, the label text changes and the face of a model becomes someone else. Editing APIs exist to limit the change.
How to do it
- Pick the edit style. Inpainting takes the source image, a mask and a prompt; only the masked area is regenerated. Conversational editing, as in Gemini image models and the OpenAI image endpoints, takes the source and an instruction in plain words, and the model decides what to touch.
- Make the mask match the intent. Cover the background, not the product. Add a small margin so edges blend. Check how the provider reads the mask: some treat transparent pixels as the area to edit, others use a separate mask image, so read the docs and test with one image.
- Pass reference images for identity. To keep a product, character or brand look the same across shots, send the original photo or a few angles as references and say so: "Keep the bottle and its label exactly as in the reference image."
- State what must not change. Name faces, logos and printed text in the instruction. Text and small logos are where models fail most.
- Edit in small steps and compare. One change per request. Keep the original; every edit is a new file, never an overwrite.
- Check the result. Compare the protected region of the output with the original, by eye for a first version and by a pixel difference or a logo-matching check at scale.
Before and after
Before: "Make this photo look better and put it outside." with the full image, no mask.
The model may restyle the bottle, redraw the label and change the lighting on the product.
After: Source photo of the bottle, a mask covering only the white table and wall, and the instruction: "Replace the masked background with a sunny beach at golden hour. Keep the bottle, its label text and its shadow shape unchanged."
What goes wrong
- The mask is the wrong way round, so the product is repainted and the background stays.
- A mask with a hard edge leaves a visible halo around the product.
- The label text comes back with new, slightly wrong letters, and nobody checks.
- Several changes in one request: colour, background and pose. One of them is silently dropped.
Examples
Background swap on a product shot
Source: bottle.png. Mask: white table and wall selected, bottle excluded, with a few pixels of margin.
Instruction: "Replace the masked area with a wooden kitchen counter in morning light. Do not change the bottle, the label or the cap."
Same character, new scene
Send three reference images of a mascot and ask: "Draw this mascot waving at a train station. Match the colours, outline weight and proportions of the references." Then compare against the references before publishing.
Key points
- An edit request defines what may change (mask), what to change it into, and what must stay.
- Inpainting regenerates only the masked area; conversational edits leave the choice of area to the model.
- Check how each provider reads the mask before relying on it; test with one image.
- Reference images and explicit text about logos, faces and printed text help keep identity stable.
- One change per request, original kept, protected region compared with the output.
Common mistakes
- Sending an edit with no mask and expecting only the named object to change.
- Masking the product instead of the background, or inverting the mask the provider expects.
- Asking for several changes in one request and not checking which ones were applied.
- Trusting regenerated text or logos without comparing them with the original.
Check yourself · 2 of 3
After an edit, the label on a bottle reads "ORGANlC" instead of "ORGANIC". What is the most reliable fix?
Check yourself · 3 of 3
Why send reference images when asking for the same mascot in a new scene?
Practice items, not counted towards your mastery.
Start here
- Image generation guide (OpenAI) (opens in a new tab)DocsFree
Size, quality, edit and mask parameters for the OpenAI image endpoints.
- Image generation with Gemini (Google) (opens in a new tab)DocsFree
Gemini's text-to-image and conversational editing, including reference images and its watermark note.
- FLUX documentation (Black Forest Labs) (opens in a new tab)DocsFree
How the FLUX API submits a job and returns a polling address instead of the image.
Where to go in your country
- Article 50: Transparency obligations (EU AI Act) (opens in a new tab)DocsFreeEU
Marking duty for providers of generated content and disclosure duty for deepfakes.