rembg:命令行一键AI抠图工具部署指南
官方信息
| 项目名称 | rembg |
| 仓库地址 | https://github.com/danielgatis/rembg |
| 官方网站 | GitHub 主页 |
| 最新更新 | 2026-08-18(v2.0.85) |
| 项目定位 | 基于 U2-Net 模型的命令行 AI 抠图工具,一行命令去掉图片背景,支持 CLI、Python 库、HTTP 服务、Docker 四种使用方式 |
项目背景
电商商家最头疼的就是商品图处理:拍了 100 张产品照片,每张都要抠掉背景换成纯白底,用 Photoshop 手动抠一个要 5 分钟,100 张就是大半天。在线抠图网站虽然方便,但图片要上传到别人服务器,隐私和批量处理都不方便。
rembg 就是为了解决这个问题而生的开源工具。它基于 U2-Net 深度学习模型训练,能自动识别图片中的主体(人、动物、商品),一键去除背景输出透明 PNG。命令行操作支持批量处理,Python API 可以集成到自己的业务系统,HTTP 服务可以部署成内网抠图接口。
项目在 GitHub 上收获超过 50K Star,是 AI 抠图领域最热门的开源项目,被广泛用于电商批量修图、头像制作、设计素材处理、视频绿幕替换等场景。
核心特性
- 一行命令抠图:rembg i input.jpg output.png,最简单的 AI 抠图
- 多模型支持:内置 u2net、u2netp、u2net_human_seg、isnet-general-use、birefnet 等多种模型
- 批量处理:支持整个文件夹批量抠图,电商批量修图必备
- HTTP 服务模式:rembg s 启动 Web 服务,POST 图片返回抠图结果
- Python 库:import rembg 直接集成到自己的 Python 项目
- Docker 镜像:docker run 一行部署,环境零配置
- GPU 加速:支持 onnxruntime-gpu,批量处理速度快 10 倍
- 多种输出:支持透明 PNG、纯色背景、灰度蒙版等多种输出格式
环境要求
- 操作系统:Windows 10+/macOS 12+/Linux
- Python:3.11 ~ 3.13(官方要求 >=3.11, <3.14)
- 显卡:非必需,CPU 即可;GPU 加速需安装 onnxruntime-gpu
- 内存:最低 4GB
- 磁盘空间:约 500MB(含模型权重)
详细部署步骤
方式一:pip 安装(推荐)
第 1 步:创建虚拟环境
conda create -n rembg python=3.11 -y
conda activate rembg第 2 步:CPU 版本安装
# 只安装 Python 库
pip install "rembg[cpu]"
# 安装 Python 库 + 命令行工具
pip install "rembg[cpu,cli]"第 3 步:GPU 版本安装(可选,NVIDIA 用户)
# 先确认你的 CUDA 版本,参考 https://onnxruntime.ai
# CUDA 11.x:
pip install "rembg[gpu,cli]"
# CUDA 12.x:
pip install onnxruntime-gpu --extra-index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/
pip install "rembg[cli]"第 4 步:验证安装
rembg --version方式二:Docker 部署(推荐服务器)
# 拉取官方镜像
docker pull danielgatis/rembg:latest
# 启动 HTTP 服务模式
docker run -p 5000:5000 \
-v ~/.u2net:/root/.u2net \
danielgatis/rembg:latest s
# 或者直接处理单个文件
docker run -v $(pwd):/data danielgatis/rembg:latest \
i /data/input.jpg /data/output.png方式三:从源码安装
git clone https://github.com/danielgatis/rembg.git
cd rembg
# 安装 Poetry(项目用 Poetry 管理依赖)
pip install poetry
# 安装依赖
poetry install
# 运行
poetry run rembg --help使用方法
1. 命令行单张抠图
# 最基本用法:输入图片,输出透明 PNG
rembg i input.jpg output.png
# 指定模型(默认 u2net,换人像用 u2net_human_seg)
rembg i -m u2net_human_seg portrait.jpg portrait_nobg.png
# 指定输出为纯色背景(白色)
rembg i -a -ae 15 input.jpg output_white.png
# 参数说明:
# i = 模式:单张图片处理
# -m = 指定模型
# -a = 输出 alpha 蒙版(黑白图)
# -ae = alpha 边缘羽化程度2. 批量处理文件夹
# 处理整个 input_dir 目录,输出到 output_dir
rembg p ./input_dir ./output_dir
# 指定模型批量处理
rembg p -m isnet-general-use ./products/ ./products_nobg/
# p = 模式:批量处理目录
# 支持 jpg/jpeg/png/bmp/webp 等常见格式3. HTTP 服务模式
# 启动 Web 服务,监听 5000 端口
rembg s --host 0.0.0.0 --port 5000
# 调用 API
curl -X POST -F "file=@input.jpg" http://localhost:5000/api/remove \
-o output.png
# 指定模型
curl -X POST -F "file=@input.jpg" \
-F "model=u2net_human_seg" \
http://localhost:5000/api/remove -o output.png4. Python API 调用
from rembg import remove, new_session
from PIL import Image
# 最简单用法
input = Image.open('input.jpg')
output = remove(input)
output.save('output.png')
# 使用指定模型(复用 session 加速批量处理)
session = new_session(model_name="isnet-general-use")
for img_path in ['1.jpg', '2.jpg', '3.jpg']:
inp = Image.open(img_path)
out = remove(inp, session=session)
out.save(img_path.replace('.jpg', '_nobg.png'))
# 输出 alpha 蒙版
output_alpha = remove(input, alpha_matting=True, alpha_matting_foreground_threshold=240)
output_alpha.save('mask.png')可用模型列表
| 模型名 | 适用场景 | 大小 |
|---|---|---|
| u2net | 通用场景(默认) | 170MB |
| u2netp | 轻量版,速度快 | 4.7MB |
| u2net_human_seg | 人像分割 | 170MB |
| isnet-general-use | 通用场景,效果更好 | 170MB |
| birefnet-general | 最新模型,精度最高 | 约 900MB |
常见问题 FAQ
Q1:首次运行模型下载很慢?
A:模型默认从 GitHub 下载到 ~/.u2net 目录。国内用户可以手动从 Releases 下载 u2net.onnx,放到 ~/.u2net/(Windows 为 C:\Users\用户名\.u2net\)目录下。
Q2:抠图边缘有白边/锯齿怎么办?
A:加 alpha matting 参数:rembg i -a -ae 15 -af 240 -ab 10 input.jpg output.png,可以显著改善边缘质量。人像推荐用 u2net_human_seg 模型。
Q3:Python 3.10 能用吗?
A:新版 rembg 要求 Python >=3.11。如果必须用 3.10,请安装旧版:pip install "rembg[cli]==2.0.50"。
Q4:批量处理 1000 张图要多久?
A:CPU 模式每张约 1~2 秒,1000 张约 20~30 分钟。GPU 模式每张约 0.1 秒,1000 张约 2 分钟。建议用 Python API 复用 session,比命令行快很多。
Q5:Docker 部署后外网访问安全吗?
A:rembg s 默认无鉴权,暴露到公网有被滥用风险。建议加 Nginx 反向代理 + Basic Auth,或只在内网使用。
跳转链接
- GitHub 仓库:https://github.com/danielgatis/rembg
- PyPI 页面:https://pypi.org/project/rembg/
- U2-Net 论文:https://arxiv.org/abs/2005.09007
- Docker 镜像:https://hub.docker.com/r/danielgatis/rembg