[图像处理] 基于 Rembg 的图像背景自动去除工具
背景
近期在进行图像处理相关需求的时候,处理了图像水印相关需求,想到了图像抠图,去除图像背景,通过查询使用 Rembg 开源库,对其进行环境配置和试用,特此记录。
步骤
安装
命令行安装 命令行安装 pip install rembg
源码安装
git clone https://github.com/danielgatis/rembg.git
pip install .
额外依赖
pip install click
pip install filetype
pip install watchdog
pip install aiohttp
pip install gradio
pip install asyncer
复制模型文件
cp u2net.onnx /root/.u2net/
添加环境变量
vi ~/.bashrc
export OMP_NUM_THREADS=10
. ~/.bashrc
执行命令
rembg i after.png after-out.png
执行过程
![](https://static001.geekbang.org/infoq/f4/f4aea69076713f59497f01a6ac3ea86d.png)
[图像处理]基于Rembg的图像背景自动去除工具-输入图像1
![](https://static001.geekbang.org/infoq/32/32997abc99d19b29d556079e7b756d03.png)
[图像处理]基于Rembg的图像背景自动去除工具-输出图像1
![](https://static001.geekbang.org/infoq/60/60841073b6098c8fcc2871d89d3928dd.png)
[图像处理]基于Rembg的图像背景自动去除工具-输入图像2
![](https://static001.geekbang.org/infoq/91/91ea038e5dadeb9068e5f4902eee6770.png)
[图像处理]基于Rembg的图像背景自动去除工具-输出图像2
总结
基于 Rembg 图像背景自动去除工具,搭建环境后对图片进行抠图。
参考
版权声明: 本文为 InfoQ 作者【alexgaoyh】的原创文章。
原文链接:【http://xie.infoq.cn/article/92eb0b9afd1d174a0f81868f9】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
评论