Compare commits

...

12 Commits

Author SHA1 Message Date
44a0847efa Update python-fu-removebg/python-fu-removebg.py
Signed-off-by: maniu <maniu@pm.me>
2025-06-22 16:21:18 +02:00
Maniu
6a29c7d05c Update python-fu-removebg.py 2025-06-03 13:26:21 +02:00
Maniu
8060eb115b Update python-fu-removebg.py
add show_alert
2025-06-03 13:17:01 +02:00
Maniu
a26a28293e Update python-fu-removebg.py 2025-05-28 21:08:46 +02:00
Maniu
9c55110585 Update python-fu-removebg.py
find path for rembg
2025-05-28 20:35:12 +02:00
Maniu
5a47832ad3 Update python-fu-removebg.py 2025-05-28 19:38:16 +02:00
Maniu
a30bba7907 Update python-fu-removebg.py
fix mask part
2025-05-25 21:50:49 +02:00
Maniu
ee660b34e6 Update python-fu-removebg.py 2025-05-25 21:29:08 +02:00
Maniu
0deee1f944 Update python-fu-removebg.py 2025-05-25 21:15:08 +02:00
Maniu
06f66a3df1 Update python-fu-removebg.py 2025-05-25 21:14:16 +02:00
Maniu
c837f83481 Update python-fu-removebg.py 2025-05-25 20:51:18 +02:00
Marek Pistorius
06540bde9b Update python-fu-removebg.py
Rename pchannel_choice to model_choice to more represent of purpose of that variable
2025-04-15 14:24:15 +02:00

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (C) 2025 Marek Pistorius # (C) 2024-2026 ---
# This file is part of gimp-python-fu-removebg . # This file is part of gimp-python-fu-removebg .
# gimp-python-fu-removebg is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. # gimp-python-fu-removebg is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
# gimp-python-fu-removebg is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # gimp-python-fu-removebg is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
@@ -16,6 +16,8 @@ from gi.repository import GLib
from gi.repository import Gio from gi.repository import Gio
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from pathlib import Path
from typing import Any, Callable, List, Optional, Tuple, Union
import os, sys, string, tempfile, platform import os, sys, string, tempfile, platform
import subprocess import subprocess
@@ -23,6 +25,52 @@ def N_(message): return message
def _(message): return GLib.dgettext(None, message) def _(message): return GLib.dgettext(None, message)
tupleModel = ("u2net","u2net_human_seg", "u2net_cloth_seg", "u2netp", "silueta", "isnet-general-use", "isnet-anime", "sam", "birefnet-general" ,"birefnet-general-lite", "birefnet-portrait", "birefnet-dis", "birefnet-hrsod", "birefnet-cod", "birefnet-massive" ) tupleModel = ("u2net","u2net_human_seg", "u2net_cloth_seg", "u2netp", "silueta", "isnet-general-use", "isnet-anime", "sam", "birefnet-general" ,"birefnet-general-lite", "birefnet-portrait", "birefnet-dis", "birefnet-hrsod", "birefnet-cod", "birefnet-massive" )
REMBG_BASE_PATH = ""
def find_rembg_install() -> Optional[Path]:
home = sys.exec_prefix
possible_paths = []
# Common installation paths
if sys.platform == "win32":
possible_paths = [
Path("C:/Program Files (x86)/Rembg/"),
Path("C:/Program Files/Rembg/"),
]
for path in possible_paths:
if path.is_dir():
return path
# Fallback to user-configured path if specified
if REMBG_BASE_PATH and (nik_path := Path(REMBG_BASE_PATH)).is_dir():
return nik_path
show_alert(
text="rembg installation path not found",
message="Please specify the correct installation path 'REMBG_BASE_PATH' in the script.",
)
return None
def show_alert(text: str, message: str, parent=None) -> None:
"""Popup a message dialog with the given text and message"""
dialog = Gtk.MessageDialog(
transient_for=parent,
flags=0,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.CLOSE,
text=text,
)
dialog.format_secondary_text(message)
dialog.set_title(f"REMBG")
dialog.run()
dialog.destroy()
class PythonRemoveBG(Gimp.PlugIn): class PythonRemoveBG(Gimp.PlugIn):
## GimpPlugIn virtual methods ## ## GimpPlugIn virtual methods ##
def do_set_i18n(self, procname): def do_set_i18n(self, procname):
@@ -39,8 +87,8 @@ class PythonRemoveBG(Gimp.PlugIn):
procedure.set_documentation (_("Remove Background AI"), procedure.set_documentation (_("Remove Background AI"),
_("Remove Background AI"), _("Remove Background AI"),
name) name)
procedure.set_menu_label(_("RemoveBG..")) procedure.set_menu_label(_("RemoveBG"))
procedure.set_attribution("MP", procedure.set_attribution("M",
"(c) GPL V3.0 or later", "(c) GPL V3.0 or later",
"2024-2025") "2024-2025")
procedure.add_menu_path('<Image>/Filters/RemoveBG/') procedure.add_menu_path('<Image>/Filters/RemoveBG/')
@@ -50,25 +98,25 @@ class PythonRemoveBG(Gimp.PlugIn):
procedure.add_double_argument("aeValue","aeValue","aeValue",0,100,15 ,GObject.ParamFlags.READWRITE) procedure.add_double_argument("aeValue","aeValue","aeValue",0,100,15 ,GObject.ParamFlags.READWRITE)
pchannel_choice = Gimp.Choice.new() model_choice = Gimp.Choice.new()
pchannel_choice.add("u2net",0,"u2net","") model_choice.add("u2net",0,"u2net","")
pchannel_choice.add("u2net_human_seg",1,"u2net_human_seg","") model_choice.add("u2net_human_seg",1,"u2net_human_seg","")
pchannel_choice.add("u2net_cloth_seg",2,"u2net_cloth_seg","") model_choice.add("u2net_cloth_seg",2,"u2net_cloth_seg","")
pchannel_choice.add("u2netp",3,"u2netp","") model_choice.add("u2netp",3,"u2netp","")
pchannel_choice.add("silueta",4,"silueta","") model_choice.add("silueta",4,"silueta","")
pchannel_choice.add("isnet-general-use",5,"isnet-general-use","") model_choice.add("isnet-general-use",5,"isnet-general-use","")
pchannel_choice.add("isnet-anime",6,"isnet-anime","") model_choice.add("isnet-anime",6,"isnet-anime","")
pchannel_choice.add("sam",7,"sam","") model_choice.add("sam",7,"sam","")
pchannel_choice.add("birefnet-general",8,"birefnet-general","") model_choice.add("birefnet-general",8,"birefnet-general","")
pchannel_choice.add("birefnet-cod",9,"birefnet-cod","") model_choice.add("birefnet-cod",9,"birefnet-cod","")
pchannel_choice.add("birefnet-massive",10,"birefnet-massive","") model_choice.add("birefnet-massive",10,"birefnet-massive","")
pchannel_choice.add("birefnet-portrait",11,"birefnet-portrait","") model_choice.add("birefnet-portrait",11,"birefnet-portrait","")
pchannel_choice.add("birefnet-general-lite",12,"birefnet-general-lite","") model_choice.add("birefnet-general-lite",12,"birefnet-general-lite","")
pchannel_choice.add("birefnet-hrsod",13,"birefnet-hrsod","") model_choice.add("birefnet-hrsod",13,"birefnet-hrsod","")
pchannel_choice.add("birefnet-dis",14,"birefnet-dis","") model_choice.add("birefnet-dis",14,"birefnet-dis","")
procedure.add_choice_argument ("Model", _("Model"), procedure.add_choice_argument ("Model", _("Model"),
_("Model"), pchannel_choice, _("Model"), model_choice,
"u2net", GObject.ParamFlags.READWRITE) "u2net", GObject.ParamFlags.READWRITE)
return procedure return procedure
@@ -131,7 +179,9 @@ class PythonRemoveBG(Gimp.PlugIn):
pdb.file_jpeg_save(tmpImage, tmpDrawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0) pdb.file_jpeg_save(tmpImage, tmpDrawable, jpgFile, jpgFile, 0.95, 0, 1, 0, "", 0, 1, 0, 0)
pdb.gimp_image_delete(tmpImage) pdb.gimp_image_delete(tmpImage)
aiExe = "C:\\Program Files (x86)\\Rembg\\rembg.exe" # aiExe = Path("C://Program Files (x86)/Rembg/rembg.exe")
aiExe = str(find_rembg_install())
aiExe += "/rembg.exe"
if AlphaMatting: if AlphaMatting:
option = "-a -ae %d" % (aeValue) option = "-a -ae %d" % (aeValue)
@@ -145,18 +195,14 @@ class PythonRemoveBG(Gimp.PlugIn):
newlayer = Gimp.file_load_layer(run_mode,image,fileOut) newlayer = Gimp.file_load_layer(run_mode,image,fileOut)
image.insert_layer(newlayer, None,-1) image.insert_layer(newlayer, None,-1)
file = Gio.File.new_for_path(pngFile)
newLayer = Gimp.file_load_layer(run_mode,image,fileOut)
image.insert_layer(newlayer, None,-1)
if asMask: if asMask:
image.select_item(Gimp.ChannelOps.REPLACE, newlayer) image.select_item(Gimp.ChannelOps.REPLACE, newlayer)
copyLayer = newlayer.copy()
image.remove_layer(newlayer) image.remove_layer(newlayer)
copyLayer = Gimp.layer_copy(curLayer)
image.insert_layer(copyLayer,None, -1) image.insert_layer(copyLayer,None, -1)
mask = copyLayer.create_mask(ADD_SELECTION_MASK) mask = copyLayer.create_mask(Gimp.AddMaskType.SELECTION)
copyLayer.add_mask(mask) copyLayer.add_mask(mask)
Gimp.selection_none(image) image.get_selection().none(image)
image.undo_group_end() image.undo_group_end()
Gimp.displays_flush() Gimp.displays_flush()