How background removal works without AI: colour distance and flood fill explained

How a simple colour method finds and clears a plain background, what the tolerance number measures, why a soft edge helps, and when to touch up by hand.

Open the Background remover Free, in your browser, nothing uploaded.

Taking the background off a photo sounds like a job for artificial intelligence, but when the background is plain (a white sheet, a grey wall, a sheet of paper) a few lines of arithmetic do it well and run instantly on any phone. This article explains that arithmetic: how the background colour is found, what “tolerance” means as a number, and why the method stops at the edge of your subject. It is for sellers photographing products, students cleaning up diagrams, and anyone tidying a logo or signature. For official photo sizes afterwards, see passport photo; for a signature, signature maker can draw a clean one from scratch; and to shrink the result, use image resizer.

What it is

Every pixel has three numbers, red, green and blue, each from 0 to 255. Two colours are close when these numbers are close. The method measures that with ordinary straight-line distance:

distance = √((R₁ − R₂)² + (G₁ − G₂)² + (B₁ − B₂)²)

The largest possible distance, from black (0, 0, 0) to white (255, 255, 255), is about 441. Tolerance is a percentage of that: tolerance 20 means “remove pixels within 0.20 × 441 ≈ 88 colour steps of the background”.

Three ideas make it work:

  • Background colour from the edges. Every pixel along the four borders is collected, and the middle value (the median) of each channel is taken. A median ignores a few odd pixels, such as a corner of the subject touching the edge.
  • Flood fill from the edges. Clearing starts at the border and spreads only to neighbouring pixels (left, right, up, down) that are close enough. So a white label on a red mug is safe even though it is white: the fill can only reach it by crossing the red.
  • Soft edge. The keep-or-remove map is blurred a little, so the cut fades over a few pixels instead of stepping, which hides jagged edges.

Your own brush marks are applied last: Keep always keeps and Erase always removes, whatever the colour says.

How to use it

  1. Open the background remover and choose a photo; very large ones are scaled to 2,400 pixels on the long side.
  2. Check the background colour it found. If the edges are uneven, press Pick colour and tap a typical background spot.
  3. Move Tolerance up until the background clears but the subject stays whole.
  4. Add a little Soft edge to smooth the outline.
  5. Paint with the Keep brush or Erase brush to fix what the colours got wrong; Clear marks starts the hand fixes again.
  6. Under Put behind it, choose see-through, white or a colour, then download the PNG.

A worked example

We made a 200 × 150 pixel test photo: a light grey background (235, 235, 235), a red mug (180, 40, 40) of 60 × 80 px, a white label (250, 250, 250) on the mug, and a grey shadow (205, 205, 205) under it. That is 30,000 pixels in all.

  • The edges are all background, so the median edge colour is (235, 235, 235).
  • Shadow distance: √(3 × 30²) ≈ 52, which is 52 ÷ 4.41 ≈ 11.8%.
  • Mug distance: √(55² + 195² + 195²) ≈ 281, or 63.8%.
  • Label distance: √(3 × 15²) ≈ 26, only 5.9%, yet it sits inside the mug.

What happens as tolerance rises:

  • Tolerance 5 or 10: 23,800 pixels cleared (79.3%). The shadow stays.
  • Tolerance 12 to 63: 25,200 cleared (84.0%): background and shadow gone, mug and label kept. This is the safe range.
  • Tolerance 64 or more: all 30,000 cleared. The mug now counts as “close”, and once the fill crosses it, the label goes too.

With Soft edge at 2 px, the mug’s left edge fades over about six pixels (opacity 0, 18, 52, 102, 154, 204, 238, 255 out of 255), instead of jumping from clear to solid.

Checked with the tools

The test photo was built pixel by pixel in Node and passed through the tool’s own code (src/lib/image/bgremove.ts): edgeColour() returned (235, 235, 235), floodBackground() gave the cleared counts above at each tolerance, and makeAlpha() with a 2 px soft edge gave the fade values listed.

Where it is used

  • Online shops. Products shot on a white sweep, saved on pure white or see-through for listings.
  • Documents. Signatures, stamps and seals photographed on paper, turned into clean overlays.
  • Logos and diagrams. A logo saved on white becomes a see-through PNG for slides or a website.
  • Science and school. Specimens or apparatus shot on a plain card, cut out for a report.

Common mistakes

  • Busy backgrounds. Patterned walls or outdoor scenes have many colours; a single background colour cannot describe them. Reshoot on a plain sheet if you can.
  • Subject the same colour as the background. A white cup on a white table leaks. Lower the tolerance and paint the cup back with Keep.
  • Uneven light. A dark corner is far from the median colour and stays. Pick colour in that corner, or erase it by hand.
  • Saving as JPG. JPG cannot hold transparency. Keep the PNG, or choose a solid colour behind.

Questions people ask

Why not use an AI model? A model is a large download and guesses at shapes. For plain backgrounds, colour distance is faster, works offline and never invents edges.

Why does a halo remain around the subject? Edge pixels mix both colours. A slightly higher tolerance or soft edge usually clears it.

Does a shadow count as background? Only if its colour is within tolerance and it touches cleared pixels.

Sources

  • Fibub's own method: the background colour is the median of the edge pixels, pixels within a straight-line RGB colour distance are cleared by a flood fill from the edges, and the cut is softened with a repeated box blur. No machine-learning model is used.

If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.

Tools in this article

Read next