天天看點

Python 摳圖庫 rembg安裝與應用

Python 摳圖庫 rembg, 可以用于特定前景的摳圖,不需要特殊指定。下圖是項目例子

Python 摳圖庫 rembg安裝與應用

項目連結:https://github.com/danielgatis/rembg 

該項目會用到u2net這個子產品進行識别,具體的原理不清楚,有興趣可以了解一下。

如果沒有下載下傳這個子產品,系統會向https://drive.google.com請求這個子產品。

我測試時GitHub和 drive Google都上不了。。。

該項目的檔案結構名稱:

Python 摳圖庫 rembg安裝與應用

bg.py檔案裡有remove函數對圖檔進行摳圖。

下面介紹安裝方法:

系統測試環境:

 1. Win10

2. Anaconda3(Python3.9)

rembg要求Python3.8或以上,如果用Win7 環境,Python版本不能超過3.8.3(好像是)。

1. Anaconda 建立 rembg2環境,并激活該環境

conda create -n rembg2 python=3.9
conda activate rembg2
           

2. 安裝rembg,我現在安裝的是rembg-2.0.21版本,該指令會下載下傳很多依賴。

pip install rembg
           
Python 摳圖庫 rembg安裝與應用

 3. 拷貝u2net.onnx到C:\users\使用者名\.u2net目錄上,該檔案的正規下載下傳路徑:

 參考以下路徑說明:rembg · PyPI

Python 摳圖庫 rembg安裝與應用

https://pypi.org/project/rembg/

Python 摳圖庫 rembg安裝與應用

百度雲下載下傳連結:https://pan.baidu.com/s/13slteJLATzf1LDmIkcqgRw?pwd=wa78

 (感謝B站up主的共享)

Python 摳圖庫 rembg安裝與應用

 4. 測試

--單檔案測試

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

例如
rembg i 1.jpg 2.jpg
           

 --檔案夾測試

rembg p path/to/input path/to/output
例如
rembg p .\image .\image2

help:
rembg --help
           

 測試時提示報錯,安裝Visual Studio後正常。

ImportError: Microsoft Visual C++ Redistributable for Visual Studio 2019 not installed on the machine.
           

5. 測試效果

如果不放大看,個人覺得還可以,但如果在複雜環境,效果不是非常好,不知道是否有優化方法。 

 主要說明以上。