Posts: 3
Threads: 1
Joined: Apr 2021
Reputation:
0
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?
Posts: 6,343
Threads: 274
Joined: Oct 2016
Reputation:
564
Operating system(s):
Gimp version: 2.10
(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.
Posts: 3
Threads: 1
Joined: Apr 2021
Reputation:
0
Hi,
Thank you for your help.
I've finally got to remove my plain background using this as a plugin in BIMP:
Code:
def plugin_remove_plain_bg(timg, tdrawable):
drawable = pdb.gimp_image_active_drawable(timg)
pdb.gimp_image_select_color(timg, CHANNEL_OP_REPLACE, drawable, gimpcolor.RGB(149,85,79)) # 95554f
pdb.gimp_edit_cut(drawable)
I was able to do this by selecting by color because my background is plain color.
Bonus: some isolated pixels had this color and were not contiguous with the background, so removing the entire color does the job the best way for me.
Posts: 6,343
Threads: 274
Joined: Oct 2016
Reputation:
564
Operating system(s):
Gimp version: 2.10
(04-23-2021, 07:10 AM)bobJ Wrote: Hi,
Thank you for your help.
I've finally got to remove my plain background using this as a plugin in BIMP:
Code:
def plugin_remove_plain_bg(timg, tdrawable):
drawable = pdb.gimp_image_active_drawable(timg)
pdb.gimp_image_select_color(timg, CHANNEL_OP_REPLACE, drawable, gimpcolor.RGB(149,85,79)) # 95554f
pdb.gimp_edit_cut(drawable)
I was able to do this by selecting by color because my background is plain color.
Bonus: some isolated pixels had this color and were not contiguous with the background, so removing the entire color does the job the best way for me.
Your background isn't totally a plain color or your object has pixellated edges (or only vertical/horizontal ones). Smooth edges are otherwise obtained with pixels in which the subject color and the background color are accurately blended. Depending on threshold these pixels are not selected by the background selection (leaving a halo) or are selected (making the edge pixellated).
Posts: 3
Threads: 1
Joined: Apr 2021
Reputation:
0
Yes, my images are pixel art images, so they have pixellated edges.
|