- Segmentation decides subject or background; matting adds soft alpha at hair and edges.
- Hair, glass, cast shadows and low contrast cause most failed cutouts. Route them to review.
- Ship AVIF with a WebP or PNG fallback. JPEG has no alpha channel.
- Rebuild shadows with drop-shadow() and test edges on both dark and light backgrounds.
Background removal used to mean an hour with the pen tool. Now a model does a first pass in seconds, and the work moves to what happens after: edges, file format, shadows and the page the cutout lands on. Under the hood it is image recognition at the pixel level. When a catalog needs thousands of cutouts with its own edge cases, it becomes a computer vision project of its own, the kind of work teams offering custom image recognition software development services build as a pipeline.
This guide is for the person who has to put those cutouts on a website. It covers what the model does, where it fails, which transparent format to ship, how to avoid halos, and the CSS that places a product on a textured background without making it look pasted on.
What does automatic background removal actually do?
Automatic background removal turns every pixel of a photo into a yes, no or partly answer to one question: does this pixel belong to the subject? The yes-or-no part is called segmentation. The partly part is called matting, and it is what makes edges look real.
A segmentation model outputs a mask. Each pixel gets a score between 0 and 1, and a threshold cuts it into subject and background. That works well for solid objects with clear outlines: a shoe, a bottle, a phone.
A matting model goes one step further. It predicts an alpha value for each edge pixel, so a strand of hair can be 30% subject and 70% background. Saved into the alpha channel of a PNG, WebP or AVIF file, those values let the new background show through the strand instead of a hard cut line.
| Step | What it produces | Good at | Weak at |
|---|---|---|---|
| Segmentation | Hard mask, subject or not | Solid products, clear outlines | Hair, fur, lace, motion blur |
| Matting | Soft alpha at the edges | Hair, fur, soft focus | Speed, very large images |
| Edge refinement | Cleaned border, less color spill | Halos from the old backdrop | Glass and reflections |
Many tools chain all three. Open-source models such as U²-Net, MODNet and BiRefNet are common building blocks, and libraries like rembg wrap them in a single command. In practice, the photo you feed in matters as much as the tool you pick.
Why do hair, glass and shadows break cutouts?
Cutouts fail where the photo itself does not say clearly what is subject and what is background. These are the usual suspects:
- Hair and fur. Thin strands mix with the backdrop color. A hard mask either eats them or leaves a fringe. Shoot against a backdrop that contrasts with the hair color.
- Glass and clear plastic. The background is visible through the object, so the model has to guess. Expect to fix bottles and glasses by hand or keep the original backdrop.
- Shadows. Most models treat a cast shadow as background and drop it. The product then floats. Plan to rebuild the shadow later (see the CSS section below).
- Low contrast. A white product on a white sweep gives the model almost nothing to work with. A mid-gray backdrop is easier to separate.
- Compression. Heavy JPEG artifacts around edges turn into jagged alpha. Remove the background from the highest-quality source you have, then compress the result.
None of this is a reason to avoid automation. It is a reason to sort photos first. Solid products go through the automatic path. Hair, glass and fine lace go to a review queue.
Which format should a transparent image ship in?
For most websites, ship transparent images as AVIF with a WebP or PNG fallback. JPEG has no alpha channel, so a cutout saved as JPEG gets flattened onto a solid color. Here is how the options compare:
| Format | Alpha | Typical use | Notes |
|---|---|---|---|
| PNG | Yes, lossless | Masters, logos, flat graphics | Largest files for photos |
| WebP lossless | Yes | Sharp graphics with transparency | Google reports about 26% smaller than PNG |
| WebP lossy + alpha | Yes | Product cutouts, photos | Color is lossy, alpha can stay lossless |
| AVIF | Yes | Product cutouts, large heroes | Usually the smallest; slower to encode |
| JPEG + mask | Via CSS mask | Very large photos | Two files, more CSS |
Keep a PNG or lossless master and generate the web versions from it. Never edit a lossy file and save it again. Each round adds artifacts at the edges, which is exactly where cutouts are fragile.
Two encoder commands cover most cases:
# WebP: lossy color, near-lossless alpha
cwebp -q 80 -alpha_q 100 product.png -o product.webp
# AVIF: separate quality for color and alpha
avifenc -q 60 --qalpha 90 product.png product.avifThen serve them with a fallback chain, so every browser gets the best file it can read:
<picture>
<source srcset="/img/product.avif" type="image/avif">
<source srcset="/img/product.webp" type="image/webp">
<img src="/img/product.png" width="800" height="800"
alt="Ceramic vase, front view" loading="lazy">
</picture>Set width and height on every cutout. Transparent images still reserve space, and missing sizes cause layout shift. The guide to fast background images covers the rest of the loading budget.
How do you avoid halos and fringes on a new background?
A halo is a thin line of the old backdrop color around the subject. It appears because edge pixels carry a mix of subject and backdrop color. When you place the cutout on a different background, that leftover color shows.
The fix happens before export:
- Decontaminate edge colors. Most editors and many removal libraries can replace the backdrop color in semi-transparent pixels with the nearby subject color. In Photoshop this is the Decontaminate Colors option in Select and Mask.
- Shrink the mask by one pixel on solid objects. A one-pixel choke removes most of the fringe and is invisible at web sizes.
- Test on both extremes. Place the cutout on near-black and on near-white. A halo that hides on one shows on the other. A dark site like this one makes white fringes from studio sweeps very visible.
One more trap: some export tools write garbage color into fully transparent pixels. It is harmless until an encoder or a resize filter blends it into the edge. If you see colored edges only after resizing, export with transparent pixels cleared, or use cwebp -exact only when you need those values preserved on purpose.
How do you place a cutout on a background in CSS?
Place the cutout as an img on top of a container that carries the background, and rebuild the shadow with filter: drop-shadow(). Unlike box-shadow, which follows the rectangle of the element, drop-shadow follows the alpha shape of the image.
.stage {
display: grid;
place-items: center;
aspect-ratio: 4 / 3;
background: #121315 url(/img/slate-1600.webp) center / cover;
}
.stage img {
width: min(60%, 520px);
height: auto;
filter: drop-shadow(0 24px 22px rgb(0 0 0 / .45));
}A few rules keep the result believable:
- Match the shadow direction to the lighting in the product photo, not to the rest of the page.
- Keep the background quieter than the product. A textured surface such as dark slate stone or white marble works when it is slightly blurred or darkened behind the subject.
- For color matching, try
mix-blend-modeon a soft overlay rather than recoloring the cutout. The blend modes guide shows the options.
Very large photos have another option. Ship the photo as a regular JPEG or AVIF without alpha, and a small grayscale PNG as a mask. mask-image uses the alpha of the mask file by default, so the photo is cut out in the browser:
.cut {
-webkit-mask: url(/img/vase-mask.png) center / 100% 100% no-repeat;
mask: url(/img/vase-mask.png) center / 100% 100% no-repeat;
}It is worth it only when the photo is big and the mask is simple. Two requests and extra CSS are a real cost for a small thumbnail.
Should you remove backgrounds in the browser, on a server or by hand?
The right place depends on volume and on who uploads the photos. Here is how the three options compare:
| Where | Fits | Trade-off |
|---|---|---|
| By hand in an editor | A hero image, a few key products | Best edges, slow, does not scale |
| In the browser (WebGPU, WebAssembly, ONNX Runtime Web) | User uploads, editors inside your app | No upload to a server; model download and device speed vary |
| On a server or in a job queue | Catalogs, marketplaces, daily imports | Consistent output and logs; needs GPU budget and review |
Third-party APIs sit between the last two. They are quick to start with, but you pay per image and send product photos to someone else. For a few hundred images a month that is usually fine. For tens of thousands, or for photos you cannot share, a self-hosted model is often the cheaper and safer path.
What does a batch pipeline for a catalog look like?
A catalog pipeline is a short chain of steps, and each step should be able to fail on its own without stopping the rest. A typical order:
- Ingest. Take the original photo, never a resized copy. Store the source path and SKU.
- Classify. Route hair, glass and transparent products to a review queue. A simple image classifier or a product-type field in your catalog is enough.
- Segment and matte. Run the model and save a lossless master with alpha.
- Score the mask. Flag outputs where the mask covers too little or too much of the frame, or has many separate islands. Those are usually failures.
- Clean edges. Decontaminate colors and choke the mask on solid products.
- Encode. Generate AVIF and WebP at the sizes your templates use.
- Review. A person checks the flagged items on a dark and a light background before they go live.
Keep the masters. When a better model appears, you rerun step 3 on the originals instead of on already compressed files. For the photo side of the same workflow, see product photo backgrounds.
Checklist before you publish a cutout
- Removed from the highest-quality original, not a compressed copy
- Edges checked on near-black and near-white backgrounds
- No halo of the old backdrop color; edge colors decontaminated
- Shadow rebuilt with
drop-shadow()in the direction of the photo lighting - AVIF and WebP generated from a lossless master, PNG kept as fallback
width,heightand meaningfulaltset on the image- Hair, glass and clear products reviewed by a person



