GIMP pythonfu get drawable from current image

1.2k Views Asked by At

I am iterating over all open images. I want to apply threshold on each of them. Since the threshold plugin takes drawable as argument, the drawable should be changed every time the image is changed, at least that's what programatically makes sense. So, how do I get the drawable from the current image?

    for subimage in gimp.image_list():
        pdb.gimp_undo_push_group_start(subimage)
        pdb.gimp_drawable_set_image(drawable, subimage)
        pdb.plug_in_randomize_pick(image,drawable,rndm_pct, rndm_rcount, randomize, seed)
        spread_amount_x, spread_amount_y = 3, 3
        # pdb.plug_in_spread(subimage,drawable,spread_amount_x, spread_amount_y)
        pdb.plug_in_spread(subimage,drawable,spread_amount_x, spread_amount_y)
        pdb.plug_in_gauss_rle2(subimage,drawable,horizontal, vertical)
        # pdb.gimp_threshold(drawable, low_threshold, high_threshold)    
        pdb.gimp_undo_push_group_end(subimage)

Note that the commented out threshold would repeatedly apply the threshold on the selected image rather than all the images looped in. In order for me to do that, I have to switch to the drawable of the image in context.

1

There are 1 best solutions below

0
On BEST ANSWER

If the images have a single layer, then either:

drawable=subimage.layers[0]

or

drawable=subimage.active_layer

If you have several layers, then you can use an index (the top layer is layers[0], the bottom/background layer is layers[-1]), or you figure out the layer by other means, for instance be something like:

drawable=[d for d in image.layers if d.name=='Target'][0]

PS: Your script is likely going to run faster and use less RAM if you freeze the undo stack (or possibly even disable undo entirely) instead of doing undo-groups.