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许可证授权

编辑推荐精选

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

扣子-AI办公

扣子-AI办公

AI办公助手,复杂任务高效处理

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

TRAE编程

TRAE编程

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

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

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

下拉加载更多