Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Batch Background Removal
#9
(04-19-2019, 02:12 PM)Ofnuts Wrote:
(04-19-2019, 01:49 PM)ct197475 Wrote:
(04-19-2019, 09:24 AM)Ofnuts Wrote: https://stackoverflow.com/questions/4443...0#44435560

Thanks Ofnuts for this resource but where can I find the command that creates the action of getting color tonalphaband then setting Opacity and Transparency thresold ?

Sorry my knowledge is poor in gimp and Puthon

Filters>Python-fu>Console, and hit the Browse button. It's all there, use the search bar. The old Color-to-alpha (the one you can use via Python-fu) has no Transparency/Opacity thresholds, but I don't think it matters much in your case. In a script, I would:

* duplicate the layer,
* increase contrast and tweak the brightness so that everything you want to remove is white.
* apply color-color-alpha and remove the white
* alpha-select the result (IIRC, gimp_select_item() the layer)
* add a layer mask to the initial image, using the selection to initialize the mask
* delete the copy


Hi Ofnuts,

I am really struggling here !-) I think I need your kind expertise. I have found this great piece of code you did formerly; we could start from that to code what you defined here above?

* duplicate the layer, 
* increase contrast and tweak the brightness so that everything you want to remove is white. 
* apply color-color-alpha and remove the white
* alpha-select the result (IIRC, gimp_select_item() the layer)
* add a layer mask to the initial image, using the selection to initialize the mask
* delete the copy

So can you please check the amended code?

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

# Erase the background of a layer, preserving the anti-aliasing of edge pixels

# (c) Ofnuts 2016
#
#   History:
#
#   v0.0: 2016-10-30: Initial version
#   v0.1: 2016-10-30: Sharpen selection before use
#   v0.2: 2016-10-31: Replace color-to-alpha with bucket-fill in color-erase mode
#   v0.3: 2016-10-31: Remove the pixelize step to figure out background color since it seems
#                     to hit a possible Gimp bug in Windows versions. 
#   v0.4: 2016-11-01: Add hint for background color in pop-up menu 
#   v0.5: 2018-03-30: Guess the color using the median from the histogram 
#   v0.6: 2018-03-31: Add edge options 
#
#   This program 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.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import os,sys,math,time
from gimpfu import *


def eraseBackground2(image,layer):
    image.undo_group_start()
    gimp.context_push()
    try:
        # Duplicate the layer
        pdb.gimp_layer_copy(layer, add_alpha)
        
        #access its drawable
        drw = pdb.gimp_image_active_drawable(img)
        
        # increase contrast and tweak the brightness so that everything you want to remove is white.
        pdb.gimp_brightness_contrast(drawable, brightness+10, contrast+127)
        
        # apply color-to-alpha and remove the white
        pdb.plug_in_colortoalpha(image, drawable, color)
    
        # alpha-select the result (IIRC, gimp_select_item() the layer)
        
        ?????

        # add a layer mask to the initial image, using the selection to initialize the mask
        pdb.gimp_image_add_layer_mask(image, layer, mask)
    
        # delete the copy
        ????
        
        # Check selection and save it to channel
        hasSelection,x1,y1,x2,y2 = pdb.gimp_selection_bounds(image)
        if not hasSelection:
            raise Exception('No selection found.')
        savedSelection=image.selection.copy()
        image.add_channel(savedSelection)
        
        # make sure we have an alpha channel
        pdb.gimp_layer_add_alpha(layer)

               
        # bluntly delete most of the background
        pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, savedSelection)
        pdb.gimp_selection_sharpen(image)
        pdb.gimp_selection_shrink(image,2)
        pdb.gimp_edit_clear(layer)
        
        # apply color to alpha on the edges
        pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, savedSelection)
        pdb.gimp_selection_grow(image,edgeStates[edgeState][1])
        pdb.gimp_edit_bucket_fill(layer,BG_BUCKET_FILL,COLOR_ERASE_MODE,100,0,False,0,0)
        
        #  cleanup
        pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, savedSelection)
        image.remove_channel(savedSelection)
        image.active_layer=layer
               
    except Exception as e:
        print e.args[0]
        gimp.message(e.args[0])

    gimp.context_pop()
    image.undo_group_end()


### Registration
whoiam='\n'+os.path.abspath(sys.argv[0])
author='Ofnuts'
copyrightYear='2018'
desc='Erase the layer background while preserving the anti-aliasing on edge pixels'
moredesc='\nWand-select (roughly) the background before use'
register(
    'ofn-erase-background',
    desc+moredesc+whoiam,desc,
    author,author,copyrightYear,
    'Erase background',
    'RGB*',
    [
        (PF_IMAGE, 'image', 'Input image', None),
        (PF_DRAWABLE, 'drawable', 'Input drawable', None),
        (PF_OPTION, 'color', 'Delete color', 0,[x[0] for x in colorOrigins]),
        (PF_OPTION, 'edge', 'Edges', 0,[x[0] for x in edgeStates]),
    ],
    [],
    eraseBackground,
    menu='<Image>/Layer'
)

main()




If I am right the code to batch process with the name of my batch file would be?
Code:
gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.eraseBackground('C:/Users/chris/Desktop/CHRIS/MABONNEAMIE/Site/Catalogue/00 Images Produits/00 Images Shooting Originale png')" -b "pdb.gimp_quit(1)
Thanks a lot in advance,
Chris
Reply


Messages In This Thread
Batch Background Removal - by ct197475 - 04-18-2019, 11:51 AM
RE: Batch Background Removal - by Ritergeek - 04-18-2019, 01:36 PM
RE: Batch Background Removal - by Ofnuts - 04-18-2019, 08:10 PM
RE: Batch Background Removal - by Ritergeek - 04-19-2019, 12:37 AM
RE: Batch Background Removal - by ct197475 - 04-19-2019, 07:05 AM
RE: Batch Background Removal - by Ofnuts - 04-19-2019, 09:24 AM
RE: Batch Background Removal - by ct197475 - 04-19-2019, 01:49 PM
RE: Batch Background Removal - by Ofnuts - 04-19-2019, 02:12 PM
RE: Batch Background Removal - by ct197475 - 04-22-2019, 01:01 PM
RE: Batch Background Removal - by Terry50 - 04-23-2019, 06:17 AM
RE: Batch Background Removal - by Ofnuts - 04-23-2019, 09:19 AM
RE: Batch Background Removal - by Ofnuts - 04-22-2019, 07:55 PM
RE: Batch Background Removal - by ct197475 - 04-23-2019, 09:10 AM
RE: Batch Background Removal - by Ofnuts - 04-23-2019, 09:46 AM
RE: Batch Background Removal - by ct197475 - 04-23-2019, 09:49 AM

Forum Jump: