Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

AI Remove image background(use rembg)

Mon Jul 25, 2022 7:49 am

First you need to successful install rembg

Download the RemoveBG.py, open with text editor, find
aiExe = "C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\rembg.exe"
Replace with your own rembg path

Image

Python-fu->AI移除影像背景(AI Remove background) ...
Image

Image

for linux flatpak gimp
RemoveBG-Flatpak.zip
find
aiExe = "/home/jamesh/.local/bin/rembg"
Replace with your own rembg path

then run gimp like below
flatpak run --socket=session-bus org.gimp.GIMP

Test successful on ubuntu

for linux AppImage gimp
RemoveBG-AppImage.zip
find
aiExe = "/home/jamesh/.local/bin/rembg"
Replace with your own rembg path

Test successful on ubuntu 22.04

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 10:33 am

I am on Linux and cannot get this working. The path to REMBG I linked was
Code:
/home/contrast/.local/bin/rembg

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 10:50 am

If anyone gets this working on Linux I would like to know what they did and what version of REMBG they are using.

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 12:31 pm

I have my rembg installed using Anaconda. So I have to figure out how to start that env from within the python script first.
I use rembg standalone .. without GIMP.

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 7:25 pm

I used it on video clips converted to PNGs using ffmpeg. It is by no means perfect. It leaves cloudy areas on anything but clear, distinct images.

The drive I had it on died. I found it works with both anaconda python and the installed python 3.9. Ubuntu 22.04 has updated to python3.10.
Here are my notes for the installed python. (I think it does work on python 3.10)

sudo apt install python3-pip
pip install rembg
python --version =3.10.4
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.9 -y
python3.9 --version =3.9.12

add REMBG to path.
You also need to download 'models' that the AI uses to remove backgrounds.
one of them is here.
Copy the custom-model.pth file to ~/.u2net (thats in your home directory)


I made simple scripts to process folders of PNGs.

Code:
#!/bin/bash
mkdir ./done
rembg -m u2net_human_seg -p ./ ./done/


https://github.com/danielgatis/rembg
Nelo's instructions for anaconda are here:
https://www.gimp-forum.net/Thread-Remov ... G#pid22354

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 8:25 pm

What do I put here to get it working on Linux?

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


I tried REMBG, /home/contrast/.local/bin/rembg and /home/contrast/.local/bin/rembg i and none of them working.

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 8:55 pm

i tested on my android phone(termux linux) with backgroundremover
it does not need full path, maybe you can try it with
aiExe = "rembg"

Re: AI Remove image background(use rembg)

Mon Jul 25, 2022 9:22 pm

This is for Windows but you may try it in other OS. This plugin really work.
I found this plugin a few months ago at a website call "romove.bg" and sent me to download a GIMP plugin from;
https://github.com/manu12121999/RemoveBG-GIMP
I downloaded it and installed it, then I signed (you have to sign up) and you get a key code, to make the plugin work. Once you get the "key code", save it in your desktop with notepad. As long as you have this key, you can use the plugin and you can erase the background within the GIMP program.



The photo is from https://www.krugerpark.co.za/africa_lion.html

ImageImage

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 12:41 am

:clap :clap :clap

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 12:51 am

Thx for sharing. :)

And,,,,, thank you manu12121999!

Star-for-manu12121999-2022-07-26_014500.jpg
Star-for-manu12121999-2022-07-26_014500.jpg (227.04 KiB) Viewed 38750 times


:hi5

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 1:11 am

I've been waiting a long time for that. Great, thank you!

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 1:13 am

You can call me impressed. Any time you need to remove white bg from very white hair it's darn near impossible.

Before rembg

before-rembg-2022-07-26_020452.jpg
before-rembg-2022-07-26_020452.jpg (66.13 KiB) Viewed 37879 times


After rembg

After-rembg-2022-07-26_020552.jpg
After-rembg-2022-07-26_020552.jpg (75.61 KiB) Viewed 37879 times


How the heck does it know what's bg or fg? It just runs and works like magic :)
And i love the fact that it's non destructive utilizing a mask. :tyspin :gimp

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 1:57 am

Peter Falkingham said it best.

https://peterfalkingham.com/2021/07/19/ ... d-remover/

