Get rid of pesky backgrounds with the GIMP

151

Author: Rob Reilly

Putting your CEO’s mug in an OpenOffice.org Impress slide show is pretty easy — just call up marketing and get them to email you the appropriate graphics file. But what if you’re the one charged with creating that portrait for the marketing department? This article will guide you through the process of eliminating a background from a graphics file, leaving just the object you want. You don’t have to be a professional photographer or graphics artist to learn this. All you need is a digital camera and the GIMP.

There are a couple of ways to erase bit-mapped image backgrounds while leaving only the desired object:

  • You can erase the background using various eraser tools.
  • You can mask just the parts you want to keep and subtract the rest.
  • You can combine these two, to speed up the process.

Using the eraser tools

Erasing everything surrounding your object is the most time-consuming way of getting rid of a background. To do this in the GIMP, double-click the Eraser tool (the pink eraser icon) and select a brush size. Use a big round circle brush to erase a lot; use a small circle for fine work. Move over to the image and begin erasing around the perimeter of the object that you want to keep. Zoom (under the View menu) the image in for detailed work and out to see how the overall image will appear.

Go all around the outside of the object, creating a buffer zone. Remember, click to erase, move, click to erase, and so on. After you’ve completed erasing the buffer zone you can switch to a larger brush and erase the rest of the background.

A good way to control what gets erased is to move the mouse over what you want to erase and make a single click. If you erase more than you want, use the Undo command under the Edit menu. Don’t hold down the left button and move the mouse at the same time unless you want to erase in a line or over large areas. If you erase around your object this way and your hand slips, you’ll have to Undo and re-erase a bunch of pixels.

If the whole process sounds pretty tedious, well, it is. That’s why graphics professional rely more on a technique called masking.

Mask that background

Masking is sort of the inverse of erasing. It’s a little less tedious too but it can give you high-quality results if you take your time and are careful.

Think of masking as putting a sheet of translucent paper over your image and then etching out just your object. You create a mask where your object peeks through the paper. Once you’ve masked your object, you turn the rest of the image into a transparent background. Here’s how you do it in the GIMP.

Go to the Select Tab and choose Toggle Quickmask. A red “mask” will cover your image. Click the Eraser tool and erase the parts of the mask necessary to reveal the object you want to keep. Your object will “get clear” as you remove the mask. If you go outside the lines of your object, click Select and then Invert, then click Select, Toggle Quickmask, and use the eraser again to “put back” the mask. Flip back and forth using the Toggle Quickmask and erasing until only your object is clearly visible. You can try different brush sizes to aid in fine detail, and zoom in or out to help you see what you’re doing. Use the Undo feature and single mouse-click erasing to minimize mistakes.

Finish up with one last Select and Toggle Mask sequence. You’ll see the “marching ants” animated border around your object and the red mask disappear. Under the Edit tab choose Copy to grab your object. Move over to the Layers dialog and create a new layer with a transparent background, using the page icon all the way to the left of the garbage can icon. Click on the new layer (called Empty Layer) and under the Edit tab select Paste to put your object on a pasted layer.

Figure 1: Green screen and laptop
Figure 1-2: Cropped Figure 1

Click the Pasted layer, then right-click and select Anchor Layer to anchor the pasted layer to the empty layer. Your object should appear on the Empty layer thumbnail graphic. If the gray and black checkerboard appears on the thumbnail in the shape of your object, you’ve copied the green screen background by mistake. Grabbing the object that’s masked can be a little tricky, and it’s easy to invert your selection. Undo the steps back through the copy command, or you’ll loose your masked object. Under the Selection tab, choose Invert, and do the copy to new empty/pasted layer sequence again. When you have your object in place (on the empty layer), click on the Background layer and delete it.

You should be left with just your object and on transparent background. Save the file, answering yes if you’re asked to flatten the image, because JPEG files can’t have transparent layers. You can now insert the file into any other type of graphics file.

The use of both the eraser tools and masking can get complicated by complex or colorful backgrounds. If you can isolate your subject, perhaps in a makeshift studio, you can save yourself some of the headache by removing most of the background clutter to begin with. That’s where we use a green screen.

The magical green screen

Many professional photos and video are shot with the object in front of a green (or sometimes blue) screen so that the color can be removed later, leaving just the object. The neon green or blue color usually isn’t found on the object, so it’s easy to subtract it digitally from the photo.

You can use any type of color digital camera with green screen masking. I used my HP iPAQ rx3715 with the built-in 1.3 megapixel camera for the example pictures. It’s fast, convenient, and takes pretty good outdoor scene shots, though it leaves a little to be desired for close-up work. Generally, the better the camera, the bigger the picture can be without sacrificing resolution. I use my iPAQ for 350×330-pixel Web photos and it works well.

Start out by visiting a local fabric store and pick up some neon green fabric. It comes in widths from about 44 to 54 inches and is sold by the yard. I chose a polyester/linen material that was flat on one side and shiny on the other, with a tight weave. I purchased two yards for a whopping $3.82, with a coupon. You’ll want a material with a consistent color across its width.

You can make a screen support frame using some 1-inch plastic pipe and fittings. Drape the green screen over the frame using cheapo 4-inch spring clamps. The green screen should cover the horizontal surface where you’ll position the object. In my examples, the green screen was clamped to a wooden frame, with my HP notebook ready for shooting (see Figure 1). I used a couple of thick Linux books to hold the screen down.

Natural light works well for green screening. The main thing to remember is to try to get a consistent color. Any folds, shadows, or spots will make the green color removal process more difficult and time-consuming.

Figure 2: First “cut” of removing green screen.

Once you’ve take your photo and downloaded it to your graphics workstation, you’re ready to edit it with the GIMP. Use the Select Rectangular Region tool (outlined box icon). Click the upper left corner of the selection box and drag it across your object to the bottom right. Make sure the selection box is just a little bigger than the object. Under the Image tab choose the Crop Image item. This will minimize the amount of green background that you need to cut out.

Double-click the Select Regions By Color icon (color bar with a pointing hand) to bring up the option box. Make sure the Replace The Current Selection mode button is pushed. Check the Feather Edges box and adjust the Radius to a value of 10. Under the Find Similar Colors heading, adjust the Threshold to a value of 50. Then move the mouse to the graphic window and click anywhere on the green background. Under the Edit tab, choose Cut. You should see quite a bit of the green screen disappear (see Figure 2). Ideally, one cut should get rid of the whole background, but variations in color make that tough. So, you’ll usually have to repeat the process with another green area. You should be able to remove nearly all of the green background in three or four cuts. You can touch up any last odd bits of green with the eraser tool.

It takes some practice to figure out the best Feather Edge Radius and Find Similar Color Threshold values. Every photo will be different, and the number of Cut iterations will vary. But it sure beats going around the object manually with the eraser tool.

Lastly, copy your object onto a transparent layer and save the file, as we did earlier.

Next steps

You’ll find all kinds of uses for graphics without those pesky backgrounds. You could take images of several of your company’s products and put them together into a nice marketing montage picture. You can use background-less objects to give a sense of depth to a graphic. You could even put your CEO’s picture on somebody else’s body, as a spoof for that big company presentation.

Laptop pasted on a scene

Well, maybe not that last one.

The techniques of erasing, masking, and using a green screen are pretty easy to implement using the GIMP, and you don’t have to practice too much to get good results.

Rob Reilly is a consultant, writer, and commentator who advises clients on business and technology projects. His Linux, portable computing, and public speaking skills-related articles regularly appear in various high-end Linux and business media outlets.