gimppython-fu

Gimp python-fu: How to crop layer to selection


What is the GIMP API call to crop a layer to a selection, equivalent to Layer -> Crop to Selection in the GUI?

I looked in the Procedure Browser but the calls I found (gimp-crop and gimp-image-crop) perform the crop on the image, not a layer.

(What I really want to do is cut-and-paste multiple layers at once; I'm making a plug-in to help.)


Solution

  • You use pdb.gimp_layer_resize() using the data from pdb.gimp_selection_bounds(image).

    x0,y0 = pdb.gimp_drawable_offsets(layer)
    non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
    pdb.gimp_layer_resize(layer,x2-x1,y2-y1,x0-x1,y0-y1)