0

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?

1

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? – Hristo Vrigazov Apr 26 '18 at 7:11
  • 1
    The channel copy is more complicated that it seems. See edited answer for a a simpler solution. If you do batch scripts, some hints and sample code here. – xenoid Apr 26 '18 at 9:28
  • 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. – Hristo Vrigazov Apr 26 '18 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.