Remove plain background, set it transparent - 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) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Remove plain background, set it transparent (/Thread-Remove-plain-background-set-it-transparent) |
Remove plain background, set it transparent - bobJ - 04-22-2021 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): Could you help me please? RE: Remove plain background, set it transparent - Ofnuts - 04-22-2021 (04-22-2021, 10:07 PM)bobJ Wrote: Hello, 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. RE: Remove plain background, set it transparent - bobJ - 04-23-2021 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): 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. RE: Remove plain background, set it transparent - Ofnuts - 04-23-2021 (04-23-2021, 07:10 AM)bobJ Wrote: Hi, 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). RE: Remove plain background, set it transparent - bobJ - 04-23-2021 Yes, my images are pixel art images, so they have pixellated edges. |