高效智能的开源图像背景去除工具
Rembg是一个开源的图像背景去除工具,支持多种预训练模型处理人像、动物、汽车等场景。该工具提供命令行、Python库和HTTP API多种使用方式,可快速批量处理图片并输出透明背景PNG。Rembg适用于个人用户和开发者,能轻松实现自动化的图像背景去除。
Rembg 是一个用于移除图像背景的工具。
<p style="display: flex;align-items: center;justify-content: center;"> <img alt="示例 车-1" src="https://yellow-cdn.veclightyear.com/835a84d5/0602f8c5-2f02-405e-988b-62951028de22.jpg" width="100" /> <img alt="示例 车-1.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/11fad1f6-9cb4-4c98-9f30-ddde595d72b8.png" width="100" /> <img alt="示例 车-2" src="https://yellow-cdn.veclightyear.com/835a84d5/132e94e1-cf3e-431f-bde8-d3fe8d2e1c0d.jpg" width="100" /> <img alt="示例 车-2.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/1f4f32e4-eee0-4a05-afbc-155c96de7c0d.png" width="100" /> <img alt="示例 车-3" src="https://yellow-cdn.veclightyear.com/835a84d5/05f9a674-1d9d-416b-8080-f21bb0d00948.jpg" width="100" /> <img alt="示例 车-3.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/cb7fc9d5-b4dd-49e8-a892-a1b4ed06a3fb.png" width="100" /> </p> <p style="display: flex;align-items: center;justify-content: center;"> <img alt="示例 动物-1" src="https://yellow-cdn.veclightyear.com/835a84d5/9c5a3fe2-4d2d-431f-8354-3ec19e861e99.jpg" width="100" /> <img alt="示例 动物-1.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/422f205b-e0df-4bd4-8e10-6a19877c52de.png" width="100" /> <img alt="示例 动物-2" src="https://yellow-cdn.veclightyear.com/835a84d5/f8375c51-c1a1-4f7e-91a5-49f36b90fb4a.jpg" width="100" /> <img alt="示例 动物-2.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/20fb0db1-7ec7-4402-a602-dcdf1e34da1b.png" width="100" /> <img alt="示例 动物-3" src="https://yellow-cdn.veclightyear.com/835a84d5/e3a449e2-a953-48de-9db4-52c8c2ea8a5a.jpg" width="100" /> <img alt="示例 动物-3.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/4af89e2d-c39a-44c5-a01e-bde84746827e.png" width="100" /> </p> <p style="display: flex;align-items: center;justify-content: center;"> <img alt="示例 女孩-1" src="https://yellow-cdn.veclightyear.com/835a84d5/e6c60a64-b569-40a2-8e68-f02a82ffc6fd.jpg" width="100" /> <img alt="示例 女孩-1.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/a0eb78f8-daae-4eb0-b3ff-4178ec3825c0.png" width="100" /> <img alt="示例 女孩-2" src="https://yellow-cdn.veclightyear.com/835a84d5/114316da-9ef5-4a5a-a041-b8bd9a46ba69.jpg" width="100" /> <img alt="示例 女孩-2.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/3e2ce394-94aa-417d-98fc-409c5df9123c.png" width="100" /> <img alt="示例 女孩-3" src="https://yellow-cdn.veclightyear.com/835a84d5/b3500424-911a-4a12-817c-fde7e7be07c9.jpg" width="100" /> <img alt="示例 女孩-3.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/a95359ca-9581-4f99-8c5b-97c2e41f1f63.png" width="100" /> </p> <p style="display: flex;align-items: center;justify-content: center;"> <img alt="示例 动漫女孩-1" src="https://yellow-cdn.veclightyear.com/835a84d5/13086b42-6c62-49c5-ae54-d2d119f0a0d5.jpg" width="100" /> <img alt="示例 动漫女孩-1.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/568875bd-4a64-47ef-a347-30dbe94d063e.png" width="100" /> <img alt="示例 动漫女孩-2" src="https://yellow-cdn.veclightyear.com/835a84d5/71faffa0-ee1b-474f-b809-f62892c9af43.jpg" width="100" /> <img alt="示例 动漫女孩-2.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/f1f266d0-aff3-4b4e-b750-097026bbe503.png" width="100" /> <img alt="示例 动漫女孩-3" src="https://yellow-cdn.veclightyear.com/835a84d5/2d2c5b4a-ccdd-45fb-b488-bbf6eecfd7fd.jpg" width="100" /> <img alt="示例 动漫女孩-3.输出" src="https://yellow-cdn.veclightyear.com/835a84d5/ce784262-8de5-42e1-8053-80d3199bfb0e.png" width="100" /> </p>如果这个项目对您有帮助,请考虑捐赠。
python: >3.7, <3.13
CPU 支持:
pip install rembg # 仅安装库 pip install rembg[cli] # 安装库和命令行界面
GPU 支持:
首先,您需要检查您的系统是否支持 onnxruntime-gpu
。
访问 https://onnxruntime.ai 并查看安装矩阵。
<p style="display: flex;align-items: center;justify-content: center;"> <img alt="onnxruntime-安装矩阵" src="https://yellow-cdn.veclightyear.com/835a84d5/04f285c4-558a-4211-b241-ad4bcaf9ba37.png" width="400" /> </p>如果支持,只需运行:
pip install rembg[gpu] # 仅安装库 pip install rembg[gpu,cli] # 安装库和命令行界面
安装完成后,您可以在终端窗口中输入 rembg
来使用 rembg。
rembg
命令有 4 个子命令,分别对应不同的输入类型:
i
用于文件p
用于文件夹s
用于 HTTP 服务器b
用于 RGB24 像素二进制流您可以使用以下命令获取主命令的帮助:
rembg --help
同样,您可以使用以下命令获取所有子命令的帮助:
rembg <命令> --help
i
用于输入和输出都是文件的情况。
从远程图像中移除背景
curl -s http://input.png | rembg i > output.png
从本地文件中移除背景
rembg i path/to/input.png path/to/output.png
指定模型并移除背景
rembg i -m u2netp path/to/input.png path/to/output.png
移除背景并仅返回蒙版
rembg i -om path/to/input.png path/to/output.png
应用 alpha 抠图并移除背景
rembg i -a path/to/input.png path/to/output.png
传递额外参数
SAM 示例 rembg i -m sam -x '{ "sam_prompt": [{"type": "point", "data": [724, 740], "label": 1}] }' examples/plants-1.jpg examples/plants-1.out.png
自定义模型示例 rembg i -m u2net_custom -x '{"model_path": "~/.u2net/u2net.onnx"}' path/to/input.png path/to/output.png
p
用于输入和输出都是文件夹的情况。
移除文件夹中所有图像的背景
rembg p path/to/input path/to/output
与上面相同,但会监视新文件/更改的文件以进行处理
rembg p -w path/to/input path/to/output
s
用于启动 HTTP 服务器。
rembg s --host 0.0.0.0 --port 7000 --log_level info
要查看完整的端点文档,请访问:http://localhost:7000/api
。
从图像 URL 中移除背景
curl -s "http://localhost:7000/api/remove?url=http://input.png" -o output.png
从上传的图像中移除背景
curl -s -F file=@/path/to/input.jpg "http://localhost:7000/api/remove" -o output.png
b
从标准输入处理一系列RGB24图像。这主要用于与另一个程序配合使用,比如FFMPEG,将RGB24像素数据输出到标准输出,然后通过管道传递给本程序的标准输入,当然也不妨碍你手动在标准输入中输入图像。
rembg b 图像宽度 图像高度 -o 输出格式指定符
参数:
output-%03u.png
,则输出文件将被命名为output-000.png
、output-001.png
、output-002.png
等。无论指定的扩展名是什么,输出文件都将以PNG格式保存。你可以省略它以将结果写入标准输出。与FFMPEG一起使用的示例:
ffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o folder/output-%03u.png
宽度和高度值必须与FFMPEG输出图像的尺寸相匹配。注意对于FFMPEG,"-an -f rawvideo -pix_fmt rgb24 pipe:1
"部分是整个命令运行所必需的。
输入和输出为字节
from rembg import remove input_path = 'input.png' output_path = 'output.png' with open(input_path, 'rb') as i: with open(output_path, 'wb') as o: input = i.read() output = remove(input) o.write(output)
输入和输出为PIL图像
from rembg import remove from PIL import Image input_path = 'input.png' output_path = 'output.png' input = Image.open(input_path) output = remove(input) output.save(output_path)
输入和输出为numpy数组
from rembg import remove import cv2 input_path = 'input.png' output_path = 'output.png' input = cv2.imread(input_path) output = remove(input) cv2.imwrite(output_path, output)
强制输出为字节
from rembg import remove input_path = 'input.png' output_path = 'output.png' with open(input_path, 'rb') as i: with open(output_path, 'wb') as o: input = i.read() output = remove(input, force_return_bytes=True) o.write(output)
如何以高性能方式迭代文件
from pathlib import Path from rembg import remove, new_session session = new_session() for file in Path('path/to/folder').glob('*.png'): input_path = str(file) output_path = str(file.parent / (file.stem + ".out.png")) with open(input_path, 'rb') as i: with open(output_path, 'wb') as o: input = i.read() output = remove(input, session=session) o.write(output)
要查看如何使用rembg的完整示例列表,请转到示例页面。
只需将rembg
命令替换为docker run danielgatis/rembg
。
试试这个:
docker run -v path/to/input:/rembg danielgatis/rembg i input.png path/to/output/output.png
所有模型都会下载并保存在用户主文件夹的.u2net
目录中。
可用的模型有:
如果你需要更精细的模型,可以尝试这个: https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289
这个库直接依赖于onnxruntime库。因此,只有当onnxruntime提供对特定版本的支持时,我们才能更新Python版本。
喜欢我的一些作品吗?请我喝杯咖啡(或者更可能是啤酒)
<a href="https://www.buymeacoffee.com/danielgatis" target="_blank"><img src="https://yellow-cdn.veclightyear.com/835a84d5/b7428d98-42b8-484d-8fbf-c2cbec29b206.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;"></a>
版权所有 (c) 2020-现在 Daniel Gatis
基于MIT许可证授权
AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
一站式AI创作平台
提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作
AI办公助手,复杂任务高效处理
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!
AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。