Search code examples
imagemagickmaskgimpshrink

ImageMagick: Is there an equivalent of Gimp's tool to "shrink" and "grow" a selection?


I have a black and white mask image produced with this ImageMagick command:

convert in.jpg -threshold 85% out.png

Giving me this result:

enter image description here

I'd like to reduce the size of each piece like if I was doing it with Gimp by selecting the white background, inverting the selection and shrink it by X pixels.

Is it possible to do it with ImageMagick and if yes, how ?


Solution

  • In Imagemagick, you can use -morphology close to reduce the white holes in the black. But if you use too large a kernel size, it will start to merge the black regions together.

    convert image.png -morphology open octagon:4 result.png
    


    enter image description here

    Here you can see with size 4, it has removed all but one white hole, but has started to connect two of the black areas.