目录
  • 安装
  • 快速上手
    • 命令行调用
    • 在Python中使用

Python 的 Rembg 库可以去掉图片中的背景,效果如下:

利用Python中​Rembg库实现去除图片背景

安装

CPU版

pip install rembg

 GPU版

pip install rembg[gpu]

快速上手

命令行调用

安装成功后,可以在命令行中调动Rembg。如果只对单个图片进行处理

rembg i path/to/input.png path/to/output.png

 对多个图片文件处理(批处理),

rembg p path/to/input path/to/output

在Python中使用

把图片读取为二进制数据

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)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。