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?
* increase contrast and tweak the brightness so that everything you want to remove is white.
* add a layer mask to the initial image, using the selection to initialize the mask
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?