rembg

rembg

高效智能的开源图像背景去除工具

Rembg是一个开源的图像背景去除工具,支持多种预训练模型处理人像、动物、汽车等场景。该工具提供命令行、Python库和HTTP API多种使用方式,可快速批量处理图片并输出透明背景PNG。Rembg适用于个人用户和开发者,能轻松实现自动化的图像背景去除。

Rembg图像处理背景去除AI工具开源项目Github

Rembg

下载量 许可证 Hugging Face Spaces Streamlit 应用

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>

如果这个项目对您有帮助,请考虑捐赠

赞助商

<table> <tr> <td align="center" vertical-align="center"> <a href="https://photoroom.com/api/remove-background?utm_source=rembg&utm_medium=github_webpage&utm_campaign=sponsor" > <img src="https://yellow-cdn.veclightyear.com/835a84d5/c5150281-041b-4b7b-9cd9-c94f408db864.png" width="120px;" alt="Unsplash" /> </a> </td> <td align="center" vertical-align="center"> <b>PhotoRoom 移除背景 API</b> <br /> <a href="https://photoroom.com/api/remove-background?utm_source=rembg&utm_medium=github_webpage&utm_campaign=sponsor">https://photoroom.com/api</a> <br /> <p width="200px"> 快速准确的背景移除 API<br/> </p> </td> </tr> </table>

要求

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

rembg 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

rembg p

用于输入和输出都是文件夹的情况。

移除文件夹中所有图像的背景

rembg p path/to/input path/to/output

与上面相同,但会监视新文件/更改的文件以进行处理

rembg p -w path/to/input path/to/output

rembg 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

rembg b

从标准输入处理一系列RGB24图像。这主要用于与另一个程序配合使用,比如FFMPEG,将RGB24像素数据输出到标准输出,然后通过管道传递给本程序的标准输入,当然也不妨碍你手动在标准输入中输入图像。

rembg b 图像宽度 图像高度 -o 输出格式指定符

参数:

  • 图像宽度:输入图像的宽度
  • 图像高度:输入图像的高度
  • 输出格式指定符:printf风格的输出文件名指定符,例如output-%03u.png,则输出文件将被命名为output-000.pngoutput-001.pngoutput-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的完整示例列表,请转到示例页面。

作为docker使用

只需将rembg命令替换为docker run danielgatis/rembg

试试这个:

docker run -v path/to/input:/rembg danielgatis/rembg i input.png path/to/output/output.png

模型

所有模型都会下载并保存在用户主文件夹的.u2net目录中。

可用的模型有:

  • u2net (下载, 源码):适用于一般用途的预训练模型。
  • u2netp (下载, 源码):u2net模型的轻量级版本。
  • u2net_human_seg (下载, 源码):用于人体分割的预训练模型。
  • u2net_cloth_seg (下载, 源码):用于从人像中解析服装的预训练模型。这里将服装解析为3类:上身、下身和全身。
  • silueta (下载, 源码):与u2net相同,但大小减小到43Mb。
  • isnet-general-use (下载, 源码):适用于一般用途的新预训练模型。
  • isnet-anime (下载, 源码):用于动漫角色的高精度分割模型。
  • sam (下载编码器, 下载解码器, 源码):适用于任何用途的预训练模型。

如何训练自己的模型

如果你需要更精细的模型,可以尝试这个: https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289

一些视频教程

参考文献

常见问题

这个库何时会支持Python 3.xx版本?

这个库直接依赖于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许可证授权

编辑推荐精选

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

下拉加载更多