Swapping the background is not recognition — it is a chain of pixel arithmetic
Swap a blue background for a white one and a light shirt fades toward white while the hairline keeps a gray ring. None of this is mysterious: every pixel on the page walks the same arithmetic chain — sample a ring along the four edges and take its median as the old background color, crop off one axis to the target ratio, then scale everything, compute a weighted color distance per pixel to build a mask, feather it with a box blur, and finally lay the base color in the chosen format. The default preset turns on just three numbers: tolerance 52, a red-shift mask of 0.7296 against a green-shift 0.2458, and feather level 6 with a transition band 13 pixels wide. The rest of this article settles each stage of that chain, account by account.
Weighted color distance: the red channel's 0.9 gives way to skin tone
The mask does not look at the plain Euclidean distance; it looks at sqrt(0.9dr²+1.2dg²+0.9db²). A probe constructed three pixels whose Euclidean distance to the old background is all 60: when the shift lands entirely on the red or the blue channel, the weighted distance is 56.921, the mask reads 0.7296, and most of the area gets replaced; when the shift lands entirely on the green channel, the weighted distance is 65.727 and only 0.2458 of the mask remains, so almost nothing changes. Red and blue carry equal weight and measure bit-for-bit identical results. The green channel's weight of 1.2 keeps green-shifted pixels outside the replacement zone, and skin leans red with its distance pressed down by 0.9, which is exactly what saves it from being replaced wholesale. The price is symmetric: pale clothing with a cyan-gray cast already sits close to the background, so it gets swept up easily.
Three accounts under tolerance 52
The tolerance slider spans 5~140 with a default of 52, and the mask splits the weighted color distance into three zones: inside 52 replace fully, from 52 to 70.2 decay linearly, beyond that change nothing. The decay width is 18.2, equal to tol×0.35, and the two extreme presets scale at the same rate: at tol=5 the band is only 1.75 wide, at tol=140 it is 49. The shirt color (210,220,235) against white already falls inside the decay band on the default preset, and the measured mask is 0.5365 — the quantified version of the page footer's warning that "too large will replace light clothing as well" reads: no need to raise the tolerance; the default preset has already replaced half of a light shirt.
Figure 1 | The axis is the weighted color distance: full swap inside 52, linear decay from 52 to 70.2, and the 18.2 width rescales together with the tolerance.
Where the background color comes from: a median over the 2% edge band
Automatic sampling scans only a 2% wide band along the four edges, stepping horizontally every w/40 pixels, sorting the samples by the sum R+G+B and taking the floor(n/2)-th one. A 295×413 source yields 1020 sample points and the median slot sits at 510; measured, repainting the entire bottom band dark — 258 of the points — still cannot flip the median, and the output stays [255,255,255]. The failure condition is stricter: the dark pixels must fill the bottom band plus both side bands at once, around 74.7%, before the median is dragged into a wrong background color — exactly the picture of dark clothing pressed against three edges. Sampling also runs only once at load time, so changing the tolerance never re-samples the page.
Crop coordinates: crop to the target ratio first, then scale overall
This page compares the target aspect ratio against the source: a wider source crops left and right, a taller one crops top and bottom, and the vertical landing point follows the offset preset, default 0.33. Three coordinate accounts were measured end to end:
- 1000×700 to a one-inch 295×413: crop left and right, source width 500, cropX=250, scale 0.59 fills the frame proportionally;
- 800×1200 to the same preset: crop top and bottom, keep height 1120 and cut away 80, the upper offset gives srcY=round(80×0.33)=26, the centered offset lands at 40;
- a small two-inch 413×531 (ratio 0.777778) against the same 800×1200 source keeps height 1029 with srcY=56.
All five output canvases — 295×413, 413×579, 413×531, 600×800 and the original-size preset — were verified against the browser. When what needs to change is the overall pixel amount rather than the crop position, that belongs to a different measurement path, the image resize to target dimensions tool, and this article does not repeat its conclusions.
Feather level 6: a 13-pixel transition run
Edge feathering runs two box blurs over the mask with window width 2r+1. At feather=0 the boundary is a hard jump 1 pixel wide; at feather=2 the transition spans 4 pixels; at feather=6 the measured run walks from 195,200,205,210,215,220 all the way down to 88,73,58,44,29,15, with a window 13 pixels wide. That gray ring along the hairline is simply the unfinished transition: leftover pixels stranded between the old background and the target color. The parameters can only move the width around; they cannot erase it. A single impulse blurred with r=2 spreads across 5 cells of 0.2 each — the mass is conserved, so a constant region that was fully replaced never gets pulled down.
Figure 2 | Transition widths across the three feather levels: level 0 is a hard boundary, level 6 measures 13 pixels end to end.
JPG underlay and PNG direct paste: one pixel, two fates
Color replacement touches RGB only and never alpha. PNG pastes the result straight onto the canvas; JPG first floods the whole frame with the target background color and then composites the picture on top, so translucent pixels get padded back by the underlay. Measured side by side: a fully transparent pixel shows alpha=0 through to the checkerboard on the PNG side but comes out as a clean background color on the JPG side; a translucent dark gray at alpha≈102 (mask 0, never supposed to change) is kept verbatim by PNG while JPG composites it into [126,12,22]; a translucent white column still carries alpha 100 on the PNG side, but one round-trip through 8-bit premultiplied storage drifts its RGB by 1. The byte account: a near-solid 295×413 output at JPG q=0.95 gives 2023 characters, roughly 1500 bytes; PNG gives 5490 characters, roughly 4101 bytes; on this machine the same picture's JPEG runs about 36% of the PNG size, and the PNG encoder ignores the quality parameter entirely. The whole chain has no network exit — the file never leaves this machine.
Two silent pitfalls
The background parser accepts only a full 6-digit hex string: the #fff three-digit shorthand, 5/7 digit lengths, leading spaces and empty strings all silently resolve to [0,0,0], and there is no error branch to catch them; the preset buttons and the color picker both emit 7 character values on their normal paths, so they never hit it. The other measured pit: custom picking does not refresh the "current:" label — set #123456 directly and the label stays on its initial text; only the preset buttons sync the label and write the value back into the picker.
Common questions
Q: Why did the swap eat my light shirt too?
The shirt color (210,220,235) sits inside the 52 to 70.2 decay band against white, so its weighted distance produces a mask of 0.5365 — half replaced. Lowering the tolerance narrows the band in sync at tol×0.35; the geometry of the decay zone does not change, only its width.
Q: Blue to white leaves a gray fringe at the hairline — why?
Two sources stack up: transition pixels inside the decay band where the mask is not 0, and the feather's spread transition run (feather=6 measures 13 pixels). This page only applies a solid-color distance threshold and does not recognize the subject, so the edge is a trade-off between these two parameters. On the ID photo background changer page, every notch of tolerance or feather recomputes the entire mask.
Q: JPG or PNG for export?
Follow the translucent pixels: PNG keeps alpha and lets transparent areas through as they are, JPG lays the base color first and pads translucent regions into solid color. On a near-solid 295×413 picture, JPG q=0.95 measured about 1500 bytes while PNG came in at about 4101 bytes.
Q: Any other route for fine-tuning the final size?
This page only handles cropping, background swapping and exporting. To change the pixel size itself, see the notes on resizing images online; this article deliberately makes no cross-tool comparison of swap quality.