I need to buy a new laptop and convert this current one to Linux so I can get some serious playtime in.

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 7:40 am

My main problem has to do with the fact that I use a Flatpak Gimp. Does anyone know how to edit the script and change it to support flatpak gimp's directory?

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 11:29 am

JamesH he wrote shortly: First you need to successful install rembg.
For Windows, this is not too complicated, but with a few surprises.

I will share my experience of installing rembg.exe in Windows 10_64bit.
I tried to compile in msys64 - unfortunately without success (lots of bugs to analyze - although the use of msys64 seems not to be considered eg.: AttributeError: 'MSVCCompiler').

So I installed python-3.9.7-amd64.exe and then:
C: \ Python39> python.exe -m pip install rembg
Successfully installed:
PySocks-1.7.1 PyWavelets-1.3.0 aiohttp-3.8.1 aiosignal-1.2.0 anyio-3.6.1 asgiref-3.5.2 async-timeout-4.0.2 asyncer-0.0.1 attrs-21.4.0 beautifulsoup4-4.11.1 certifi-2022.6.15 charset-normalizer-2.1.0 click-8.0.3 colorama-0.4.5 fastapi-0.72.0 filelock-3.7.1 filetype-1.0.9 flatbuffers-2.0 frozenlist-1.3.0 gdown-4.4.0 h11-0.13.0 idna-3.3 imageio-2.20.0 llvmlite-0.38.1 multidict-6.0.2 networkx-2.8.5 numba-0.55.2 numpy-1.22.3 onnxruntime-1.10.0 packaging-21.3 pillow-9.0.1 protobuf-4.21.3 pydantic-1.9.1 pymatting-1.1.5 pyparsing-3.0.9 python-multipart-0.0.5 rembg-2.0.21 requests-2.28.1 scikit-image-0.19.1 scipy-1.8.0 six-1.16.0 sniffio-1.2.0 soupsieve-2.3.2.post1 starlette-0.17.1 tifffile-2022.5.4 tqdm-4.62.3 typing-extensions-4.3.0 urllib3-1.26.11 uvicorn-0.17.0 watchdog-2.1.7 yarl-1.7.2

After correcting the path in RemoveBG.py, I ran the plugin.
Another surprise:

Image

So I installed:

Image

I restarted it and surprise again:

Image

It only uses the model u2net.onnx (A pre-trained model for general use cases) - I don't know how to change it, for example: u2net_human_seg.onnx (A pre-trained model for human segmentation).
The result leaves little illusion, so I agree with Tas_mania opinion: It is by no means perfect.
However, there is a mask for corrections - so thanks to JamesH for thinking about it.
Also, the installation method is probably not encouraging for the majority of users.

Re: AI Remove image background(use rembg)

Tue Jul 26, 2022 1:30 pm

MareroQ wrote:It only uses the model u2net.onnx (A pre-trained model for general use cases) - I don't know how to change it, for example: u2net_human_seg.onnx (A pre-trained model for human segmentation).


I guess it could be something like this (found somewhere on that site)

Add a custom model
Copy the custom-model.pth file to ~/.u2net and run:
Code:
rembg -m custom-model > output.png


Probably replace by the name of the model.
So:
Code:
rembg -m u2net_human_seg > output.png

Maybe you have to download the model in question first and place it in that u2net folder.

Re: AI Remove image background(use rembg)

Wed Jul 27, 2022 6:35 am

Thanks Pocholo for the Windows version. It is amazing.

Re: AI Remove image background(use rembg)

Sat Jul 30, 2022 2:15 am

Image


I came across DIS (Dichotomous Image Segmentation). It could be a developed version of REMBG. I may try it because this team also developed the U2-Net models used by REMBG.

Training neural networks appears to be a rapidly developing industry ATM. The gif above is a Gimp-reduced one from their project page.

Re: AI Remove image background(use rembg)

Sat Jul 30, 2022 5:53 am

You did all of that with Gimp? Really nice!

Re: AI Remove image background(use rembg)

Sat Jul 30, 2022 9:45 pm

wirra wrote:Thanks Pocholo for the Windows version. It is amazing.

You're welcome, wirra! It's so much simpler the one I posted for simple Windows users.
Post a reply