Search code examples
selectiongimpdirectionshrinkscript-fu

How to shrink selection from two sides in GIMP script?


I need to shrink selection in my GIMP script.

Default shrink selection command shrinks the whole selection by given size value in pixels.

(gimp-selection-shrink inImage 2)

This would shrink selection by 2 pixels.

But I don't want to shrink the whole selection. I just want to remove selection from left and right side, keeping vertical selection unmodified.

That requires removing the farthest pixels on each side (left and right) from selection mask.

How can I do that?


Script desired effect for value "3px":

https://media.giphy.com/media/l3q2v50vsk8Ze8Ldu/source.gif

As you can see, it's quite rectangular on left and right side and it's required for user to make a selection like this.

So I think a good approach would be to get left and right posX of selection mask and cut the rect with height of the layer and width of pixels we want to cut off on each side of selection mask.

I'd like to use Script-Fu instead of Python-Fu, because I don't like have extra requirements for the users.


Solution

    • Save your selection
    • Get the bounding box (gimp-selection-bounds)
    • Shrink that box (+2px to Xmin, -4px to width)
    • Create a rectangle selection from these new values
    • Intersect with saved selection