0

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 ?

DevonDahon
  • 7,460
  • 6
  • 69
  • 114
  • 3
    Sure, use `-morphology dilate disk:3` or similar to dilate the white. – Mark Setchell Jan 22 '19 at 16:52
  • 2
    Imagemagick `-morphology dilate disk:3` will shrink the black while `-morphology erode disk:3` will enlarge the black. Or you can use `open` and `close` to do both. See https://imagemagick.org/Usage/morphology/ – fmw42 Jan 22 '19 at 21:46

1 Answers1

1

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.

fmw42
  • 46,825
  • 10
  • 62
  • 80