Gimp-Forum.net
Batch Background Removal - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+--- Thread: Batch Background Removal (/Thread-Batch-Background-Removal)

Pages: 1 2


Batch Background Removal - ct197475 - 04-18-2019

Hi There,

After having scanned despairately the net to find a simple solution (and found out this place from 
http://gimp-tools.sourceforge.net/index.shtml and previously https://sourceforge.net/projects/gimp-tools/)

I am asking help for this problem I have: a solution to remove the background of 300 dresses. 

Here are the specs
- in jpg 
- the background is not evenly white unfortunaltely

I did not find any script doing it even roughly on the net (did I miss something after 4h of search ???[Image: biggrin.png])

I have implemented Bimp but am blocked by not being able in Bimp to create a alpha channel from a jpg photo.

➤ Has anybody the procedure on Bimp ?
➤ or a beautiful script?

Thanks for your help !

Chris


RE: Batch Background Removal - Ritergeek - 04-18-2019

If I understand right, you need to add an alpha channel to these images. Have you tried right-clicking your base image and adding an alpha channel? JPG images did not load with an alpha channel until 2.10.10, which released a few days ago. Now you can set GIMP to automatically add an alpha channel to jpg images. The switch is in Preferences>Image Import & Export >Import Policies.


RE: Batch Background Removal - Ofnuts - 04-18-2019

Removing a background is still a relatively labor-intensive process in the general case. You can automate it quite a bit if you take the photographs with that in mind: good lighting, contrasting and uniform background as a minimum. But in the pictures above, even a human has trouble distinguishing the mannequin from the background.

Otherwise you can offshore the work to sweat shops in Asia, the rates aren't that high and these folks are likely more trained than you, and this can be less expensive than hiring a trained photographer locally to redo the shorts properly.

PS: with that link above, it looks like I'm spamming my own forum Smile


RE: Batch Background Removal - Ritergeek - 04-19-2019

(04-18-2019, 08:10 PM)Ofnuts Wrote: Removing a background is still a relatively labor-intensive process in the general case. You can automate it quite a bit if you take the photographs with that in mind: good lighting, contrasting and uniform background as a minimum. But in the pictures above, even a human has trouble distinguishing the mannequin from the background.

Otherwise you can offshore the work to sweat shops in Asia, the rates aren't that high and these folks are likely more trained than you, and this can be less expensive than hiring a trained photographer locally to redo the shorts properly.

PS: with that link above, it looks like I'm spamming my own forum Smile

Spam away Ofnuts, if that's what it is. Many of us are new enough not to know of your rich treasure trove.


RE: Batch Background Removal - ct197475 - 04-19-2019

Ofnuts Wrote:Removing a background is still a relatively labor-intensive process in the general case. You can automate it quite a bit if you take the photographs with that in mind: good lighting, contrasting and uniform background as a minimum. But in the pictures above, even a human has trouble distinguishing the mannequin from the background.

Otherwise you can offshore the work to sweat shops in Asia, the rates aren't that high and these folks are likely more trained than you, and this can be less expensive than hiring a trained photographer locally to redo the shorts properly.

PS: with that link above, it looks like I'm spamming my own forum Smile



Thanks a lot Ofnuts for your swift answer proving Gimp is a real alternative; 
yes I believe in automation;-) and I want to be autonomous on this matter if possible;

I have discovered that the "alfa to color" function with Threshold and Opacity set to 50% create a realy good job even in complicated background!

➤ I am missing the code though to batch process it as the Bimp in Gimp does not offer to deal with those functionality.

Would you have such a code that I could adapt to the file where I have 500 pictures to handle ?

Very best,

Chris

(04-18-2019, 01:36 PM)Ritergeek Wrote: If I understand right, you need to add an alpha channel to these images. Have you tried right-clicking your base image and adding an alpha channel? JPG images did not load with an alpha channel until 2.10.10, which released a few days ago. Now you can set GIMP to automatically add an alpha channel to jpg images. The switch is in Preferences>Image Import & Export >Import Policies.

