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 Answer 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
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
  • "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
    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
    Commented Apr 26, 2018 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. Commented Apr 26, 2018 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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