`4/11/22, 4:06 PM
`The Wayback Machine - https://web.archive.org/web/20120928070642/http://www.imagemagick.org:80/Usag…
`
`ImageMagick v6 Examples --
` Masks
`
`
`
`Index
` ImageMagick Examples Preface and Index
`Alpha (Matte) Channel
`
`Internal Matte Channel
`Alpha Channel Operator
`
`Off (or +matte), Set (or -matte), On,
`Opaque, Transparent, Extract, Copy,
`
`Shape, Remove, Background
`
`Remove Transparency of an Image
`Boolean Alpha Transparency
`Outline or Halo Transparency
`
`
`
` Using Masks with Images
`
`Editing a Image Mask
`Masks as Colored Shapes
`Mathematical Composition
`Masked Alpha Composition
`Aligning Two Masked Images (under construction)
`
` Special Image Masks
`
`Write Mask - Protect Pixels form Change
`Clip Mask and Clip Paths
`Read Masks - Ignore Pixel Input
`
` Regions and Region Sub-Images
`
`Warping a Local Region
`How Regions Work, and its Problems
`
` Background Removal
`
`Simple Backgrounds (floodfill)
`Masking Bordered Objects
`Removing a Known Background
`Difference Image Masking and Feathering
`Recovering Semi-Transparent Edges
`Background Removal using Two Backgrounds
`
` Hole_Filling (under construction)
`
`In these examples we look at the special handling of transparency, the transparency channel, using masks, and
`ultimatally the removal of unwanted backgrounds, or other elements, such as signs, text, spam.
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`1/35
`
`Lightricks Ltd., EX1006, Page 1 of 35
`
`
`
`4/11/22, 4:06 PM
`
`Alpha (matte) Channel
`
`Masks -- IM v6 Examples
`
`The transparency (alpha) channel of an image is completely optional, and often requires special handling
`separate to the normal 'color' channels. See Image Color Space above.
`
`The existance of a transparency channel can also effect how the various operators treat the other color channels,
`generally because a fully-transparent color should often be completely ignored by an operation. If this was not
`the case you get 'Black Halos' around images, such as was seen in major IM Bugs in the early days of IM v6. For
`example the Resize Halo Bug, and the Blur with Transparency Bug.
`
`To make matters worse, this channel is also sometimes referred to at an image's 'transparency' or 'opacity'
`channel, or even the image's 'mask'. All however refer to the same, special, fourth channel of the image.
`
`To explain the difference we need a working example image and for this I'll use a PNG image of a
`'crescent moon' image (from a CopyOpacity Composition example). Now as you can see this image
`has a lot of areas which are fully transparent. Not only that I needed to save the image using the
`'PNG' image format which is one of the small number of image formats that properly understands
`and handles transparent and semi-transparent colors.
`
`I can demonstrate this transparency by overlaying the image onto the IM built-in checkerboard
`pattern, using Alpha Composition.
`
` composite -compose Dst_Over -tile pattern:checkerboard \
` moon.png moon_background.jpg
`
`Internal Matte Channel
`
`Now internally IM saves the transparency information in a 'matte' channel, which just like the color channel is
`just a plain grey scale image of values which range from white, for fully-transparent (or clear), to black for fully-
`opaque. It is sort of like what you would get if you look at a silhouette of the original image.
`
`Here is the normal way to 'separate' the matte layer from the image.
`
` convert moon.png -channel matte -separate moon_matte.png
`
`You can also use "channel" options 'A' or 'alpha' the result will be the same, an extraction of the internal 'matte'
`channel of the image. As you more typically want a 'alpha' channel, this is not the recommended way of extracting an
`'alpha channel'.
`
`See Alpha Extract below.
`
`There is another way of other extracting the internal matte channel too, typically by saving the channel as a
`special 'matte' image file format.
`
` # Here is a IM version 5 way of extracting the matte (or other) channel
` # Note how this required two separate steps, and commands.
` convert moon.png matte:moon.matte
` convert MIFF:moon.matte moon_matte2.png
`
` # You can join those two steps in a pipeline as well...
` convert moon.png matte:- | convert - moon_matte3.png
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`2/35
`
`Page 2 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
`This technique for extracting the 'matte' of an image was common when IM v5 was in use. Basically it was the
`the only method provided to get access to the transparency of an image. It is now very rarely used.
`
`Now while the image transparency data is stored internally as 'matte' values, almost all the operators will deal
`with transparency as 'alpha'. As such on very low level operators such as "-level" and "-threshold" actually
`handle the data as 'matte' rather than alpha. Check the Official Option Reference if you are unsure.
`
`Controlling Image Transparency
`
`There are two operators that give you low-level control of the transparency channel of an image in memory. The
`newer operator "-alpha" methods are now the recommended method of control, though many IM Examples still
`show and use the older and now obsolete "-matte" operator.
`
`An image can not only have alpha channel data, but it also has a 'switch' that defines if the channel data is
`viewable or valid. This means images can have three states with regards to the alpha channel.
`
`Switch
` alpha off
` alpha off
` alpha on
`
`
`
`Channel Data
`no alpha data (no memory has been allocated)
`old alpha data present (but not in use)
`alpha data that is currently in use
`
`This needs to be remembered as how the various methods behave depends on which of the above three states the
`image was in.
`
`If the 'switch' is off operators will not touch the alpha data, as it may not actually exist at all. In such a case old
`alpha could still be present, unmodified, and thus out-of-date. As you will see this is actually sometime useful in
`some situations.
`
`Note however that some operators may automatically turn on, or turn off the alpha switch for one reason or
`another.
`
`For example, "-compose CopyOpacity -composite" will always turn on the alpha channel in the resulting image,
`as the operators job is to actually copy data into an alpha channel. as such it must exist in the final result.
`However its existance in the input data can have other consequences. See Copy_Opacity Composition Method
`for more details.
`
`Similarly creating a canvas using the color 'None' will also automatically create and enable the transparency
`channel, so as to ensure the blank image really is transparent. On the other hand, creating a canvas using some
`other Color Name will generally not create any transparency channel as images are opaque by default.
`
`Here are the various "-alpha" methods and examples of how they effect images and there transparency.
`
`Alpha Off or "+matte"
`
`This is just a simple switch on the image, which turns off any effect the transparency has on the image. It does
`not actually delete or destory the alpha channel attached to the image, it just turns off any effect that channel has
`on the image. Similarly no operator will effect the attached alpha channel while it has been turned off.
`
`For example lets the 'crescent moon' image (from a CopyOpacity Composition example), and simply turn the
`image alpha channel off.
`
` convert moon.png -alpha off alpha_off.png
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`3/35
`
`Page 3 of 35
`
`
`
`4/11/22, 4:06 PM
`
`Masks -- IM v6 Examples
`
`
`
`
`
`Note that the moon shape completely vanished when the transparency was turned off, though that is actually
`rarely the case. Basically even the 'transparent' areas have color, which is just not normally visible, in this case
`the hidden color was the fractal canvas image that was used to create the moon image.
`
`This hidden color could be anything, from a simple GIF Transparency Color, that the GIF format uses to
`represent transparency in its color table, to garbage colors left behind during the images creation, as above. More
`typically the transparency color is simply pure-black for any pixel that was fully-transparent. Note that pixels
`close to the edge may be semi-transparent, and thus still have a valid color that is only partially visible.
`
`Note that while the transparency channel has been 'deactivated' or 'turned off' like a simple switch, the data itself
`has not been cleared or removed from the image data stored in-memory. It is still present, just unavailable at this
`time.
`
`Be warned that saving the image with the transparency data turned off, will not save any transparency data to the
`image file format. In such a case the turned-off alpha data will then be lost.
`
`Also as many file formats do not allow transparency (such as JPEG), these file formats automatically do the
`equivalent of a "-alpha Off" when the image is saved. Generaly resulting in all transparent areas turning
`unexpectantally, black. See Alpha Remove - Removing Transparency below for the corect way to remove
`transparency before saving to a JPEG file format.
`
`The "+matte" operator is an older command that is exactly the same as "-alpha Off". That is it just turns off the
`transparency channel.
`
`Note that this method is often required when using a gray-scale mask image with the CopyOpacity Alpha
`composition method, so it copies the grayscale colors, rather than some existing alpha or opacity channel.
`
`Alpha Set or "-matte"
`
`The 'Set' alpha method is the same as the older "-matte" option.
`
`This ensures that the image has a 'transparency' or alpha/matte channel, but if it was not present or turned off,
`that it be initialised to be fully-opaque (See the Alpha Opaque method below).
`
`However if the image already has an alpha channel present and enabled, it will do nothing.
`
`In other words this operator ensures an alpha channel is present, without modifying the look of the image in
`memory, as it was before the option was run. As such on its own this operator does not show any change to the
`image, but has real effects when combined with other operators.
`
`So if you turn off the alpha channel using Alpha Off, and then enable it again using Alpha Set, the image will
`have an alpha channel, but it will be fully-opaque, just as the image looked, before 'Set' operation was requested.
`
` convert moon.png -alpha off -alpha set alpha_set.png
`
`If applied to an image that has an enabled alpha channel, no change is made.
`
` convert moon.png -alpha set alpha_noset.png
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`4/35
`
`Page 4 of 35
`
`
`
`4/11/22, 4:06 PM
`
`Masks -- IM v6 Examples
`
`In summery, this operator should never change the appearance of the image, at the time the operator is applied. It
`just ensures the alpha channel set up such that the image is left as it was.
`
`This is typically used after reading images of an unknown image file format or source, which may or may not
`have an alpha channel present. This operator will then ensure that the image does have an alpha channel (for
`image formats like JPEG), but leaving any enabled and existing alpha channel alone (such as for GIF or PNG
`formats).
`
`This is the recommended way of ensuring an image has an alpha channel after reading it into memory, or more
`importantly, after an image has been processed and you want to re-enable a clean alpha channel.
`
`Alpha On
`
`The "-alpha On" is the exact opposite to the previously looked at Alpha Off method. Typically this is to
`simplistic for the purpose you are wanting and as such should be very RARELY used. You should use "-alpha
`Set" in almost all cases.
`
`Basically 'On' method just flips the switch so that the image transparency data is visible again. Any existing
`transparency data is not modified, so if the in-memory image still has some old alpha channel data, that data will
`suddenly be visible again.
`
`For example here we turn 'Off' the transparency data, then immediatally turn it back 'On, reproducing the original
`image.
`
` convert moon.png -alpha off -alpha on alpha_on.png
`
`However if the image does not have any previous alpha data (yet) it will initialize it to be fully-opaque. Which is
`the logical thing to do. As such for new images just read into memory, it is equivalent to Alpha Set, but it should
`not be used for this purpose.
`
`The only time Alpha On should be used is when you previously, and purposefully turned off alpha for some
`reason, and not wish to restore that data.
`
`For example turning the alpha channel off then on can be used to preserve the alpha channel data before
`applying some very specific operators, such as "-shade".
`
`For an example of this special usage see Shaded Shape Images.
`
`Alpha Opaque
`
`This method not only ensures the alpha channel is 'active' but that it is also completely opaque, regardless of if
`the image had transparency 'activated/on' or 'deactivated/off'. For example...
`
` convert moon.png -alpha opaque alpha_opaque.png
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`5/35
`
`Page 5 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
`On older versions of IM, this was equivalent to using both "+matte" to turn off the alpha channel, then using "-
`matte" to turn it on, while resetting it to be opaque.
`
` convert moon.png +matte -matte alpha_opaque_matte.png
`
`The original 'shape' of the image can no longer be recovered after this operation as the original alpha channel
`data has been overwritten.
`
`Of course that is also equivalent to using "-alpha off -alpha set", though you may as well use "-alpha
`opaque" in that case.
`
`Alpha Transparent
`
`Similarly this ensures the alpha channel is 'active' but also fully transparent.
`
` convert moon.png -alpha transparent alpha_transparent.png
`
`The color data of the image is still present, so turning off transparency afterward will again show the images
`existing colors.
`
` convert moon.png -alpha transparent -alpha off alpha_transparent_off.png
`
`Of course the original 'shape' of the image was actually destroyed, so it can no longer be recovered after this
`operation.
`
`Other ways of making an image fully transparent is presented in Transparent Canvas.
`
`Alpha Extract
`
`The 'Extract' method will simply copy the 'alpha' mask of the image as a gray-scale channel mask.
`
` convert moon.png -alpha extract alpha_extract.png
`
`
`
`
`
`Note that fully-opaque is white, while fully-transparent is pure black.
`
`As image contained some semi-transparent pixels along the edges (for anti-aliasing providing the images shape
`with a smoother look), this image is not pure black and white, but also contains some gray colored pixels around
`the edges.
`
`If your imagemagick is old, this is a (near) equivelent technique, using channel extraction.
`
` convert moon.png -channel a -separate +channel -negate alpha_extract.png
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`6/35
`
`Page 6 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
`The 'Extract' method will also turned 'Off' the alpha, but it is not cleared, so turning the alpha channel back 'On'
`will re-create a shape mask of the original image.
`
` convert moon.png -alpha extract -alpha on alpha_extract_on.png
`
`Note that all the original colors will have been replaced with white with various shades grays around the edges.
`We can see this if we remove the transparency with a white background, (See Alpha Remove method below)
`
` convert alpha_extract_on.png -background white -alpha remove alpha_edge.png
`
`These 'gray' pixels are actually used to good effect in Edge Outlines from Anti-Aliased Shapes to generate a
`smooth edge or outline from a image shape.
`
`This side-effect of saving the alpha channel, has particular benefits when Using the Shade Operator, which does
`not understand or use the alpha channel of an image. See the sub-section, Masking Shaded Shapes.
`
`Alpha Copy
`
`The 'Copy' method is the reverse of 'Extract', and essentially performs a CopyOpacity against itself. That is it
`will turn a gray-scale image (regardless if its alpha channel is enabled or not) into a shape mask image.
`
` convert alpha_extract.png -alpha copy alpha_copy.png
`
`
`
`
`
`It does not matter if the image had an existing alpha channel or not, all it does is create the images transparency
`from the image gray-scale values.
`
`Once you have a shape mask, you can use various Color Tinting or Duff-Porter alpha composition methods, to
`color it. For examples of using a shape mask see Masks as Colored Shapes.
`
`Alpha Shape
`
`To make use of a gray-scale image easier, the 'Shape' method not only creates a shape mask (as per Alpha
`Extract, but will also color it using the current background color.
`
` convert alpha_extract.png -background Yellow -alpha shape alpha_shape.png
`
`
`
`
`
`This means you can very quickly color a gray-scale mask simply by shaping the image, then flattening it onto a
`different background color
`
` convert alpha_extract.png -background Yellow -alpha shape \
` -background Blue -alpha remove alpha_colormask.png
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`7/35
`
`Page 7 of 35
`
`
`
`4/11/22, 4:06 PM
`
`Masks -- IM v6 Examples
`
`Of course a faster and better way to map a black and white image, directly to specific colors is by using the more
`specialised Level Adjustment by Color. This will avoid the need to enable or even modify the existing images
`transparency channel.
`
` convert alpha_extract.png +level-colors Blue,Yellow level_color.png
`
`Alpha Remove
`
`The "-alpha Remove" method (added to IMv6.7.5) is designed to remove the transparency from an image, using
`the current "-background".
`
` convert moon.png -background tan -alpha remove alpha_remove.png
`
`Note that while transparency is 'removed' the alpha channel will remain turned on, but will now be fully-opaque.
`If you no longer need the alpha channel you can then use Alpha Off to disable it.
`
`This operation is simple and fast, and does the job without needing any extra memory use, or other side effects
`that may be associated with alternative transparency removal techniques. It is thus the prefered way of removing
`image transparency.
`
`For other techniques, or if your Imagemagick is older that v6.7.5, then look at the larger discussion Removing
`Transparency from Images) below.
`
`Alpha Background
`
`As of IM v6.5.2-10, a 'Background' method was made available that will set the hidden color of fully-transparent
`pixels to the current background color.
`
`Normally this color is of no consequence, as it can only be seen if the alpha channel is turned off. However the
`color of fully-transparent pixels is saved in PNG Image file format, and for large images random unknown
`colors can significantly effect its compression handling. See PNG with Better Compression and the IM Forum
`Discussion Eliminating alpha channel garbage for more details.
`
`Note that no color mixing is applied, only a direct color assignment to any fully-transparent color. The pixels
`however will still remain fully-transparent, and as such you will see not change to the image.
`
`For example here I use it to set all fully-transparent pixels to 'HotPink'.
` convert moon.png -background HotPink -alpha
` Background moon_hotpink.png
`
`As you can see this made no change to the actual look of the image.
`
`To see the change we will now turn off the alpha channel.
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`8/35
`
`Page 8 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
` convert moon_hotpink.png -alpha off moon_hotpink_off.png
`
`This is not the same as Removing Transparency
`
`The edges of the shape will have made all semi-transparent pixels opaque, and as a result produced some strong
`aliasing (stair-cased) edge effects.
`
`This process of replacing the colors is actually almost the same as doing a "-channel RGB -fill color -opaque
`None +channel". See Direct Color Replacement.
`
`Note that many other image processing operators will also convert any fully-transparent pixels, to fully-
`transparent black (color 'None'), as this is the color equivalent of a mathematical zero. Here is a summery of some
`image operations that are known to do this, though none are as direct or as fast as using this operator.
`
` convert moon.png \( +clone -alpha off \) \
` -compose SrcIn -composite moon_black.png
` convert moon.png -channel RGBA -blur 1x.000000001 moon_black.png
` convert moon.png -channel RGBA -gaussian 1x0 moon_black.png
` convert moon.png -fuzz 0% -transparent none moon_black.png
`
`That last method (see Fuzz Factor and Transparent colors is particularly useful as you can not only set all
`transparent colors to full-transparent-black ('None'), but also all near-fully-transparent colors (which otherwise
`does have a valid but practically invisible color), simply by specifying a fuzz factor. It will produce some data
`loss, but may improve compression in images with lots of near-fully-transparent colors. Often these nearly total
`transparent pixels can have very odd or wrong colors, and this method will allow you to remove such odd pixels
`before they cause other problems.
`
`Removing Transparency from Images
`
`While the Alpha Off will simply flip a switch and turn off the transparency channel. You can also get the same
`effect if you attempt to save the image into a file format that does not allow the use of transparency. For example
`by saving to JPEG...
`
` convert -size 70x60 xc:none -font Candice -pointsize 50 \
` -fill Black -annotate +10+45 'A' -channel RGBA -blur 0x5 \
` -fill white -stroke black -draw "text 5,40 'A'" a.png
`
` convert a.png a.jpg
`
`
`
`
`
`Remember the JPEG File Format, does not save the alpha (transparency) channel, and as such simply turned it
`off.
`
`In this case transparent parts just became black (a typical result). But depending on the image source the
`transparent areas could have just as easily become some other random, or other inappropriate color.
`
`Also in many cases semi-transparent pixels can have some very odd colors that is typically not visible because
`they are almost completely transparent. Simply turning off transparency will make these pixels stand out like a
`sore thumb, making the result look even worse than you may have expected. See for example the top-left edges
`of 'A' in the above.
`
`In either case simply turning off transparency is typically NOT what is wanted.
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`9/35
`
`Page 9 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
`The best solution is to use the Alpha Remove method to quickly and simply replace the transparency with a
`background color underlay...
`
` convert a.png -background skyblue -alpha remove -alpha off a_remove.jpg
`
`Strictly speaking the Alpha Off is not needed in this case, as the save to JPEG does this automatically.
`
`Alternative technqiues of removing transparency, is to somehow generate a new 'background' or 'canvas' image
`and Over Compose your image onto that background so that the transparency is replaced. Preferably while
`preserving the original image's meta-data, such as profiles, labels, captions and comments that may be present.
`
`Methods for generating such a canvas is exampled in Creating Image Canvases of Same Size. Here is one such
`method...
`
` convert a.png \( +clone -alpha opaque -fill SkyBlue -colorize 100% \) \
` +swap -geometry +0+0 -compose Over -composite \
` -alpha off a_compose.jpg
`
`Other simplier ways to do this, is to use a operation that internally creates a 'cloned background canvas' for you,
`generating it as part of the larger image processing operation that the operator is performing.
`
`The most common method is to Flatten the image. This operator is so often used for this purpose that the process
`of removing transparency has often erroneously been called 'flattening'. For example...
`
` convert a.png -background skyblue -flatten -alpha off a_flatten.jpg
`
`However this will not work with "mogrify" or with a sequence of multiple images, basically because the "-
`flatten" operator is really designed to merge multiple images into a single image.
`
`The other common method that does work with multiple images is to give the image a zero sized Border with
`the appropriate "-bordercolor". For example...
`
` convert a.png -bordercolor skyblue -border 0 -alpha off a_border.jpg
`
`Other image processing operators which are closely related to the above methods, can also remove transparency
`from an image. These include: Mosaic, Merge, and Frame.
`
`The Extent operator can also be used, and allows you to expand or crop images at the same time as you remove
`the transparency, but only if you know the size of final image you desire.
`
`You do not have to replace transparency with a solid color. If you use a DIY composition (as shown above) you
`can use any image for the replacement background. One simple example of this is to use the "composite"
`command to Tile an image 'under' the original, (using Dst_Over). This compose method ensures the original
`images meta-data and size is preserved.
`
` composite -compose Dst_Over -tile pattern:checkerboard \
` a.png a_undertile.jpg
`
`Many of the above methods, are either effected by, or may destroy any virtual canvas information an image may
`have, as part of its processing. When the virtual canvas is involved, you may need to look at the details of individual
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`10/35
`
`Page 10 of 35
`
`
`
`4/11/22, 4:06 PM
`
`Masks -- IM v6 Examples
`operators more closely. In many cases the virtual canavs effects can be useful to your overall image processing.
`
`Boolean Alpha Transparency
`
`For some image file formats you don't need to completely remove the alpha channel, but only allow pure on/off
`or boolean transparency. Index (Palette) image file formats such as GIF and PNG8, are typical of this.
`
`Examples are currently looked at in GIF Boolean Transparency, but should eventually move here.
`
`Outline or Halo Transparency
`
`Sometimes you will like to add a outline around an image containing transparency.
`
`One way is to use EdgeOut Morphology to quicked get all the neighbouring pixels to the original image, color
`them, and then Under (DstOver) Compose it with the original image.
`
` convert knight.png \( +clone \
` -channel A -morphology EdgeOut Diamond +channel \
` +level-colors red \
` \) -compose DstOver -composite knight_outlined.png
`
`
`
`
`
`This can be particularly useful when creating GIF format images from PNG images containing semi-transparent
`edge pixels. It provides a minimal amount of background color, but leave the rest of the image fully-transparent.
`For more about this see GIFs on a Background Pattern for more about this problem.
`
`An alturnative method is to generate a soft semi-transparent halo around the shape. To do this we Blur and
`recolor the image, then again Under (DstOver) Compose it with the original.
`
` convert knight.png \( +clone \
` -channel A -blur 0x2.5 -level 0,50% +channel \
` +level-colors red \
` \) -compose DstOver -composite knight_halo.png
`
`This last is actually similar to using a Soft Outline Compound Font effect, but using a shaped image rather than
`annotated text.
`
`Using Masks with Images
`
`Masking An Image
`
`As shown previously there are a couple of ways to mask an image, so as to make part of the image transparent.
`And which method you choose depends on the weather your image mask is a grayscale mask, or a shaped mask.
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`11/35
`
`Page 11 of 35
`
`
`
`4/11/22, 4:06 PM
`Editing a Image Mask
`
`Masks -- IM v6 Examples
`
`The mask of an image is a really useful thing to have.
`
`We can for example erase parts of an image very easily by modify a mask of the original image. Remember the
`"-draw" operator can not draw nothing, and currently has no erase option.
`
`Here we create a image, then by extracting and modifying its mask, before restoring it to the original image.
`
` convert -size 100x100 xc:none -stroke black -fill steelblue \
` -strokewidth 1 -draw "circle 60,60 35,35" \
` -strokewidth 2 -draw "line 10,55 85,10" drawn.png
`
` convert drawn.png -alpha extract mask.png
`
` convert mask.png -fill black -draw "circle 40,80 60,60" mask_bite.png
`
` convert drawn.png mask_bite.png \
` -alpha Off -compose CopyOpacity -composite \
` drawn_bite.png
`
`
`
`
`
`
`
`
`
`
`
`
`
`Remember "black" in a mask is transparent, while white is opaque, so all we need to do is draw black over
`anything we don't want visible.
`
`Don't forget the "-alpha Off" operation in the above as it is vital to ensure the grayscale image does not contain
`unneeded transparent channel.
`
`And Presto we took a bite out of the original image.
`
`We can also re-add a part of the image we removed. For example here I re-add part of the 'bite' I removed from
`the original image, by drawing white area onto the mask. The mask is then again returned to the original image
`using CopyOpacity Channel Composition.
`
` convert mask_bite.png -fill white \
` -draw "circle 50,70 60,60" \
` -draw "roundRectangle 78,5 98,25 5,5" \
` -alpha off mask_bite2.png
` composite -compose CopyOpacity mask_bite2.png drawn.png drawn_bite2.png
`
`
`
`
`
`
`
`
`
`Just a word of warning about re-adding parts. Typically ImageMagick replaces any fully-transparent color with
`black, usually because that is how the mathematics behind operators work. It is after all fully-transparent and
`thus its color should not normally matter. That means that if we make a part of the image we haven't drawn
`before opaque, then it will generally be black, since that is the color under the image's transparency.
`
`https://web.archive.org/web/20120928070642/http://www.imagemagick.org/Usage/masking/
`
`12/35
`
`Page 12 of 35
`
`
`
`Masks -- IM v6 Examples
`4/11/22, 4:06 PM
`However in the above example you will have noticed that the PNG image file format correctly preserved the
`original (made transparent) color of the image. As such the color of the re-added part remained the original
`'SteelBlue' color of the original image. You should not count on this if the image was saved to some other file
`format or further modified.
`
`Here is an alternative method of erasing parts out of an image but rather than extracting and modifying a
`Grayscale Mask, we instead use a Shape mask as a sort of 'erase' tool using DstOut Composition Method.
`
` convert -size 100x100 xc:none -draw "circle 40,80 60,60" mask_shape.png
`
` convert drawn.png mask_shape.png -compose DstOut -composite drawn_bite3.png
`
`
`
`
`
`
`
`
`
`As you can see sometimes Shape Masks are easier to handle, as you avoid the need to extract and restore the
`alpha channel.
`
`However the Duff-Porter Alpha Composition Methods, which is what I am using, will never allow you to restore
`colors that have been made transparent. With these methods, anything that has been made transparent (and thus
`undefined in color), stays transparent.
`
`In actual fact erasing parts of an image using Alpha Composition Methods will actually destroy the underlying
`color of fully-transparent pixels. It will not preserve it. After all, a transparent color is actually not a real color!
`
`Masks as Colored Shapes
`
`An alternative to just using the mask to add or re-add transparency to an image is to actually combine the mask
`directly with images in various ways.
`
`For example suppose we just want to use a mask as a symbol or shape we want to overlay onto an image in
`various colors. For this we need a mask, which I'll extract from a special 'symbol' font.
`
` convert -font WebDings -pointsize 24 label:Y \
` +trim +repage -negate heart_mask.gif
`
`Note that I negated the label image to make it proper mask image, consisting of a white foreground (opaque) on
`black background (transparent).
`
`As of IM v6.4.3-7 the simplest way to convert a grayscale mask, into a colored shape is to use the Alpha Shape
`operator, This is exactly like Alpha Copy, but with a extra step to color the final shape.
`
` convert heart_mask.gif -background Red -alpha Shape heart_red.png
`
`Note the use of 'PNG' image format for generated shaped image rather than GIF so as to avoid problems with GIF
`Boolean Transparency.
`
`Before this the simplist solution was to negate the alpha mask into a matte Cha