环境mac
python版本 3.9.13
Github地址 https://github.com/danielgatis/rembg
去除背景效果真的不错。
上一张github的图。
下面介绍一下:
安装
python的版本必须要切换到 3.9,高版本的会安装失败。
python -m pip install rembg
使用的时候需要下载一个模型,模型地址 https://drive.google.com/uc?id=1tCU5MM1LhRgGou5OpmpjBQbSrYIUoYab
这里可以直接下载~
放到下边目录下使用:
~/.u2net/u2net.onnx
还有一种docker安装方式,不过试了一下没成功。
使用
有三种使用方式。
cli方式
# 处理文件
rembg i path/to/input.png path/to/output.png
# 处理文件夹
rembg p path/to/input path/to/output
server方式
# 启动服务
rembg s
# 使用
http://localhost:5000/?url=https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/1280px-Gull_portrait_ca_usa.jpg
library方式
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)