不用ps也可以抠图,更快!!!!

2020-12-18  本文已影响0人  时尚灬IT男

【前言】:一款可用于快速移除背景图片的开源工具,安装配置后可在服务器、命令行中对图片进行批量操作。

简介

rembg 是使用 Python 实现的用于移除背景图片的工具,要求 Python 3.8 或更高版本,支持批量操作,使用方式比较灵活,可以直接使用命令行、作为服务运行、在 docker 中使用,还可以作为库调用。

下载安装

项目的源码地址是:
https://github.com/danielgatis/rembg

要求 Python 3.8 或更高版本,使用以下命令进行安装:
pip install rembg

安装有很多坑!!!!

系统:Windows 10(版本: 10.0.18363)
先将下载源切换到国内镜像,避免连接超时等错误。运行代码pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
错误
错误:找不到torchvision 0.8.1版本
报错时机:运行pip install rembg之后
具体信息:No matching distribution found for torchvision==0.8.1
解决方式:
在stackoverflow找到解决方法,地址
开代理安装:pip install torch===1.7.0 torchvision===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html --proxy [proxy server]
torch-1.7.0包比较大(1.2G),git-bash在下载到600Mb的时候可能会request timeout。可以用浏览器下载,下载地址:https://download.pytorch.org/whl/cu102/torch-1.7.0-cp38-cp38-win_amd64.whl,下载完成后运行pip install ~path/torch-1.7.0-cp38-cp38-win_amd64.whl
错误:找不到msvc
报错时机:编译rembg之时
具体信息:Attempted to compile AOT function without the compiler used by numpy.distutils present. Cannot find suitable msvc.
解决方式:安装c编译器
安装c编译器
原文地址:C compiler installation
步骤1:下载安装器
打开Visual Studio Download页面https://visualstudio.microsoft.com/zh-hans/downloads/
滚动页面,在『所有下载』下方点击『Visual Studio 2019 工具』

Visual Studio 2019 工具
在『Visual Studio 2019 工具』列表下找到『Visual Studio 2019 生成工具』并点击下载
安装 C++ Build Tools
安装器下载完成之后,打开它并点击,直到进入下图界面之时勾选『C++ build tools』

C++ build tools
点击安装按钮

链接:https://www.jianshu.com/p/212be2908edd

简单使用

1、在命令行中使用

在命令行中可以对服务端图片、本地图片以及整个文件夹图片进行操作,如下:
(1)对远程图片进行操作:
curl -s http://input.png | rembg > output.png
(2)对本地图片进行操作:
rembg -o path/to/output.png path/to/input.png
(3)对整个文件夹里的图片进行操作:
rembg -p path/to/inputs

2、作为服务使用

(1)启动服务
rembg-server
(2)如果图片可以直接链接访问,则可以通过浏览器直接打开以下地址操作:
http://localhost:5000?url=http://image.png
也可以使用HTML表单通过上传文件的形式进行操作:

<form action="http://localhost:5000" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="upload"/>
 </form>

3、在docker中使用

在docker中使用也很方便,直接运行以下命令:
curl -s http://input.png | docker run -i -v ~/.u2net:/root/.u2net danielgatis/rembg:latest > output.png

4、作为库调用

import sys
from rembg.bg import remove
sys.stdout.buffer.write(remove(sys.stdin.buffer.read()))

再执行命令运行:

cat input.png | python app.py > out.png

高级使用
有些图片可能使用alpha matting模式(即使用-a -ae 15选项)处理效果会更加完美,命令如下:

curl -s http://input.png | rembg -a -ae 15 > output.png

效果对比图如下:


image.png
上一篇下一篇

猜你喜欢

热点阅读