目录

今天来介绍一个特别有趣的python库,rembg库,全称是"Remove Background"的缩写,意为"去除背景",是一个基于深度学习的图像背景去除工具。
🎁库的导入
python
from rembg import remove
from PIL import Image
这两个库都是第三方库,需要自行下载安装。
🎀库的安装
清华镜像源地址:https://pypi.tuna.tsinghua.edu.cn/simple
这里建议使用清华镜像源进行安装,库不仅齐全而且安装速度很快。
打开PyCharm的终端,输入下列两行命令即可
🌸安装rembg
python
pip install rembg -i https://pypi.tuna.tsinghua.edu.cn/simple
🌸安装PIL
python
pip install Pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
建议所有的库都用清华镜像源进行安装,毕竟速度快,一般不会超时,用自带的源容易超时。
🎁rembg库去除背景
python
from rembg import remove
from PIL import Image
# 指定输入和输出路径
input_path = r"test.png" # 替换为你的实际输入路径,这里直接去复制自己电脑上图片地址即可
output_path =r"output.png" # 替换为你想要的输出路径,可以把复制过来的地址的末尾的数字改一改就可以
# 打开输入图像
inp = Image.open(input_path)
# 移除背景
out = remove(inp)
# 保存输出图像
out.save(output_path)
这里要注意两个问题:
①建议使用png图,png图很好转换,一般是图片,打开"画图"工具,点击"另存为",可以选择保存为png格式
② 首次运行此程序,会自动从Github上下载算法包(可能需要魔法才能正常下载)
🎁效果
🎀原图

🎀运行效果

🎀原图

🎀效果

第二张的效果可能不是很好哈🤣🤣🤣
🎁文末彩蛋
下面这些无水印图大家可以拿去作素材试试

