I'm working on a weird problem for a project, and I need to do some complex masking. I have an image that looks like this:
The real images are larger and the shapes are irregular, but there is always a black outline on a white background and then more black area inside the outline. The inside shape may not be the same as the outside outline. What I need to do is cut out only the outside black outline, masking out the rest of the image.
The end result should look like this:
I would like to do this without specifying any coordinates for the different areas. The only guarantee is that the black area never touches the edge of the image. Is this possible?
Complex masking situation
-
- Posts: 2
- Joined: 2011-06-08T13:12:18-07:00
- Authentication code: 8675308
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Complex masking situation
try this
infile="boxes.gif"
coords=`convert $infile -format "%[fx:w/2],%[fx:h/2]" info:`
echo $coords
convert $infile -fuzz 1% -fill white -draw "color $coords floodfill" -transparent white boxes_border.gif
vary the fuzz XX% as much as needed if not pure black and white, ie. if antialiased so the edges are gray. If pure black and white, then -fuzz is not needed.
Note this does not seem to work right in IM 6.7.0.5 Q16 with png output. I will report the bug.
if in Windows, see differences at http://www.imagemagick.org/Usage/windows/
infile="boxes.gif"
coords=`convert $infile -format "%[fx:w/2],%[fx:h/2]" info:`
echo $coords
convert $infile -fuzz 1% -fill white -draw "color $coords floodfill" -transparent white boxes_border.gif
vary the fuzz XX% as much as needed if not pure black and white, ie. if antialiased so the edges are gray. If pure black and white, then -fuzz is not needed.
Note this does not seem to work right in IM 6.7.0.5 Q16 with png output. I will report the bug.
if in Windows, see differences at http://www.imagemagick.org/Usage/windows/
-
- Posts: 2
- Joined: 2011-06-08T13:12:18-07:00
- Authentication code: 8675308
Re: Complex masking situation
Yep, this works, assuming that the center area always covers the center pixel. In the end, that may not be the case, but we can cross that bridge if we run into it.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Complex masking situation
This is where the color of the -floodfill operator comes in useful. If the pixel is NOT the expected matching color, floodfill does nothing. It aborts gracefully in unexpected situations.
http://www.imagemagick.org/Usage/color_ ... #floodfill
Draw flood fill (immediately above in the given link) will always floodfill the specified area, regardless of its color.
http://www.imagemagick.org/Usage/color_ ... #floodfill
Draw flood fill (immediately above in the given link) will always floodfill the specified area, regardless of its color.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/