Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove plain background, set it transparent
#2
(04-22-2021, 10:07 PM)bobJ Wrote: Hello,

I have 1500 png to process so I'm searching a script/plugin to do this.

Given a png, I would like the script to do the following:
- do a magic selection (wand/fuzzy) with starting point coordinattes 1,1 (I know this pixel is the background color)
- remove this selection and have transparency instead

I tried this script in a plugin via BIMP:
Code:
def plugin_use_fuzzy(timg, tdrawable):
   
    pdb.gimp_layer_add_alpha(timg)
    pdb.gimp_image_select_contiguous_color(timg, 0, tdrawable, 1, 1)
    sel = pdb.gimp_image_get_selection(timg)
    pdb.gimp_edit_cut(sel)
But it does nothing.
Could you help me please?

The parameter to gimp_edit_cut() should be your layer (the selection is implicit, but if you have several layers, Gimp needs to know which...). Since you don't do anything with what you remove, you can use gimp_edit_clear() instead. Likewise, gimp_layer_add_alpha() takes a layer. The gimp_image_get_selection() step is not necessary. To debug your Python scripts under Windows, see this.

Now, removing the background by Select/Delete is not very correct, you get a remaining halo of the background color. A more proper way is to select the background, grow the selection by one or two pixels, and then use Color-to-Alpha. For some code that does this see for instance my ofn-erase-background script.

But if you need to process files in batch it is usually easier to use ImageMagick from a shell script. For an IM equivalent to Color-to-Alpha, see this.

If you want to go the Gimp route, you can steal the core code in my plugin above and make it work in a batch that processes a whole directory by following this example.
Reply


Messages In This Thread
RE: Remove plain background, set it transparent - by Ofnuts - 04-22-2021, 10:42 PM

Forum Jump: