Sub topic 1
Selections, masks, and partial transparency
Cutting out hair and fur depends on one idea: an edge in Photoshop can be partly transparent. This chapter sets up that idea. Later chapters use it when you run Select Subject, open Select and Mask, and output a layer mask.
Prerequisites
This chapter assumes you already have the following:
- You can find your way around Photoshop: the Layers panel, the Tools panel, and the Channels panel.
- You know what a selection is and what marching ants look like.
- You know the basic rule of a layer mask: white reveals, black hides.
- You have a photo with a subject that has visible hair or fur, so you can try the checks below.
What a selection stores
Marching ants look like a thin line, but a selection is not a line. Photoshop stores a selection as a grayscale image with the same pixel dimensions as your document. Each pixel has a value from 0 to 255:
- 0 means the pixel is not selected.
- 255 means the pixel is fully selected.
- Values in between mean the pixel is partly selected.
A partly selected pixel gets a partial effect. If you apply a blur or an adjustment to a selection, a pixel at 128 receives about half the effect.
The marching ants only mark where the stored value crosses the 50% level. Pixels brighter than that fall inside the ants and pixels darker fall outside. A selection can therefore contain a lot of partial information that the ants hide. A feathered selection is the clearest case. Its ants show one line, but the stored values fade gradually across a band around that line.
You can look at the stored values directly:
- Press Q to enter Quick Mask mode. Unselected areas get a red overlay, and partly selected areas get a weaker red.
- Press Q again to leave Quick Mask mode.
- Choose Select > Save Selection. Photoshop writes the selection into an alpha channel, which you can open in the Channels panel and view as a grayscale picture.
A saved selection and a layer mask hold the same kind of data. Both are grayscale images, so you can move values between them. Cmd-click (Mac) or Ctrl-click (Windows) a mask thumbnail to load that mask as a selection.
Hard and soft edges
Photoshop has two kinds of edge, and the difference matters for hair.
A hard-edged selection has only 0 and 255 in it. Every pixel is either in or out. This works for a brick wall or a product with a crisp outline. It also works for an anti-aliased edge, where a single row of pixels is given an in-between value so that diagonal lines look smooth.
A soft-edged selection has a range of values along the edge. It covers three cases:
- Feathering, which spreads the edge over a chosen number of pixels.
- Natural softness, such as an out-of-focus subject.
- Fine detail, such as a hair strand that is thinner than one pixel.
The third case is the hard one. A strand of hair may be a quarter of a pixel wide. The camera records that pixel as a mix of hair color and background color. No yes-or-no decision about that pixel is correct, because it is partly the subject and partly the background. The right answer is a value like 64: about a quarter subject, three quarters background.
Layer masks: selections you keep
A selection disappears when you deselect or select something else, unless you saved it. A layer mask is a saved selection attached to a layer. The mask uses the same 0 to 255 scale, and it controls how visible each pixel of the layer is:
| Mask value | Appearance | Effect on the layer's pixel |
|---|---|---|
| 255 (white) | White | Fully visible |
| 0 (black) | Black | Fully hidden |
| 1 to 254 (gray) | Gray | Partly visible, proportional to the value |
The layer's pixels are not changed. If you erase pixels with the Eraser or press Delete on a selection, the data is gone. If you hide the same pixels with a mask, the pixels are still there, and you can paint the mask white to bring them back. This is what non-destructive means in this tutorial.
There are some mask handling shortcuts you will use often:
- Click the mask thumbnail to edit the mask rather than the layer pixels.
- Alt-click (Option-click on Mac) the mask thumbnail to view the mask as a grayscale image on the canvas.
- Press the backslash key (\) to show the mask as a red overlay on top of the image.
- Shift-click the mask thumbnail to turn the mask off temporarily and see the unmasked layer.
A vector mask is a different feature. It is defined by a path, so its edges are hard or uniformly feathered. Use a pixel-based layer mask for hair, because only a pixel mask can hold a different value at each pixel.
Partial transparency as arithmetic
Compositing with a mask is a weighted average. For each pixel, with alpha as the mask value divided by 255:
result = alpha × subject + (1 − alpha) × background
The following Python example applies that formula to one gray channel. It runs as written with no extra libraries.
def blend(subject, background, mask_value):
"""Composite one channel value using a 0-255 mask value."""
alpha = mask_value / 255
return alpha * subject + (1 - alpha) * background
subject = 200 # a light hair pixel
background = 40 # a dark new background
for mask_value in (0, 64, 128, 255):
result = blend(subject, background, mask_value)
print(f"mask {mask_value:3d} -> {result:.1f}")The output is:
mask 0 -> 40.0
mask 64 -> 80.2
mask 128 -> 120.3
mask 255 -> 200.0A mask value of 64 gives a result about a quarter of the way from the background to the subject. A fine strand behaves this way: it blends into whatever is behind it instead of sitting on top as a solid line.
This arithmetic also explains why edges that look fine on one background fail on another. The formula assumes the subject pixel contains only subject color. In real photos, an edge pixel has already been mixed with the old background by the camera. When you put it over a new background, the old color is still in it. You see this as a halo or a colored fringe. Decontaminate Colors, covered later, exists to remove that old color from the edge pixels.
The pipeline in this tutorial
The workflow in the remaining chapters turns a rough selection into a mask with a graded edge. The diagram shows where the selection becomes data you keep.
Each stage uses the grayscale model from this chapter:
- Select Subject makes a base selection. It is usually close to hard-edged, with a good body shape and a poor hair edge.
- Select and Mask recalculates values along the edge. Edge Detection, the Refine Edge Brush, and Refine Hair mostly work by assigning partial values to hair pixels that were 0 or 255 in the base selection.
- Decontaminate Colors changes the color of edge pixels, not their mask values.
- Output settings decide where the result goes. A layer mask keeps the values editable. A plain selection does not keep them. Deleting the background throws them away.
Common mistakes at this stage
- Judging a selection by the marching ants alone. The ants hide soft values. Check in Quick Mask mode or view the saved channel instead.
- Using Delete to remove a background. This removes the pixels permanently. Use a mask so that you can fix the edge later.
- Expecting a hard-edged selection to give a good hair result by feathering it. Feathering blurs the edge evenly. It does not recover individual strands, and it softens the whole outline, including parts that should be sharp.
- Choosing a vector mask for hair. Use a pixel layer mask.
- Treating the mask as the whole job. The mask controls visibility, and the color of the edge pixels is a separate problem.
Check your understanding
Exercise
A hair strand covers about one quarter of a pixel against a dark background. Which mask value should that pixel get, roughly, and why does a hard-edged selection handle it badly?
Show answer
The pixel should get a mask value near 64, because 64/255 is about 25%: a quarter subject and three quarters background. A hard-edged selection can only choose 0 or 255. It either drops the strand completely or turns it into a full-strength pixel that looks too thick and too bright.
Exercise
You loaded a selection and the marching ants look clean, but the result looks harsh when you apply an adjustment. How can you check what values the selection actually holds, and how do you turn it into something you can keep?
Show answer
Press Q to see the selection in Quick Mask mode, or use Select > Save Selection and view the new alpha channel in the Channels panel. Both show the grayscale values behind the ants. To keep the selection, use Select > Save Selection, or add a layer mask while the selection is active, so the values are stored with the layer.