Open-source background removal and alpha matting from RGB images. This repository
hosts the OSS variant exported as a self-contained ONNX graph for ONNX Runtime.
The graph includes DepthAnythingV2 and a ConvNeXt-fused matting head — no
PyTorch checkpoints are needed at inference time.
Read the sidecar first. It is the authoritative source for canvas_size (448),
input/output names, precision, model version, depth_variant, convnext_size,
and SHA256.
1from huggingface_hub import hf_hub_download
2
3model_path = hf_hub_download(
4 repo_id="withoutbg/withoutbg-openweights-onnx",
5 filename="withoutbg-open-weights.onnx",
6)
7sidecar_path = hf_hub_download(
8 repo_id="withoutbg/withoutbg-openweights-onnx",
9 filename="withoutbg-open-weights.onnx.json",
10)
1hf download withoutbg/withoutbg-openweights-onnx \
2 withoutbg-open-weights.onnx \
3 withoutbg-open-weights.onnx.json
1from pathlib import Path
2import json
3import numpy as np
4import onnxruntime as ort
5from PIL import Image
6
7model_path = Path("withoutbg-open-weights.onnx")
8sidecar = json.loads(model_path.with_suffix(model_path.suffix + ".json").read_text())
9canvas = sidecar.get("canvas_size", 448)
10input_name = sidecar.get("input_name", "rgb")
11
12session = ort.InferenceSession(str(model_path), providers=["CPUExecutionProvider"])
13
14image = Image.open("input.jpg").convert("RGB")
15orig_w, orig_h = image.size
16scale = canvas / max(orig_w, orig_h)
17new_w = max(1, round(orig_w * scale))
18new_h = max(1, round(orig_h * scale))
19
20resized = image.resize((new_w, new_h), Image.Resampling.BILINEAR)
21padded = Image.new("RGB", (canvas, canvas), (0, 0, 0))
22padded.paste(resized, (0, 0))
23
24rgb = np.asarray(padded, dtype=np.float32) / 255.0
25rgb = np.transpose(rgb, (2, 0, 1))[None, ...]
26
27alpha_canvas = session.run(None, {input_name: rgb})[0][0, 0]
28alpha_crop = alpha_canvas[:new_h, :new_w]
29alpha_u8 = np.clip(alpha_crop * 255.0, 0, 255).astype(np.uint8)
30alpha = Image.fromarray(alpha_u8, "L").resize((orig_w, orig_h), Image.Resampling.BILINEAR)
31
32out = image.copy()
33out.putalpha(alpha)
34out.save("output.png")
1python >=3.11
2numpy
3pillow
4onnxruntime
Built with DINOv3.
This distribution includes upstream components under their respective licenses.
See
THIRD_PARTY_NOTICES.md.