Compare commits
12 Commits
main
...
44a0847efa
| Author | SHA1 | Date | |
|---|---|---|---|
| 44a0847efa | |||
|
|
6a29c7d05c | ||
|
|
8060eb115b | ||
|
|
a26a28293e | ||
|
|
9c55110585 | ||
|
|
5a47832ad3 | ||
|
|
a30bba7907 | ||
|
|
ee660b34e6 | ||
|
|
0deee1f944 | ||
|
|
06f66a3df1 | ||
|
|
c837f83481 | ||
|
|
06540bde9b |
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user