搜尋此網誌

2022年6月25日 星期六

GIMP 自動去背,AI 移除影像背景 (Auto remove background, 使用 Rembg)

Rembg 是一個去除圖像背景的工具,它是 Python 寫的一個程式庫,所以需先安裝 Python

安裝 Rembg : 在命令提示字元下,輸入 pip install rembg 即可

安裝完後,輸入 rembg i path/to/input.png path/to/output.png 測試一下,是否安裝成功

path/to/input.png 是你要去背景的來源檔案,jpg 也可以

path/to/output.png 是去背完成後的輸出檔案
(第一次執行,會從網路下載預訓練的模型)

底下是它的範例輸出



RemoveBG.zip 是給 Gimp(Windows版) 與 Rembg 交互的 plugin,以利後續的編修合成

下載後,用文字編輯器打開,找到

aiExe = "C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\rembg.exe"

把它替換成 rembg 在你電腦上安裝的路徑後,再存檔

如果你是用 Linux 用戶

而且是使用 Flatpak GIMP,那可以下載 RemoveBG-Flatpak.zip 

如果是AppImage GIMP或其它非使用Sandbox的GIMP,那可以下載 RemoveBG-AppImage.zip

一樣用文字編輯器打開,找到

aiExe = "/home/jamesh/.local/bin/rembg"

把它替換成你的安裝路徑後,再存檔

之後Flatpak版再以以下方式啟動Gimp

flatpak run --socket=session-bus org.gimp.GIMP

AppImage GIMP 直接啟動即可

使用 :

在 Python-Fu -> AI移除影像背景 ...


這是內定 alpha matting 為 否 的結果

這是 alpha matting 為 是,ALPHA_MATTING_ERODE_SIZE 調 30 的結果

另一個測試,原圖

直接使用內定值的結果

再一個,原圖(取自 Pexels )


一樣直接使用內定值的結果,做了簡單的合成

最後一個,原圖

使用整張圖的結果,不是很滿意,下半部消失了

這時可以試試使用矩形選取工具定義範圍

或許會有比較好的結果


當然,如果你是用內定輸出成遮罩,當結果不是很滿意的時候,也是比較容易做編修

3 則留言:

Krzysiu 提到...

Hello!

Is this plugin by any chance on free license? Header says "software that can be used not only for personal use, but also for commercial use by individuals/companies", but I'm not sure if it allows modification of your code. I'm asking because I modernized it a bit - removed lossy compression, changed some lines to more proper python, added new models, added ability to run without showing console and added benchmark routine. If it would be on MIT license or GPL, I could publish in on GitHub - and maybe other people would make it even better. Of course your author note would remain.

JamesH 提到...

all my plugins were free licence, you can do what ever you want

Martinson 提到...

Hi
What about the development of this extension?
Will it be rewritten for GIMP 2.99/3.0?
Because it cannot be used with new GIMP 2.99/30 with a new API.