Linux.com

Feature

Get rid of pesky backgrounds with the GIMP

By Rob Reilly on May 03, 2005 (8:00:00 AM)

Share    Print    Comments   

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 350x330-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.

Share    Print    Comments   

Comments

on Get rid of pesky backgrounds with the GIMP

Note: Comments are owned by the poster. We are not responsible for their content.

GIMP Tip

Posted by: Anonymous Coward on May 03, 2005 11:17 PM
By default, I find the mousewheel fairly useless. Make these two changes to set the mousewheel to zoom in and out.

Start the preferences dialog by clicking:
File->Preferences->Input Controllers
Set the Main Mouse Wheel event Scroll Up to view-zoom-in
Set the Main Mouse Wheel event Scroll Down to view-zoom-out

#

Re:GIMP Tip

Posted by: hosh on May 03, 2005 11:35 PM
By default, I find the mousewheel fairly useless.


Really? By default... Scroll up/down pans the image up and down, Shift-Scroll up/down zooms the image in and out, and Ctrl-Scroll up/down pans the image left to right. I really like this setup.

#

Use Paintbrush instead of Eraser

Posted by: Anonymous Coward on May 04, 2005 02:31 AM
QuickMask is indeed a god-send! (I use it all the time to "Desaturate" a background, while leaving my subject in full color.)

One suggestion I would make would be to use the Paintbrush Tool instead of the Eraser.

If you have your colors set to the default black and white (Which is trivial: simply click on the black and white overlapping squares next to the color chooser) you can "Black Out" the areas you want to mask [go away] and "Whiten" the parts of the mask you want to keep.

Then, using the 'X' key, you can swap FG <-> BG to alternate between black (mask) and white (expose). It sure beats having to get out of QuickMask mode, Invert, get back INTO QuickMask, fix, etc. etc. etc. especially when you're working at very high detail.

Start with a large 19x19 fuzzy brush to make macro-level changes, then fine-tune your edges with a 5x5 or 3x3 brush as necessary.

When you're done, don't forget to save your selection to a channel -- that way, when you want to make Just One More tweak to your image, you won't have to start all over again.

Another tip: You can use the "Select Shapes from Image" Tool to quickly create a path around the object you'd like to mask (And hit 'Shift+Q' to turn it into a QuickMask). Then most of the 19x19 work is already done. You'll still want to make some adjustments by hand, but it's a nice way to knock out 95% almost instantly.

Great article! GIMP rocks!<nobr> <wbr></nobr>:-D

#

Bleah

Posted by: Anonymous Coward on May 04, 2005 03:09 AM
1) what is this artcle doing in Newsforge?

2) learn to use trim paths and save yourself both time and that nasty green tint from the "green screen"

#

Re:Bleah

Posted by: Anonymous Coward on May 04, 2005 04:27 AM
Why do multiple select/cuts? Use the Shift modifier key to ADD to the existing selections, and do it all at once. Use the lasso tool with Shift(add) and Ctrl(subtract) modifiers to do any edge detail work. Use Selection->Shrink or Grow (a few times if needed) to "soak up" any small areas of selection that aren't appropriate and blend the added selects. If you want to do some smoothing later, save the selection to a path and then stroke the path with smudge or convolve tool and an appropriate brush.

#

Re:Bleah

Posted by: Anonymous Coward on May 04, 2005 04:55 AM
How do I use trim paths? Where can i find more info on this?

#

Awesome

Posted by: Anonymous Coward on May 04, 2005 02:01 PM
Hey, I've been using Gimp for years and always masked things out by hand. It takes a long time for no reason. This is a great Idea!

#

Time Needed

Posted by: Anonymous Coward on May 05, 2005 02:52 AM
ETPI = Estimated Time Per Image
NI = Number of Images

NI * ETPI = TDL

TDL = TOO DAMN LONG

Batch process 100,000+ images to remove backgrounds in reasonable amount of time. Then I will be more impressed.

#

Good stuff

Posted by: Anonymous Coward on May 05, 2005 05:40 AM
I use quick mask in Photoshop all the time, but didn't know it existed in Gimp... thanks for the tip.

#

Filters | Colors | Color to Alpha

Posted by: Anonymous Coward on May 06, 2005 12:38 AM
This way, edges get anti-aliased by the alpha channel. Any green reflected on the object gets an alpha component too, so you can drop a different colored background behind it to "replace" the reflected color.

#

Get rid of pesky backgrounds with the GIMP

Posted by: Anonymous [ip: 71.70.212.19] on November 17, 2007 11:53 PM
For the masking technique: An easier way to place the image onto a new layer is to copy it and then go the the Edit tab and select Paste as New.

#

Get rid of pesky backgrounds with the GIMP

Posted by: Anonymous [ip: 65.125.138.247] on January 13, 2008 12:44 PM
thanks for article. as a new user of gimp and an newbie when it comes to more detailed work you have given me a better understanding of masks and one method to accomplish it. and thanks to all you who have posted comments to this article your posts have also increased my knowledge and given me more ideas. thank you all.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya