 |
| 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()
|
|