CPSC 324, Spring 2004
Lab 4: Gimp Colors and Selections


GIMP WAS INTRODUCED in Lab 1, which covered some of the basic Gimp drawing tools. We continue in this lab with a look at some of Gimp's tools for manipulating colors and for selecting regions of an image. These are large topics that we will continue next week in our third and final lab on the Gimp.

Some images that I used for the examples in this lab can be found in the directory /home/cs324/lab4. You might also want to work with the photos that I took during the first lab. These can be found in /home/cs324/photos. And there are lots of free photos at the 3D Cafe (http://www.3dcafe.com/asp/photographs.asp). (I got some of the photos for this lab from that site.)

Please remember that you should use Gimp-1.3 for this lab. Many of the things described on this page were different in the previous version.

Did you know that in the KDE desktop, you can move a window by ALT-clicking and dragging at any point in the window? Unfortunately, GIMP also wants to use ALT-clicking for certain features, but KDE grabs the ALT-clicks before GIMP ever sees them. I suggest changing the KDE configuration. Open the KDE Control Center (from the Start menu). Select "Desktop" then "Window Behavior" in the list at the left, then select the "Actions" tab. Near the bottom, under "Modifier Key: ALT", change the setting for "Modifier Key + Left Button" to "Nothing". I would also change the setting for "Modifier Key + Right Button" to "Move"; this will allow you to ALT-right-click-and-drag a window to move it.

Note that the exercises for this lab are scattered throughout this page.


Color Tools

In class, we have looked at the RGB, RGBA, CMY, CMYK, grayscale, and indexed color models. Gimp can work with all of these to some extent. Every image in Gimp has a "mode" which can be RGB, Grayscale, or indexed. (You can change the mode using the "Mode" submenu of the "Image" menu, if, for example, you want to change a color image to grayscale.) One of the interesting things you can do with colors is to decompose a color image into components or "channels". Each channel is a grayscale image that represents one of the color components. And given three or four grayscale images, you can "compose" a new color image by using the grayscale images as the individual color components.

The "Decompose" and "Compose" commands can be found in the "Colors" submenu of the "Filters" menu on any image window. The "Decompose" command will be active only if the window contains an RGB image. The "Compose" command is active only if the window contains a grayscale image. When you use the Decompose command, you can select the type of decomposition that you want. For an RGB decomposition, you get three grayscale images that show the red, green, and blue components of the image. In an HSV decomposition, the grayscale images represent the hue, saturation, and value. And similarly for a CMY decomposition. The Compose command allows you to put the channels back together. It gives you an opportunity to compose a color image from several grayscale images. You can select which grayscale image is used for each channel in the color image. Now, the thing is that you can decompose the image one way and then compose a new image another way. For example, you could mix up the R, G, and B channels. Or you could combine the channels from an RGB decomposition into an HSV composition. For example, here is an image I made by mixing up the R, G, and B channels in hws.jpg:

false color image

Things can get much wierder than this.

Exercise 1. Make a surrealistic (that is, weird) image by decomposing an image one way and then re-composing it another way. Add the image to your Web page, along with an explanation of how you made it.


Selection Tools

One of the most important operations in Gimp is selecting regions of an image, and Gimp has many tools that can be used in doing this. The first thing to understand is that a selection is not simply a set of pixels. A pixel can be partially selected. If you do a cut-and-paste, a partially selected pixel will be partially transparent, so that some of the original color will show through the pasted image. For most selection tools, pixels along the boundary are partially transparent by default. This is meant to provide anti-aliasing for cut-and-pasted images. In fact, a selection is really a kind of "alpha channel" that assigns a degree of transparency to each pixel in an image, and this assignment can be done in a completely arbitrary way. As with other channels, it's possible to think of a selection as a grayscale image -- later, we'll see how this can be useful.

Gimp selection tools The first seven tools in Gimp's Toolbox window are selection tools. The rectangular selection tool lets you select rectangular or square regions by clicking and dragging. The elliptical selection tool lets you select elliptical or circular regions by clicking and dragging. The lasso, or freehand selection tool, lets you draw a freehand curve around the region you want to select. The magic wand tool is used simply by clicking; it selects a contiguous group of pixels that are similar in color to the pixel you click. It's just like the bucket tool, except that instead of filling a region, it selects the region. The color selection tool does something similar, but instead of just selecting neighboring pixels, it selects all pixels in the entire image that are similar in color to the pixel where you click.

The next tool is the Bezier path tool which is used to create and edit paths. Paths are actually more general than selections, but you can make a selection by using this tool to draw a path around the region that you want to select. You can then edit the path to adjust the boundary as exactly as you like. I will return to the Bezier tool below. The final selection tool is the "magic scissors tool", which is rarely used because it doesn't work all that well. However, I will also return to this tool below.

For the first four selection tools, you can use the SHIFT key and the CONTROL key to modify the action of the tool. They allow you to modify an existing selection by adding a new region to the selection or by subtracting a region from the selection. If a selection already exists, and if you hold down the SHIFT key as you click the mouse, then the newly selected region will be added to the previous selection. If you hold down the CONTROL key as you press the mouse, the region that you select will be subtracted or deleted from the previous selection. (This only has an effect if the new region and the old region intersect. The intersection is deleted from the selection.)

For the rectangular and elliptical selection tools, it's confusing because SHIFT and CONTROL have an additional, entirely different meaning. If you are using the rectangular selection tool and you hold down the SHIFT key as you drag the mouse, then the rectangle is constrained to be a square. So: If you want to add a rectangle to a selection, press the SHIFT key before you click the mouse, then release the SHIFT key. If you want to make a new, square selection, click the mouse, then press the SHIFT key and hold it down while dragging. And if you want to add a square to a selection, you can hold down the SHIFT key the whole time. Similarly, if you are using the elliptical selection tool and you hold down the SHIFT key as you drag the mouse, then the ellipse is constrained to be a circle. The effect of the CONTROL key while dragging is this: The point where you first clicked the mouse will be the center of the rectangle or ellipse, instead of one of the corners. It's usually easier to draw an elliptical selection from its center point instead of from a corner.

feathered selection Once you've made a selection, there are several operations that you can perform on it. Some of them can be found in the "Select" menu in the menu bar of the image window. For example, the "Invert" command turns a selection into its exact opposite: What was outside is now inside, and what was inside is now outside. This is useful if it's easy to select the part of the image that you don't want in a selection. Just select the part you don't want, then invert. There are commands for shrinking and growing a selection by a specified number of pixels. The "Feather" command has an especially interesting effect. It makes a border around the selection in which the amount of transparency increases gradually. You don't actually see the effect until you cut out the image or copy and paste it somewhere else. For the flower image, I scaled flowers.jpg to about 0.6 of its original size, made an elliptical selection, feathered the selection by 25 pixels, did a copy and then a paste onto a white background.

Once a selection has been made, you might decide that you want to reposition it. To do this, position the cursor inside the selection, then ALT-click and drag. In KDE, this will only work if you have configured KDE to ignore ALT-click, as described at the top of this page.

If you simply click-and-drag inside the selection, without using the ALT key, you will actually move the pixels contained inside the selection! The selected region becomes what is called a floating layer. If you have the "Layers" dialog box open, you will see that a new layer has been created. Floating layers are also used with cut-and-paste, so its important to understand how they work. In Gimp, when you paste something, the pasted image is placed into a new floating layer, so that the original image is still there, below the pasted image. This allows you to drag the pasted image into position, without messing up the original image in the underlying layer. Before you can proceed with any other editing of your image, you musto decide what to do with the floating layer. You can either "anchor" it, which will merge it into an existing layer, or can make it into a new, separate, normal layer. Normally, you will want to do the latter, so that you can come back and move or otherwise edit the layer later. All you need to do is select "New Layer" or "Anchor Layer" from the "Layer" menu at the top of the image window.

(Note: When you drag a selection with the grab tool, without holding down the ALT key, it is exactly the same as Cutting it and Pasting it into a new floating layer in the same image.)

Exercise 2. Use a feathered selection and cut-and-paste to make an image similar to the flower image shown above. Use a different image as a starting point. If you want, you can make your image from pieces of several different images. Post the image on your Web site, with an explanation of how you made it.


Working with Selected Regions

When working with Gimp, remember that when there is a selection, any operation that you apply to an image (painting, erasing, filtering, color adjustments, etc.) will apply only within the selected region. (Also, it will only apply to the currently selected layer.) This can actually be very useful. Here, for example, is a picture taken at a talk given by Leya Tesmenistky in Lansing 300:

Leya's talk

The picture is dark and indistinct, except for the projection screen. I would like to brighten the background colors, without changing the colors of the screen. To do this, I need to select everything except the screen. The easiest way to do this is to select the screen and then invert the image (using the "Invert" command in the "Select" menu).

There are several ways to select the screen. For example, you could use a combination of the magic wand, plus the lasso tool to capture regions missed by the magic wand. (Remember to use control-click with a selection tool to add to an existing selection.) But the easiest tool in this case is the magic scissors tool. To use this tool, you are supposed to click on points around the boundary of a region -- the tool is supposed to figure out the exact path of the boundary between points. As I said, it doesn't work all that well in most cases. However, here it does work well.

To use the scissors tool, just click on the four corners of the projection screen in the image, then click again on the point where you started. This makes a path around the screen -- click inside this path to change the path into a selection. You should find that the screen has been neatly selected. Now, invert the selection to select the background.

Once the background is selected, you can use the "Levels" tool to brighten the colors. (The Levels tool was covered in last week's lab. Look for it under Tools / Color Tools.)

After adjusting the levels of the background, I decided to sharpen up the image a bit by applying the "Unsharp Mask" filter to the entire image. Look for it under Filters / Enhance, and maybe decrease the "Amount" in the dialog box for this filter. Finally, I cropped away part of the image using the Crop tool.

Exercise 3. Enhance the picture of Leya's talk, leya_orig.jpg, as described here, and put the enhanced image on your Web page. Include the original image if you like. You can take a look at my sample solution to this exercise, leya_modified.jpg,


More Advanced Selections

Getting a selection just right can be difficult, you should understand that an artist might spend hours just making a selection. One way of increasing the quality of a selection is to work on a magnified view of an image. The "+" and "-" keys can be used to quickly zoom in or out on an image. The "1" key will restore it to 100%. But you also need some more advanced and accurate selection tools: the Bezier path tool and masks.

A Bezier curve is determined by a set of points that it passes through and by the tangent or direction of the curve at each of those points. The curve can have a "corner" at a point if it has two tangent lines at that point. Between the points, the curve is smooth. Bezier curves often show up in drawing programs, since they are relatively easy to work with. In Gimp, they are used for the Bezier path tool.

When used for making selections, the Bezier tool is used to make a path around the border of the region you want to select. Simply click at a series of points along the border of the region. Then close the curve by CONTROL-clicking on the starting point. You can move points by dragging on them. You can also drag any part of the curve. This will change the shape of just one of the curve segments. Initially, the points that you click are connected by straight lines. However, dragging one of the segments between a pair of points will convert that segment to a curved path. If you select an endpoint of a curved segment, you will see a "handle" on the endpoint that represents the tangent line to the curve at that point. You can change the shape of the curve by dragging the handle. When a point has two handles, you can drag them independently, giving a "corner" at that point on the curve. However, if you shift-click and drag one of the two handles, the two handles will be "locked" into a single straight tangent line.

You can delete points and handles by control-clicking them. You can add a new point in the middle of a curve segment by control-clicking on the curve. You can move the path as a whole by ALT-clicking and dragging one of the endpoints (but only if you have configured KDE to allow this, as described at the top of this page).

bezier selection The picture at the right shows the bezier tool in use. I began by clicking the five black points, and then Control-clicked on the first point. I then dragged three segments of the path, to change them into curves. Finally, I control-clicked on one of the curved segments to add another endpoint, shown here as an open circle with handles.

Ordinarily, the Bezier tool would be used to select some irregularly shaped object out of an image. But a path is not the same as a selection. Once you have a Bezier path set up exactly as you want it, you can convert it to a selection. The only way I have found to do this in Gimp 1.3 uses the Paths Dialog. If you open this dialog, you will see little icons of the paths you've drawn. (By the way, if you lose a path from the image somehow, it's still in the Paths Dialog -- you can show it again in the image by double-clicking the icon in the Paths Dialog.) To convert the path to a selection, click on the Path-to-Selection icon at the bottom of the Paths Dialog window. (The icon is a pink rectangle with a dotted border.)


Masks are a completely different sort of tool for making selections. A mask is just an alternative way of looking at a selection. A selection is really an "alpha channel" that specifies a degree of transparency for each pixel. The mask corresponding to a selection is just a grayscale image that represents this alpha channel. That is, pixels that are black in the mask are completely outside the selection, pixels that are white are completely inside, and pixels that are gray are partly in the selection.

Gimp has a cool way of visualizing and editing the mask corresponding to a selection. It's called the "Quick Mask." You can turn the Quick Mask on and off using the "Toggle Quick Mask" command in the "Select" menu. (There is also a tiny button at the lower left corner of the image window that can be used to toggle the Quick Mask -- right-click this button to see some configuration options for Quick Mask.)

When you turn on Quick Mask, the mask is displayed as a translucent red overlay on the image. Remember that this is just an alternative visualization of the selection. If there is no selection, the red film covers the entire image. If there is a selection, the region inside the selection has no red overlay. The neat thing is that you can edit the mask. When the quick mask is on, all the usual paint tools will work on the mask, not on the image. You can add to the selection by erasing part of the red film (or by drawing on it in white). You can remove pixels from the selection by painting them (with black). If you paint with an anti-aliased brush or with gray paint, you will get partially transparent pixels in the selection. To change back from the mask view to the selection, click the small button just to the left of the quick-mask button.

To use quick mask effectively, you would generally make a rough selection using the selection tools. Then turn on the mask, and edit the boundary using the paint brush or eraser. Zoom in on the image if necessary.

Here is an image that I made using selection tools and cut and paste. I don't claim that this is a particularly professional job:

buffalo in the trees

I started with an image of a buffalo and an image of some trees. Since the buffalo image was too dark, I used the Levels dialog to adjust its brightness and colors. I wanted it to look more brown than the original image. I used a combination of selection tools to select everything outside the buffalo in the image (because that seemed easier), and I used quick mask to refine the selection. I then inverted the selection, copied the buffalo and pasted it into the trees image. I used the "New Layer" command from the "Layers" menu to put the buffalo in a layer of its own.

At this point, I decreased the size of the buffalo. This can be done using the "Scale" tool from Gimp's toolbox window or by using the "Scale Layer" command in the "Layers" menu. I dragged the buffalo into position using the "Move" tool, and then the problem was to make one of the tree trunks appear to be in front of the buffalo. I needed to erase just the right portion of the buffalo. (Actually, it would have been better to "mask" that portion rather than erase it. We will probably return to the editing of "layer masks" later.)

This was actually pretty easy because of the following facts: Painting tools always apply only the layer that is selected in the Layers Dialog. So I could edit the buffalo layer without having any effect on the trees layer. (To select a layer, click its name in the Layers Dialog.) Secondly, you can edit a layer even if it is invisible or nearly so. Toggle a layer visibility by clicking on the eye next to the layer name in the Layers dialog. You can make it partly transparent by adjusting the Opacity slider in that dialog. Doing this let me see the tree trunk. I used the Bezier selection tool to draw a path around the trunk, and then converted the path into a selection. Remember that this selection is actually inside the buffalo layer, although its shape is based on the shape of the tree trunk. A cut operation (CONTROL-X) deletes the part of the buffalo image that lies in front of the trunk, making it look like the tree trunk is in front of the buffalo.

Exercise 4. Put an animal or other object in the trees. Use the same trees.jpg that I used (this is a requirement!). You can use the buffalo if you want, but you might prefer to use a shark or a fish or some other image that you find on your own. Use more than one animal, if you want. Add your image to your web site, along with the original image from which you obtained the animal or object. (This exercise will probably take you a lot more time than the first three exercises put together.)


David Eck