Thanks Ritergeek for your answer! As I did not know it I made a Bimp on it;-) took ages ;-)

I have discovered that the "alfa to color" function with Threshold and Opacity set to 50% create a realy good job even in complicated background!

➤ I am missing the code though to batch process it as the Bimp in Gimp does not offer to deal with those functionality.

Would you have such a code that I could adapt to the file where I have 500 pictures to handle ?

Very best,

Chris


RE: Batch Background Removal - Ofnuts - 04-19-2019

(04-19-2019, 07:05 AM)ct197475 Wrote: Thanks a lot Ofnuts for your swift answer proving Gimp is a real alternative; 
yes I believe in automation;-) and I want to be autonomous on this matter if possible;

I have discovered that the "alfa to color" function with Threshold and Opacity set to 50% create a realy good job even in complicated background!

➤ I am missing the code though to batch process it as the Bimp in Gimp does not offer to deal with those functionality.

Would you have such a code that I could adapt to the file where I have 500 pictures to handle ?

https://stackoverflow.com/questions/44430081/how-to-run-python-scripts-using-gimpfu-from-windows-command-line/44435560#44435560


RE: Batch Background Removal - ct197475 - 04-19-2019

(04-19-2019, 09:24 AM)Ofnuts Wrote:
(04-19-2019, 07:05 AM)ct197475 Wrote: Thanks a lot Ofnuts for your swift answer proving Gimp is a real alternative; 
yes I believe in automation;-) and I want to be autonomous on this matter if possible;

I have discovered that the "alfa to color" function with Threshold and Opacity set to 50% create a realy good job even in complicated background!

➤ I am missing the code though to batch process it as the Bimp in Gimp does not offer to deal with those functionality.

Would you have such a code that I could adapt to the file where I have 500 pictures to handle ?

https://stackoverflow.com/questions/44430081/how-to-run-python-scripts-using-gimpfu-from-windows-command-line/44435560#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


RE: Batch Background Removal - Ofnuts - 04-19-2019

(04-19-2019, 01:49 PM)ct197475 Wrote:
(04-19-2019, 09:24 AM)Ofnuts Wrote:
(04-19-2019, 07:05 AM)ct197475 Wrote: Thanks a lot Ofnuts for your swift answer proving Gimp is a real alternative; 
yes I believe in automation;-) and I want to be autonomous on this matter if possible;

I have discovered that the "alfa to color" function with Threshold and Opacity set to 50% create a realy good job even in complicated background!

➤ I am missing the code though to batch process it as the Bimp in Gimp does not offer to deal with those functionality.

Would you have such a code that I could adapt to the file where I have 500 pictures to handle ?

https://stackoverflow.com/questions/44430081/how-to-run-python-scripts-using-gimpfu-from-windows-command-line/44435560#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


RE: Batch Background Removal - ct197475 - 04-22-2019

(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/44430081/how-to-run-python-scripts-using-gimpfu-from-windows-command-line/44435560#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


RE: Batch Background Removal - Ofnuts - 04-22-2019

Given the question marks, you haven't really understood how my procedure works. The fundamental idea is that your source image hasn't enough contrast to do a useful color-erase. But we can;


1) make a copy of that layer and increase the contrast on it so that the background becomes white
2) color-erase the background on that over-contrasted copy.

We now have a cut-out, over-contrasted image and the initial image. We wan to transfer the transparency of the over-contrasted image to the initial image, so

3) create a selection from the opacity of the cut-out image (in the UI, that's Alpha to selection, but in a script, that's gimp_select_item(layer))
4) use the selection to create a layer mask for the initial image

Then we are done, we just need to

5) erase the over-contrasted image, that has served its purpose.
6) export the image to file (in a format that supports transparency, so PNG or WEBP).

As to your code:

1) please remove any reference to me, that's your code...
2) no need to fetch the active drawable since this is exactly what is passed to you as the layer argument.
3) when you duplicate the layer, the function returns a reference to the copy, keep it safely, it will be useful later
4) brightness-contrast needs two plain numbers, you use brightness and contrast variables out of the blue...