Remove Object from Photo
Operator: Abdulla Al Fahad. How tools are built and checked
Nothing is invented here: every pixel of the fill is copied from somewhere else in the same photo, which is why it works so well on grass, sky, sand, water and walls, and so poorly on a face. Brush over a stray signpost, wire or person and the page fills the gap with patches found elsewhere in the picture, refined over a pyramid of up to 8 levels, each half the size of the one before, that runs from the working copy down to a small thumbnail. Because the fill is only a suggestion, it is shown as a preview. You can keep it, ask for another arrangement of patches, or discard it, and a kept result becomes the picture you mark next, so several objects can come out one after another with undo and redo throughout.
Key features
- A round brush from 6 to 240 px wide, an eraser for the mask, and the mask grown by 0 to 12 px to take in soft edges and shadows
- Multi-scale PatchMatch inpainting with 5, 7 or 9 px patches and 3, 5 or 8 rounds per level
- Runs in a background worker with a progress bar and a Cancel button, so the page stays responsive
- A preview of each fill with Keep, Try another variation and Discard
- Undo and redo of every brush stroke and every kept fill, up to 15 steps, also by Ctrl or Cmd+Z
- PNG or JPG at the picture's own size, with everything outside the filled area untouched
How the fill is built, and where it fails
A picture over 4096 px on the long side is scaled down when it is opened, and an animated GIF is read as its first frame. The fill itself is worked out on a copy of at most 1600 px on the long side, since patch search time grows quickly with the number of pixels, and is then placed on the full-size picture. The filled area therefore has the detail of the 1600 px copy, while everything outside it keeps the full resolution. A picture already under 1600 px is filled at its own size. If the marked area covers more than 60 percent of the picture, or leaves less than 20 unmarked patches to copy from, the page says so and does nothing.
The mask you paint is grown by the chosen number of pixels in a diamond-shaped step repeated that many times. Source patches must lie wholly outside the grown mask, with one more pixel of margin, so a copied patch never contains part of the object. Distance between patches is the sum of squared red, green and blue differences, with the patch drawn from the current estimate for any pixels still inside the hole. The Quick, Balanced and Thorough settings only change the patch width and the number of rounds; they do not change the working resolution.
How it works
PatchMatch finds, for every patch that overlaps the hole, the most similar patch from the unmarked area, without comparing against all of them. Each candidate position is improved in two ways: by trying the match of the neighbour just visited, shifted by one pixel, since neighbouring patches usually match neighbouring sources, and by random probes around the current best at radii that halve from the picture size down to one pixel. Sweeps alternate between forward and backward scan order. After each search, every marked pixel is set to a weighted average of the colours that the patches covering it propose, with weights falling off as a patch's match gets worse.
This search and vote loop runs on a pyramid built by halving the picture and the mask; the coarsest level, which still has at least 40 patches to copy from, is first filled from the edges inward with the mean of known neighbours, then refined, and each finer level starts from the enlarged result and the enlarged match field. Working coarse to fine is what lets large structure, such as the direction of a fence or a horizon, carry through before fine texture is fitted. Marked pixels are the only ones ever changed.
Common use cases
- A beach photo with a person walking in the distance: mark a 60 x 120 px figure on sand and sea, grow by 4 px to take in the shadow, and the fill borrows sand and surf from either side.
- A wall photo with a green sign in the middle of rendered plaster: the plaster's grain is copied in, which is why Thorough, with 9 px patches, gives a more even texture than Quick.
- A product shot on a white background with dust specks: brush each speck with a 3 to 6 px brush, grow the mask by 2, choose Quick, and every speck is replaced by white copied from beside it.
- A sky with a power line running across it: paint along the wire in several strokes, grow by 3 px, and the gradient and clouds either side are repeated over it.
How to use this tool
- Open a photo and paint over the object with the brush, switching to Erase mask to trim anything you covered by mistake. Then press Fill the marked area and wait for the progress bar, or Cancel it.
- Check the preview, press Keep this result or Try another variation, then download PNG or JPG.
Example
A 4000 x 3000 photo, a signpost marked at 300 x 200 px on the working copy, Balanced, grow 3 px
working copy: 1600 x 1200 px (scale 0.4)
marked area: 300 x 200 px, grown by 3 px
quality: Balanced (7 px patches, 5 rounds per level)What happens
7 levels, from 1600 x 1200 down to 25 x 19 px; 40 search-and-vote rounds in all (10 at the coarsest level)
the fill covers about 750 x 500 px of the 4000 x 3000 picture
timing: a Balanced fill of a 300 x 200 px area on a 1600 x 1200 px textured picture took about 1.0 s in Node on an Apple M4 (Quick 0.7 s, Thorough 1.8 s); a phone or a larger area takes longerCommon pitfalls
Patch copying has no idea what was behind the object. Where a straight edge, a face, text or a change of material runs through the marked area, the fill will bend it, repeat it or smear it, and a hole that is wide compared with the repeating features around it will show visible copies. Smaller holes in repetitive texture are fine; removing a person standing in front of a building is not. For those, expect to touch up in an editor afterwards.
Because every pixel of the fill is copied from the same photo, the telltale of a poor fill is repetition: the same tuft of grass or the same cloud edge twice. Look at the result at 100 percent before you keep it. A clean-looking fill also proves nothing about what was there, so label an edited photo as edited where that matters.
- Mark a little more than the object. A 3 to 6 px margin covers the soft edge and the shadow, and leaving a fringe of the object is the commonest reason a fill looks like a smudge.
Common errors and how to fix them
- Nothing is marked
- The mask is empty, so there is nothing to fill. Paint over the object, and check that you are on Paint and not Erase mask.
- The marked area covers more than 60 percent of the picture
- Too little of the picture is left to copy from. Mark only the object, or crop the picture to the area around it first.
- This browser could not start the background worker
- Module workers are not available, for example in an old browser or a restricted embedded view. Open the page in a current browser.