Segment Anything (sam) promptable segmentation#
Meta's Segment Anything (ViT-B, quantized): segments whatever you point at with a click or box prompt.
Run it: rembg i -m sam input.jpg output.png setup
How to run sam
sam ships with rembg as the session sam. The weights download on first use.
Command line
pip install "rembg[cpu,cli]"
rembg i -m sam input.jpg output.pngPython
from rembg import new_session, remove
from PIL import Image
session = new_session("sam") # load once, reuse for every image
output = remove(Image.open("input.jpg"), session=session)
output.save("output.png")Other sessions: all rembg models.
Quick take
- Best for: Picking one object out of a busy scene with a point or box
- Avoid when: Hands-off batch background removal: it needs a prompt per image
- Notes: Not on Bench v0: results depend on the prompt. Without `sam_prompt`, rembg clicks the image center.
When a hosted model is worth it
sam is free and runs on your machine; keep it if the results above are good enough. If you need finer hair and edges, or don't want to run large models locally, rembg also ships the withoutBG Pro Model as the withoutbg session, ranked #1 of 17 by accuracy on Bench v0. Images are sent to withoutBG's servers.
Same command, hosted model
export WITHOUTBG_API_KEY=sk_...
rembg i -m withoutbg input.jpg output.pngSign up for 50 free credits (no credit card) or See its results.