Computer Graphics

Basics: Masks

Another way of separating graphical information is, besides layers, using masks. Before we get to actual masks, let's excercise working with layers again.

We will be combining two images into one throughout this tutorial. The first example uses a picture of a flat screen television and a photograph (or a picture from your favorite movie). You can find your own pictures or use these two:

Our task will be to place the photograph into the picture of the television so that it appears to be showing on the screen.

1. Open the files in GIMP

Save both image files on disk, start GIMP (if you haven't already) and click on File > Open .... The open file dialog looks different than what you're problably used to but that's the price you have to pay for having a multi-platform free software. Anyway, you should be able to locate the files and open both of them. They will be opened as two separate files, switchable by the tabs (with their thumbnails) above the main image area or via the Windows menu:

2. Put them together

Use the Select > All ... to select everything in the photo and copy it to clipboard (Ctrl+C). Now, switch to the tab with the television. Normally, you would press CTRL+V now and the copied image would be pasted here. GIMP is a little tricky here and for the moment just use Edit > Paste as > New layer.

Note: If you did press CTRL+V instead, the image would still be pasted. But it'd be pasted as a so-called Floating selection (see Layers panel). To fix that, right-click the Floating selection in the Layers panel and choose To new layer.

You now have two layers in the same image: The background layer containing the television and a second layer containing the photograph. We will be working with the second layer only from now on.

3. Fit the photo into the television screen

We will now distort the photograph in order to fit it into the screen area of the television. The area is not rectangular and simple scaling won't work. But first, decrease the layer's opacity to 50-60. (The opacity bar for the active layer is in the Layers panel.) Decreasing opacity was not entirely necessary but it will us in the next step:

Select Perspective tool (Shift+P) in the tools panel. It's one of the blue quadrilateral icons. Now, make sure you have the photograph layer selected and click the photograph in the main area. The photograph will now have a grid and you can move each corner separately. Drag the corners of the photograph layer, each to it's corresponding corner of the television screen. Like this:

4. And we're done ... or are we?

If you're done, hit Enter or press 'Transform' in the Perspective tool dialog window and increase the photograph layer's opacity back to 100.

Â

5. Now do this again

Now, repeat the previous steps, but this time with this image of a television:

Clearly, fitting the photograph into the corners of the television screen won't be a problem. But the man obstructing the screen will.

There are basically two ways to fix this: erasing the contents of the photograph layer using the Eraser tool (Shift+E) or using a layer mask. You will need a lot of luck and patience if you choose the eraser. Accidental errors are very hard to fix and you have very little control over the final result. Once you erase something it's lost forever and you'd have to do Edit > Undo to bring it back.

On the other hand, masks are designed to remove parts of the layer in a reversible way and they are much easier to work with.

6. So what is a mask anyway?

Layer mask is a special black-and-white image that says which parts of a layer should be visible and which not. It's the same size as the layer and if a pixel is black in the mask, the corresponding pixel in the layer is invisible (i.e. transparent). White areas in masks mean the layer is visible in those areas.

Erasing (or hiding) a part of a layer is performed by painting black color in the mask. If you accidentaly erase something you didn't want to, you can always paint white in the mask and bring it back. This is not possible with the Eraser tool.

This is our situation after fitting the photograph to the television screen:

To create a maks, select the photograph layer in the layer panel, right click on it and choose Add layer mask. A dialog will pop up, let's just stick with the default options (or select White (full opacity)) and click Add. A black-and-white thumbnail now appeared to the right of your layer in the layer panel:

7. Painting into the mask

Select the Paintbrush tool and set the foreground color to black. Click the mask thumbnail in the layers panel to make sure you're going to be painting into the mask and not into the layer. There should be a smal white frame around the mask thumbnail if the mask is selected. If the layer is selected, the white frame is around the layer thumbnail.

Select the brush size of your choice (e.g. 10 pixels, sharp corners) and paint with black in those areas where you want the photograph layer to be hidden and the man from the TV layer to be visible.

Tip: Decrease the photograph layer opacity to 70 to get a better idea where the man's head and man's hands are.

Don't worry if you happen to accidentally remove too much of the photograph layer. Just switch to white color and bring it back by painting white in the mask in those areas you wish to be visible.

Tip: When painting in a layer mask, select black as foreground color, white as background color (or the other way round). This way, you can easily switch between black and white color just by pressing X.

Paint the mask until the man is fully visible. Don't worry about the hair. Hair are always a problem. We might look into masking out hair some time in the future.

And another tip: Quick zoom-in, zoom-out comes handy in this situation. Ctrl+Mousewheel will help you out.

You should end up with something like this. There's always things to fix when you're masking something out, but let's not overdo it the first time: