I am using Gimp's non-interactive mode, since I want to process a big number of images and their masks. For every image file, I have a mask image file, which consists of 1 and 0s (1 means the pixel should be selected). How do I select those in Python? I am looking at the reference here. What I see is that there is a function called gimp_selection_load
, which loads the selection from a channel. However, I don't know how to convert the mask image to a channel. I tried reading it with gimp_file_load
and then gimp_image_get_channels
, but there are no channels found in the mask image. What am I doing wrong?
Add a comment
|
1 Answer
The simplest way is to load your mask as a new layer, do a color selection from it, and then remove it. So assuming that image
is your current image:
import gimpcolor
# load mask as layer
mask=pdb.gimp_file_load_layer(image,'/path/to/mask.png')
pdb.gimp_image_add_layer(image, mask,0)
# get selection
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, mask,gimpcolor.RGB(0,0,0))
# drop the mask layer
pdb.gimp_image_remove_layer(image,mask)
-
"copy the layer to a channel in the initial image" how do I do that? Which function should I use? Commented Apr 26, 2018 at 7:11
-
1
-
Fantastic. I have been fighting with this for quite some time, and turned out that my main mistake was forgetting to remove the layer! It's great that you included a snippet. Commented Apr 26, 2018 at 10:09