Friday, March 29, 2024

Web Developer Class: How to Mask Images

There will be times when you want to manipulate your images to create something new. One of your options is to remove part of an image to use in another project. The process of removing an image from the background is known as masking, and in this tutorial we’ll show you how to do just that.

Masking can be quite simple or complex, depending on the image. If you have a well-defined background then the masking process is relatively simple. In contrast, if your foreground mingles with the background, or if the foreground object has a lot of fine detail such as hair, masking becomes more difficult. One way of making the process easier is if you can photograph your subjects in a photographic studio. With a combination of lighting and high contrast backgrounds you can simplify the process. Real life, however, doesn’t always offer those opportunities.

In this tutorial, we will create a relatively simple mask, where we remove a background of sky from an image. Our program of choice is Paintshop Photo Pro X3. Before we get started, it is important to save a copy of the original file. If anything goes wrong with the file you have a backup. Another important step is to make sure you copy the background before you start the masking process. This also preserves the original image.

Another step I recommend is to use a high contrast layer beneath the mask layer. This allows you to clearly see the edges of the mask and will let you know if you need to refine the mask.

To get started, here’s an image with a blue sky in the background, which will be easy to mask.

The first step is to activate the Layers palette. To do so, go to View: Palettes: Layers (F8).

Our next step is to create a new layer. To do so, click on the New Raster Layer button at the top left of the Layers palette and in the dropdown list, choose New Raster Layer. In the dialog box, name it: High Contrast.

Make sure the layer is active, then in the Materials palette, click on the color swatch to the right of the rainbow color mixer.

This brings up the Material Properties dialog box. Here, set the color to RGB: 0, 255, 0. Click on OK and use the paint bucket to fill the layer, which gives you a green, high-contrast layer.

The next thing to do is to make a copy of the background. To do so, right-click on the background layer and in the popup menu, choose Duplicate. Double-click on the duplicate layer to bring up the Layers Properties dialog box and name the layer: Masking Layer. The next thing is to click on the High Contrast layer and drag it below the Masking Layer.

Here’s what the palettes should look like on the right side of the workspace.

Now we are ready to mask the image. To begin, click on the tool that is third from the top on the Tools bar and choose the Magic Wand tool. If you don’t see this, go to View: Toolbars: Tools to activate it.

When you do so, this brings up the Magic Wand settings. Here are the settings I recommend:

Mode: Add 
Match Mode: Color
Tolerance: 20
Contiguous: Enabled
Use all layers: unchecked
Feathering: 0
Anti alias: unchecked

Now we are ready to mask the image. Note that the cursor has changed to a Magic Wand with a plus (+) sign beside it. This indicates that we will add to the mask as we go along. The next step is to click in all the blue sky areas around the image.

Here’s the masking in progress.

If you make a mistake, you can erase it by clicking on the Undo Last Command button or by clicking on Ctrl+Z. If you want to clear the entire selection and start again, click on Ctrl+D.

When you’ve finished masking the entire image, you’re now ready to remove the background. To do so, go to Edit: Cut (Shift+Delete or Ctrl+X).

You can now see through to the high-contrast layer below. The next step is to invert the mask, so you will have the foreground image. To do so, go to Selections: Invert (Ctrl+Shift+I).

At this point you could stop, but you’ll find that the masked image may look as if it’s been cut out and pasted on to the new image. This is because the mask has a hard edge. In real life, the edges of images have a gradual transition.

Here is an example of an image, which is blending into a background at high magnification. Note the transition. To mimic that effect with masked images, we need to use feathering, which softens the edge, makes it semi-transparent and allows the image to blend into the background.

Conclusion

This tutorial introduced you to the process of masking images. There is far more to masking than this example, and it can become quite complex. Still, you now have a starting point which will help you with your images.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured