Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rembg
#1
I have rembg installed locally on 22.04 and it is working fine through Terminal.

https://github.com/danielgatis/rembg

It would be nice to know how to use the GIMP plugin though. Any help will be great.

PS: I am using the GIMP AppImage.  

Thanks.

I added the file from GIMP Chat to the plugins folder for the AppImage, but I do not see any PythonFu entry in the menu

I think the script is for Flatpak and I am using AppImage

There is a line in the script which says

Code:
cmd = "flatpak-spawn --host %s i %s %s %s" % (aiExe, option, jpgFile, pngFile)

Not sure which is the right command for the AppImage

There is also a part which says remove temporary files


Code:
if removeTmpFile:
        if osName == "Windows":
            del_command = "del \"%s%sTemp-gimp-0000.*\"" % (tdir, exportSep)
        else:
            del_command = "rm %s%sTemp-gimp-0000.*" % (tdir, exportSep)
        os.system(del_command)

Is the right command for Ubuntu?

I replaced the Chinese text with English. Not sharing because I have no intention to remove parts of original code distribute.

Looks like I have to install Python2 to get gimpfu working. Because there is a line saying

from gimpfu import *

Tried installing Python 2 and it says

python2 is already the newest version (2.7.18-3).

Not sure how to get the PythonFu working inside GIMP AppImage. Maybe Flatpak version is the only option.
Reply
#2
I think the command to run the AppImage is simply

Code:
./GIMP.AppImage

maybe the path before it
Reply
#3
I do not have rmbg installed, a bit large / bloat for me and I try and keep a tight installation however;

Quote:...snip...I added the file from GIMP Chat to the plugins folder for the AppImage, but I do not see any PythonFu entry in the menu...

That depends on the appimage, old versions (2.10.22 / 25) from https://github.com/aferrero2707/gimp-appimage/releases/ have a self contained python 2.7

New version from https://github.com/ivan-hc/GIMP-appimage...continuous (Gimp 2.10.32) as with regular ubuntu release does not support python.

For running the appimage, instead of the more usual 'direct portable command' have you considered unpacking to a folder
Code:
./gimp.xxx.appimage --appimage-extract

..and running as a command something like this: /home/rich/Portable/gimp/squashfs-root/AppRun That is mine to start Gimp
Reply
#4
Thanks admin

The AppImage is 2.10.25

GIMP_AppImage-git-2.10.25-20210401-withplugins-x86_64.AppImage

I am not able to see the PythonFu entry at all.

I thought of replacing the " cmd = " right hand side in the plugin file using " ./AppImage ". Would be nice to know how to modify the lines to extract and then run. I am just a beginner with Python. Thanks.
Reply
#5
Thanks for the tips. I did as you said. But when I use the option under PythonFu, it works but nothing happens to the image

Code:
cmd = "/home/demo/Downloads/squashfs-root/AppRun --host %s i %s %s %s" % (aiExe, option, jpgFile, pngFile)

This is what I have in the line for cmd instead of the command for Flatpak.

This is what I have now

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

# James Huang <elastic192@gmail.com>
# 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 = 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 = "flatpak-spawn --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()

    if removeTmpFile:
        if osName == "Windows":
            del_command = "del \"%s%sTemp-gimp-0000.*\"" % (tdir, exportSep)
        else:
            del_command = "rm %s%sTemp-gimp-0000.*" % (tdir, exportSep)
        os.system(del_command)

register(
    "python_fu_RemoveBG",
    "AI Remove, AI Remove image background",
    "AI Remove, AI Remove image background",
    "JamesH, <elastic192@gmail.com>",
    "JamesH, https://elastic192.blogspot.com",
    "2022/6/4",
    "<Image>/Python-Fu/AI Remove(AI Remove background) ...",
    "RGB*",
    [
       (PF_TOGGLE, "asMask", ("As Mask (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()
Reply
#6
I have tried to install rmbg in a variety of 'buntu versions 22.04 / 20.04 / 18.04 without success always get a incompatible version error. That is me stuck.

The only thing I can suggest for your appimage is either put a symbolic link to it into /usr/bin or even the whole appimage file in /usr/bin and avoid the dot-slash

The appimage is the same as a regular Gimp, it will open files tacked on after the gimp command so it should work with those parameters in the script.

This an example running the appimage which is in /usr/bin and opening two image files. https://i.imgur.com/UjsBGOX.mp4

You are probably better off asking on gimpchat.
Reply
#7
Thanks Admin.

It is working now. But rembg is not acting. ie, the cycle completes but the background is not removed. Since nothing was happening, I delete the part where the plugin removes the copy from the tmp folder. And, I was able to see the input picture as is in the tmp folder.
Reply
#8
I would say if you are using the Daniel Gatis developed background remover then you don't need a Gimp plugin. If you want it for videos, this works for me. Convert video to PNGs using ffmpeg. Then use a bash script to remove their backgrounds with rembg via anaconda. There are lots of background removers on github. Some use an online server but I want to do it locally not upload and d'load thousands of images. Also get a clear image stream with a neutral background like on a beach. Or in the sky. The AI is not that smart Smile

I experimented with just removing a green screen from video clips. There are probably thousands of free to use online. This is easy to do in kdenlive but that app only makes videos Sad Its PNG export is just huge bloat.

   

Now I found a video editor where I can import a thousand PNGs, remove their backgrounds and then export them as PNGs. It's not FOSS so I won't post it's name here.
Reply
#9
Thanks @Tas_mania

I am comfortably using the CLI which can deal with one photo or a group of photos in a folder.

I was wondering how to use it with GIMP and define what to remove and what not.
Reply


Forum Jump: