Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

How to Remove an Image Background in the Browser (No Upload, No

Most background remover sites ask you to upload your photo to their server. That is slow, and not great when the image is private. There is another way: run the AI model inside the browser. The image never leaves the us

Most background remover sites ask you to upload your photo to their server. That is slow, and not great when the image is private.

There is another way: run the AI model inside the browser. The image never leaves the user's device. I did this for a small tools site I run, and here is how it works.

The idea

Open-source libraries like @imgly/background-removal ship a segmentation model that runs on the client with WebAssembly. Your page downloads the model once, the browser caches it, and every later image is processed locally.

Basic usage

import { removeBackground } from "@imgly/background-removal";

const blob = await removeBackground(file, {
  model: "isnet_fp16",
  output: { format: "image/png" },
});

const url = URL.createObjectURL(blob);

file is the image the user picked, and blob is a transparent PNG.

Things I learned

1. Shrink the input first. Running the model on a 4000 px photo is slow. I resize the image to about 1024 px for the model, then use the resulting mask on the original image for export, so the final PNG keeps its resolution.

2. Clean the edges. Raw masks leave a faint halo around hair and skin. Blurring the mask a little and applying a threshold gives much cleaner edges.

3. Expect a one-time download. The model is roughly 90 MB. Tell the user about it with a progress message, because without one the first run looks frozen.

4. Some networks block the model CDN. In my case, one browser setup could not fetch the model files. A same-origin fallback (proxying the files through your own domain) fixed it.

Try it

I put all of this into a free tool, so you can see how the result looks before building your own: Remove Background. It needs no signup, and the image is not uploaded.

If you build something similar, I would like to hear which edge-cleanup tricks worked for you.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.