Mughees Ul Haq

Work 10 / 18

Removing objects from photos without leaving a patch

You brush over an object and a hosted generative model removes it. The first version left a lighter, flatter rectangle around every edit. I changed what is sent to the model and how the result is blended back, so the edit matches the photo around it.

For
A property-media platform
Status
Version 1.1.0
Pixels outside the mask
unchanged
Byte-identical, checked in tests
Model input limit
4 MP
Set by the hosted API
Seam check
ΔE per edit
Logged for every request
Weights
none
A hosted FLUX API

The problem

The API accepts at most 4 megapixels, so the earlier version shrank the whole photo, sent it, and pasted the result back with a 6-pixel feather. The edited area came back softer and lighter than the rest, and showed as a rectangle.

What I changed

  1. Brush maskGrown a little, then cropped
  2. Full-size cropSnapped to 16 px
  3. Hosted modelPNG in, PNG out
  4. Colour matchLAB mean and spread from a ring outside the mask
  5. Seamless cloneNormal mode, never mixed
One removal in version 1.1.0. Every pixel outside the mask comes back exactly as it went in.
  • Send only a crop around the brushed area, at full resolution, padded and snapped to a multiple of 16. Only a crop that is still over the limit gets shrunk, and only the crop.
  • Match colour in LAB space: the mean and the spread of the result are matched to a ring of pixels just outside the mask. Matching the spread is what brings the grain back.
  • Blend with OpenCV seamless cloning in normal mode. Mixed mode lets the removed object show through, so it is never used for removal.
  • Send PNG both ways and leave every pixel outside the mask byte-identical. Tests check both, and a test checks that the colour match beats a plain RGB blend.
  • Log the colour difference (ΔE) on the seam for every request, so a bad edit can be found afterwards.

Smaller fixes

An earlier release failed with a 422 error on some phone photos: the API turned the image upright using its EXIF orientation, but the mask had been sized from the raw pixels. Switching to asynchronous polling ended the timeouts on slow edits.

Limits

Only removal by brush gets the full-resolution path; removal by prompt and other edits still use the old one. It depends on a third-party API that charges per edit, and there is no benchmark beyond the seam log.

Tools

FLUX (hosted API), OpenCV, Python, pytest.

The full write-up

There is a longer write-up of this project. It has client details in it, so email me if you’d like a redacted copy.