搜尋此網誌

2022年6月25日 星期六

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

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

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

安裝完後,輸入 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 )


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

最後一個,原圖

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

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

或許會有比較好的結果


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

10 則留言:

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.

Lukas ThyWalls 提到...
作者已經移除這則留言。
Lukas ThyWalls 提到...

Hello! Very useful little script you have here!

I found something with the 2023-02-09 dated version, that I think I didn't see with an older one but I'm not sure (I was using that until recently that I realized there is a new version).

When the layer is bigger or smaller than the canvas, and both are not aligned in the top, right or both, the RemoveBG result mask is shifted. Images talk better here than thousands words: https://i.imgur.com/HgGz2rX.png https://i.imgur.com/8qi5b3t.png

Thanks for your work!

PD.: I deleted the older comment to add the example of a bigger canvas than the layer.

JamesH 提到...

I had fix that error
You can download again

Lukas ThyWalls 提到...

Tested and It looks like fixed.

Thanks!

JamesH 提到...

you're welcome

Ivon Huang 提到...

幫補充比較詳細的安裝方法
Linux Flatpak版GIMP 2.10,外掛路徑是在 ~/.var/app/org.gimp.GIMP/config/GIMP/2.10/plug-ins/

把RemoveBG.py放進去後還要chmod +x RemoveBG.py賦予執行權限,才會顯示在GIMP的選單上。

JamesH 提到...

謝謝您的補充