用python抠图

GitHub上有个好完的项目,rembg

简单实用的删除图像背景/抠图工具
能把图片背景去掉,抠出来主要的内容。

在这里插入图片描述

如何使用?

1、安装

CPU support:

pip install rembg

GPU support:

pip install rembg[gpu]

安装的东西挺多的,安装完成后就可以使用了

2、使用

使用方式很多;可以敲一行命令直接抠图、也可以部署到服务器上用浏览器抠图、也可以写py代码抠图、也可以用docker等等。

2.1 以命令行方式使用

2.1.1、在命令行中,切换到使用的虚拟环境,我的虚拟环境起名叫py310
conda activate py310
2.1.2、开始抠图
# 1、扣本地的图
rembg i path/to/input.png path/to/output.png
# 2、扣网上的图
curl -s http://input.png | rembg i > output.png
# 3、扣整个文件夹的图
rembg p path/to/input path/to/output

抠图前:(图片来自百度)
请添加图片描述

抠图后:
请添加图片描述

2.2以服务的方式使用

也就是可以部署到自己云服务器上,整个网页或者app什么的,就可以随时随地抠图了。

2.2.1 官方网页版
# 在虚拟环境中,敲以下命令:
rembg s

在这里插入图片描述

会提示服务地址:http://0.0.0.0:5000
注意这个地址不是直接打开的,后边得加个docs,也就是:http://0.0.0.0:5000/docs
get方式适用于网上的URL地址
post方式适合文件流,这里我们使用post的方式
在这里插入图片描述

点Try it out
在这里插入图片描述

上传图片
在这里插入图片描述

点execute
在这里插入图片描述

得到抠图结果:
在这里插入图片描述

2.2.2 自己做网页用

新建一个html文件,添加如下代码:

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

打开html文件
在这里插入图片描述

在浏览器中上传文件之后,会自动抠图
在这里插入图片描述

在这里插入图片描述

2.3以一个python库的方式使用

2.3.1 以文件流的方式

Input and output as bytes

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)
2.3.2 用PIL图像的方式

Input and output as a PIL image

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)
2.3.3 甚至于用numpy array

Input and output as a numpy array

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)

2.4以docker的方式使用(这个以后试试)

docker run -p 5000:5000 danielgatis/rembg s

写了这么多,问一个灵魂问题:
作为一只程序猿,抠图有什么用呢?我自己一般用来做ppt的时候用。不过好像很多软件都自带了抠图功能,所以rembg这个项目大家感兴趣玩玩就行了。

举个例子:
图1是没抠图插入ppt的样子;图2是抠图之后插入ppt之后的样子。图2对原版ppt遮挡较小,并且没什么违和感。
在这里插入图片描述

在这里插入图片描述

根据引用所述,Python作为一门高效的编程语言,其图像处理能力也非常强大。Python图像处理库具有高精度的边缘检测、自动裁剪等功能,可以准确地提取需要的物体。因此,Python抠图算法的实现方式可以分为以下几个步骤: 1.读取图像:使用Python的图像处理库PIL(Python Imaging Library)或OpenCV等库读取需要进行抠图的图像。 2.预处理:对读取的图像进行预处理,包括图像增强、降噪、平滑等操作,以提高后续处理的准确性。 3.边缘检测:使用Python的图像处理库进行边缘检测,以便更好地分离前景和背景。 4.分割图像:根据边缘检测的结果,将图像分割成前景和背景两部分。 5.抠图:根据分割出的前景部分,使用Python的图像处理库进行抠图操作,将前景部分从原图中分离出来。 6.保存图像:将抠图后的结果保存为需要的格式,如PNG、JPEG等。 以下是一个使用OpenCV库实现的Python抠图算法的代码示例: ```python import cv2 # 读取图像 img = cv2.imread('image.jpg') # 预处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) # 边缘检测 edges = cv2.Canny(blur, 50, 150) # 分割图像 contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) mask = cv2.drawContours(img, contours, -1, (0, 0, 255), 3) # 抠图 result = cv2.bitwise_and(img, mask) # 保存图像 cv2.imwrite('result.png', result) ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值