It is currently Wed Jul 22, 2026 6:29 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 50 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Fri Dec 16, 2022 11:44 am  (#31) 
Offline
New Member

Joined: Dec 13, 2022
Posts: 4
Thank you JamesH


Attachments:
File comment: This is the result I expected
my62-mod2.png
my62-mod2.png [ 688.1 KiB | Viewed 32174 times ]
Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Fri Dec 23, 2022 7:47 am  (#32) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Hi, I am able to remove the background using rembg CLI. But not through GIMP 2.10.25 AppImage using this plugin in Ubuntu 22.04.

The plugin works but nothing changes in the canvas. And, when I removed the part where it deletes the tmp folder entry I am able to see the input picture as-is in the tmp folder.

My guess is the rembg is not applied at all due to some reason.

Adding the plugin code I have. I modified it a bit to add my rembg entry and use the AppImage from /usr/bin


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# James Huang <[email protected]>
# https://elastic192.blogspot.com/

from gimpfu import *
import os, sys, string, tempfile
import platform

def python_fu_RemoveBG(image, drawable, asMask, AlphaMatting, aeValue):
   
   osName = platform.system()
   exportSep = str(os.sep)
   tdir = tempfile.gettempdir()
   #tdir = "./"
   print(tdir)
   jpgFile = "%s%sTemp-gimp-0000.jpg" % (tdir, exportSep)
   pngFile = "%s%sTemp-gimp-0000.png" % (tdir, exportSep)
   x1 = 0
   y1 = 0
   option = ""

   image.undo_group_start()
   curLayer = pdb.gimp_image_get_active_layer(image)

   if pdb.gimp_selection_is_empty(image):
      pdb.file_jpeg_save(image, drawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0)
   else:
      pdb.gimp_edit_copy(drawable)
      non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
      tmpImage = gimp.Image(x2-x1, y2-y1, 0)
      tmpDrawable = gimp.Layer(tmpImage, "Temp", tmpImage.width, tmpImage.height, RGB_IMAGE, 100, NORMAL_MODE)
      pdb.gimp_image_add_layer(tmpImage, tmpDrawable, 0)
      pat = pdb.gimp_context_get_pattern()
      pdb.gimp_context_set_pattern("Leopard")
      pdb.gimp_drawable_fill(tmpDrawable, 4)
      pdb.gimp_context_set_pattern(pat)
      pdb.gimp_floating_sel_anchor(pdb.gimp_edit_paste(tmpDrawable,TRUE))
      pdb.file_jpeg_save(tmpImage, tmpDrawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0)
      pdb.gimp_image_delete(tmpImage)
   
   aiExe = "/home/demo/.local/bin/rembg"
   if AlphaMatting:
      option = "-a -ae %d" % (aeValue)
   cmd = "gimp25.AppImage --host %s i %s %s %s" % (aiExe, option, jpgFile, pngFile)
   os.system(cmd)

   file_exists = os.path.exists(pngFile)
   if file_exists:
      newlayer = pdb.gimp_file_load_layer(image, pngFile)
         image.add_layer(newlayer, -1)
         pdb.gimp_layer_set_offsets(newlayer, x1, y1)
         if asMask:
            pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, newlayer)
            image.remove_layer(newlayer)
         copyLayer = pdb.gimp_layer_copy(curLayer, TRUE)
         image.add_layer(copyLayer, -1)
         mask=copyLayer.create_mask(ADD_SELECTION_MASK)
         copyLayer.add_mask(mask)
         pdb.gimp_selection_none(image)

      image.undo_group_end()
      gimp.displays_flush()

   

register(
    "python_fu_RemoveBG",
    "AI移除影像背景, AI Remove image background",
    "AI移除影像背景, AI Remove image background",
    "JamesH, <[email protected]>",
    "JamesH, https://elastic192.blogspot.com",
    "2022/6/4",
    "<Image>/Python-Fu/AI移除影像背景(AI Remove background) ...",
    "RGB*",
    [
       (PF_TOGGLE, "asMask", ("當作圖層遮罩(as Mask)"), True),
       (PF_TOGGLE, "AlphaMatting", ("alpha matting"), False),
       (PF_SPINNER,"aeValue", ("ALPHA_MATTING_ERODE_SIZE"), 15, (15,100,1))
    ],
    [],
    python_fu_RemoveBG,
    domain=("gimp20-python", gimp.locale_directory))

main()


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Fri Dec 23, 2022 7:48 am  (#33) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Hi, I am able to remove the background using rembg CLI. But not through GIMP 2.10.25 AppImage using this plugin in Ubuntu 22.04.

The plugin works but nothing changes in the canvas. And, when I removed the part where it deletes the tmp folder entry I am able to see the input picture as-is in the tmp folder.

My guess is the rembg is not applied at all due to some reason.

Adding the plugin code I have. I modified it a bit to add my rembg entry and use the AppImage from /usr/bin

Any help will be great.


Attachments:
RemoveBG.zip [1.42 KiB]
Downloaded 277 times
Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Tue Jan 17, 2023 3:35 am  (#34) 
Offline
GimpChat Member
User avatar

Joined: Apr 25, 2011
Posts: 121
Location: Taiwan
meetdilip wrote:
Hi, I am able to remove the background using rembg CLI. But not through GIMP 2.10.25 AppImage using this plugin in Ubuntu 22.04.

The plugin works but nothing changes in the canvas. And, when I removed the part where it deletes the tmp folder entry I am able to see the input picture as-is in the tmp folder.

My guess is the rembg is not applied at all due to some reason.


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 GIMP_AppImage-git-2.10.25-20210610-withplugins-x86_64.AppImage

_________________
Sorry for my poor English, I use Google Translate.
There may be something wrong, Please understand.

GIMP 2.10.38 rev1 portable+GAP+G'MIC+nufraw+Beautify+MathMap+...


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Tue Jan 17, 2023 5:11 pm  (#35) 
Offline
GimpChat Member
User avatar

Joined: Apr 15, 2017
Posts: 1837
Finally a RemBG plugin for Gimp that actually works. It works great in PPA installed Gimp, the other ones wouldn't work. I like the option of using Mask, much more useful that way.

Thanks JamesH for posting the last one.


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Sun Jan 22, 2023 6:06 pm  (#36) 
Offline
New Member

Joined: Jan 22, 2023
Posts: 1
This is pretty cool, thanks! I'll have to check out backgroundremover too.

In case anyone is still having trouble installing this on Windows, I wrote up a small guide as it took me a little while to figure out.

First make sure you have Python installed. You may need Python 3, I don't know.
Make sure you installed pip with it, added to PATH, etc.

Install rembg. Open a console window (Win+R, type "cmd" without quotes, hit enter) then type: "pip install rembg", without quotes.
It will download rembg and I assume dependencies.
Or follow instructions here: (github)/danielgatis/rembg
Sorry, not allowed to post urls...but if you can't find github, you need more help than I can give :)

Then, download RemoveBG.zip file attached from the first post. Unzip it and find the RemoveBG.py file.
Open this file with a text editor, like notepad.
Around line 42, or somewhere, you'll find something like this:
aiExe = "C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\rembg.exe"
As stated in the first post, change this to fit your path. You have to locate the rembg.exe executable. Mine was:
C:\Users\YOUR_USERNAME\AppData\Roaming\Python\Python310\Scripts\rembg.exe
But remember to add the extra backslashes.
C:\\Users\\SomeRando\\AppData\\Roaming\\Python\\Python310\\Scripts\\rembg.exe
Don't forget to save your changes.

Now you have to put RemoveBG.py in the plugins directory. You can try:
C:\Users\USERNAME\AppData\Roaming\GIMP\2.10\plug-ins
If you can't find it, Open GIMP, Click Edit --> Preferences, expand the Folders section, click Plugins.
You should see at least one folder where you can put plugins. Select one, then find the button on the right which will open that folder in your file manager.
Move the RemoveBG.py file into the plugins directory. You may need to restart GIMP.

Now you're done, right? Eh, not quite. Open an image in GIMP and run this plugin. On first run, rembg.exe will need to download u2net.onxx, around 180MB file; let it do its thing, then you're done.
If for whatever reason you want to do this step manually, the file should be at:
(github)/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx
Place it here: C:\Users\YOUR_USERNAME\.u2net\u2net.onnx

Hope this helped!


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Sat Feb 18, 2023 3:47 pm  (#37) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Hi, I added the .py file in plugin folder for both AppImage and Flatpak (and run as instructed). But I do not see any option to use rembg inside GIMP. Where can I find it? Thanks.

Edit 1:

Tried CLI, it is working. I am on Ubuntu 22.04


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Fri May 19, 2023 7:40 am  (#38) 
Offline
GimpChat Member
User avatar

Joined: Jan 06, 2020
Posts: 401
Location: The Netherlands
I was so happy with remove background, but somehow he doesn't do it anymore. Can it still be repaired?


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Sep 11, 2023 4:33 am  (#39) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Hi, I tried rembg install on a new Ubuntu. Sadly, it is giving the following error

$ rembg --help
Traceback (most recent call last):
  File "/home/myPC/.local/bin/rembg", line 5, in <module>
    from rembg.cli import main
  File "/home/myPC/.local/lib/python3.10/site-packages/rembg/__init__.py", line 5, in <module>
    from .bg import remove
  File "/home/myPC/.local/lib/python3.10/site-packages/rembg/bg.py", line 6, in <module>
    from cv2 import (
ImportError: cannot import name 'BORDER_DEFAULT' from 'cv2' (unknown location)


Not sure what is wrong.

Started a bug report as well

https://github.com/danielgatis/rembg/issues/515

Any help will be great. Thanks


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Sep 11, 2023 5:12 am  (#40) 
Offline
GimpChat Member
User avatar

Joined: Nov 04, 2015
Posts: 1459
Looks like you need opencv-python. You may have to trick it into believing it's installed :)


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Sep 11, 2023 9:17 am  (#41) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Thanks Tas_mania

I got stuck here though

Quote:
$ mkdir ~/.conda/envs/orion/lib/python3.8/opencv_python-3.4.11.dist-info
mkdir: cannot create directory ‘/home/myPC/.conda/envs/orion/lib/python3.8/opencv_python-3.4.11.dist-info’: No such file or directory


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Sep 11, 2023 9:32 am  (#42) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
I installed Open CV using

sudo apt install python3-opencv -y


but still getting an error

Quote:
rembg i /home/myPC/12.png /home/myPC/haa.png
Traceback (most recent call last):
File "/home/myPC/.local/bin/rembg", line 8, in <module>
sys.exit(main())
File "/home/myPC/.local/lib/python3.10/site-packages/rembg/cli.py", line 33, in main
_main() # type: ignore
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1257, in invoke
sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 700, in make_context
self.parse_args(ctx, args)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1048, in parse_args
value, args = param.handle_parse_result(ctx, opts, args)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1623, in handle_parse_result
value = self.full_process_value(ctx, value)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1965, in full_process_value
return Parameter.full_process_value(self, ctx, value)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1592, in full_process_value
value = self.get_default(ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1917, in get_default
return Parameter.get_default(self, ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1534, in get_default
return self.type_cast_value(ctx, rv)
File "/home/myPC/.local/lib/python3.10/site-packages/click/core.py", line 1561, in type_cast_value
return self.type(value or (), self, ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/types.py", line 46, in __call__
return self.convert(value, param, ctx)
File "/home/myPC/.local/lib/python3.10/site-packages/click/types.py", line 681, in convert
raise TypeError(
TypeError: It would appear that nargs is set to conflict with the composite type arity.


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Sep 11, 2023 9:42 am  (#43) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Solved using this trick. Commented out a few lines in i_command.py

https://github.com/danielgatis/rembg/is ... 1621412245


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Thu Nov 16, 2023 11:42 am  (#44) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
A different PC, installing on Windows 10. Added rembg, the GPU version + CLI, added the correct path and placed the plugin in plugins folder. Nothing under PythonFU. Restarted the computer, still nothing. Do we need to use some special command to start GIMP in Windows 10 to get the bg remove options? Thanks

Using 2.10.24


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Thu Nov 16, 2023 12:00 pm  (#45) 
Offline
GimpChat Member

Joined: Nov 19, 2019
Posts: 209
Found a solution. It is not working when added under program files. So added under AppData path. Now fine :)


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Nov 27, 2023 12:37 pm  (#46) 
Offline
GimpChat Member
User avatar

Joined: Oct 31, 2020
Posts: 2013
This plugin will soon have a Linux only GEGL counterpart that calls REMBG or any bash string from the users system in GEGL.

viewtopic.php?f=9&t=20652

On my machine for it to work I will have to
1.open the GEGL plugin.
2. type in a given import/export file name
3.insert the string
source ~/Applications/mc/etc/profile.d/conda.sh && conda activate base && rembg -p


Then REMBG will be inside GEGL due to it being a bash string. Though I must note Gimp will be frozen until REMBG loads, Stay tuned!


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Mon Nov 27, 2023 1:36 pm  (#47) 
Offline
GimpChat Member
User avatar

Joined: Sep 27, 2016
Posts: 345
Location: Germany, BW
@contrast_ that sounds really cool. :coolthup

_________________
Regards
nelo

(Gimp 2.10 on Linux Mint MATE 20.1)


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Fri Dec 01, 2023 2:57 pm  (#48) 
Offline
GimpChat Member
User avatar

Joined: Oct 31, 2020
Posts: 2013
outdated post


Last edited by contrast_ on Thu Dec 28, 2023 8:52 pm, edited 1 time in total.

Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Tue Dec 05, 2023 2:11 am  (#49) 
Offline
GimpChat Member
User avatar

Joined: Oct 31, 2020
Posts: 2013
REMBG now runs in GEGL and you guys can see it here.

viewtopic.php?f=9&t=20652&start=10


Top
 Post subject: Re: AI Remove image background(use rembg)
PostPosted: Thu Dec 28, 2023 3:53 am  (#50) 
Offline
GimpChat Member
User avatar

Joined: Oct 31, 2020
Posts: 2013
JamesH was the one who wrote the flatpak-spawn stuff but I edited it and got it working on the latest release of REMBG from late 2023. Please replace contrast with your username


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# James Huang <[email protected]>
# https://elastic192.blogspot.com/

from gimpfu import *
import os, sys, string, tempfile
import platform

def python_fu_RemoveBG(image, drawable, asMask, AlphaMatting, aeValue):
   removeTmpFile = True
   osName = platform.system()
   exportSep = str(os.sep)
   tdir = "/home/contrast/rembggimp"
   #tdir = "./"
   print(tdir)
   jpgFile = "%s%ssubjectR.jpg" % (tdir, exportSep)
   pngFile = "%s%ssubjectR.png" % (tdir, exportSep)
   x1 = 0
   y1 = 0
   option = ""

   image.undo_group_start()
   curLayer = pdb.gimp_image_get_active_layer(image)

   if pdb.gimp_selection_is_empty(image):
      pdb.file_jpeg_save(image, drawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0)
   else:
      pdb.gimp_edit_copy(drawable)
      non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
      tmpImage = gimp.Image(x2-x1, y2-y1, 0)
      tmpDrawable = gimp.Layer(tmpImage, "Temp", tmpImage.width, tmpImage.height, RGB_IMAGE, 100, NORMAL_MODE)
      pdb.gimp_image_add_layer(tmpImage, tmpDrawable, 0)
      pat = pdb.gimp_context_get_pattern()
      pdb.gimp_context_set_pattern("Leopard")
      pdb.gimp_drawable_fill(tmpDrawable, 4)
      pdb.gimp_context_set_pattern(pat)
      pdb.gimp_floating_sel_anchor(pdb.gimp_edit_paste(tmpDrawable,TRUE))
      pdb.file_jpeg_save(tmpImage, tmpDrawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0)
      pdb.gimp_image_delete(tmpImage)
   
   aiExe = "/home/contrast/.local/bin/rembg"
   if AlphaMatting:
      option = "-a -ae %d" % (aeValue)
   cmd = "flatpak-spawn --host %s i %s %s" % (aiExe, jpgFile, pngFile)
   os.system(cmd)

   file_exists = os.path.exists(pngFile)
   if file_exists:
      newlayer = pdb.gimp_file_load_layer(image, pngFile)
         image.add_layer(newlayer, -1)
         pdb.gimp_layer_set_offsets(newlayer, x1, y1)
         if asMask:
            pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, newlayer)
            image.remove_layer(newlayer)
         copyLayer = pdb.gimp_layer_copy(curLayer, TRUE)
         image.add_layer(copyLayer, -1)
         mask=copyLayer.create_mask(ADD_SELECTION_MASK)
         copyLayer.add_mask(mask)
         pdb.gimp_selection_none(image)

      image.undo_group_end()
      gimp.displays_flush()

   if removeTmpFile:
      if osName == "Windows":
         del_command = "del \"%s%ssubjectR.*\"" % (tdir, exportSep)
      else:
         del_command = "sleep 0.6 && rm %s%ssubjectR.*" % (tdir, exportSep)
      os.system(del_command)

register(
    "python_fu_RemoveBG",
    "AI移除影像背景, AI Remove image background",
    "AI移除影像背景, AI Remove image background",
    "JamesH, <[email protected]>",
    "JamesH, https://elastic192.blogspot.com",
    "2022/6/4",
    "<Image>/REMBG/AI移除影像背景(AI Remove background) ...",
    "RGB*",
    [
       (PF_TOGGLE, "asMask", ("當作圖層遮罩(as Mask)"), False),
       (PF_TOGGLE, "AlphaMatting", ("alpha matting"), False),
       (PF_SPINNER,"aeValue", ("ALPHA_MATTING_ERODE_SIZE"), 10, (10,100,1))
    ],
    [],
    python_fu_RemoveBG,
    domain=("gimp20-python", gimp.locale_directory))

main()


Top
Post new topic Reply to topic  [ 50 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group