Replacing a Foreground with The GIMP

I get lots of email from GIMP users who have discovered my tutorials. Sometimes they pose interesting questions that get me thinking. One fellow wanted to take photos of children in front of a chroma key background (usually some shade of blue) and then cut out the kids and place them in christmas-y background scenes. Thinking about this a little bit got me interested in taking another look at replacing the background in a photograph. The difference this time around is the chroma key background. It should be a lot easier to select the subject when you can control the background like that. In this tutorial we’ll explore a different method for isolating/selecting the subject, cutting it and pasting it into a different image, thus replacing the foreground of the new image.

Before After

The Procedure

Since I don’t actually own a “real” chroma key background, I used my house as a substitute. A real chroma key background should work even better than this.
Might as well get the bad news out up front. I know of no perfectly simple, repeatable technique for one-click selection of complex subjects (like a person) in a photograph. There may be products (like Knock-out for Photoshop) that make easier work of things, but even under the most controlled contitions this is usually a multi-step affair. That said, the GIMP provides some decent tools for getting the job done, and with a little practice you should be able to get this part down to a few minute exercise. I’ve done precious little of this kind of selection and it only took me a few minutes, so take heart.The key to doing this with a near constant color background that is different from the subject is to make good use of the fuzzy selection (aka “magic wand”) tool, followed by judicious use of quick mask to touch up the selection.

Double-click on the magic wand () to bring up the Tool Options dialog. You’ll have to experiement here at first to find the right setting for the Threshold value, which is the amount of “fuzziness” to apply to the color you click on in defining a contiguous region. Remember, the background will probably have some creases, wrinkles, lines, reflections, shadows, etc. that all decrease the uniformity of the color. You’ll have to find a Threshold value that’s about right for your background, subject and lighting conditions. Once you’ve set the Threshold, click in the background somewhere to set up the initial selection. You probably will get something similar to that shown at right. There will probably be regions of the background that aren’t selected and regions of the subject that are. That’s OK. Just as long as you get a decently large chunk of the background and not too much of the subject, all will be fine. If you need to, Undo (Ctrl+Z), adjust the Threshold in the Tool Options and try again.

Now begin an iterative process of adding to the selection. What you want to do is hold down the SHIFT key and click again on an area of the background that didn’t get selected, but should have been. The SHIFT tells the GIMP to add the new selection to the previous one. In the previous step you can see that there are a couple of areas under each arm and by the neck where the boy’s shadow caused the color to deviate enough that the first fuzzy select didn’t pick it up. Clicking once on the neck shadowed area while holding down the SHIFT key results in the selection is shown at right. It’s pretty darn good. There are areas of the boy selected that shouldn’t be, but I’ll clean those up next. In this case, one additional selection was enough; you may need to repeat for additional areas.
Now to manually touch up the selection. For this the Quick Mask tool is the ticket. Convert the selection into a quick mask by clicking on the little red square in the bottom left corner of the image window. You should see something similar to that shown at right.Think of a quick mask as just another way to view/manipulate a selection. The parts that are not selected are shown in a red tint. To change the selection, just paint onto the image with black or white: black for no selection, white for selection. You will see that instead of painting actual black or white, you get red-tinted or not. (Side note for the curious: selections, channels and masks are all really just 8-bit grayscale images! The difference is that normally you don’t see them that way. Knowing that, however, lets you understand how to manipulate them using the ordinary GIMP toolset.)

Looking at this quick mask I can see that I’ve got a pretty great selection and just need to remove some parts of the boy that were selected. I pick a largish, fuzzy brush from the Brushes dialog (Shift+Ctrl+B), click on the Paint tool (), zoom in a bit (++), make sure Black is selected as the foreground paint color, and begin painting over spots that should not be selected. Pan around the image, fixing up all the spots that need it. If something is not selected that should be, switch to white (x) and paint it. If you make a mistake and paint into something that should not be, switch (x) and paint it. Switch to a smaller brush size near the edges.

With just a little practice you can fix up a selection like this in a few minutes. When you are done it should look something like that shown at lower right.

Now convert the quick mask back to a selection by clicking on the little selection button just to the left of the quick mask button in the lower left corner of the image frame. You should see the image switch back to the “marching ants” selection outline.Now invert the selection (Ctrl+Ior Selection/Invert). This makes the selection the subject rather than the background.

At this point you can feather the selection if you wish. I did not do so here.

Finally, copy the selection (Ctrl+C).

If you don’t need to resize or do any other work on the image being pasted, proceed to the next step.Otherwise, do a New (Ctrl+N) and go with the defaults, except choose “Transparent” as the fill type. In the new image, Paste (Ctrl+V) and Anchor the image in the Layers dialog.

Now manipulate the image as necessary (e.g. Image/Scale), then Select All (Ctrl+A) and Copy (Ctrl+C).

In this case I didn’t need to do anything special to the pasted image.

Open the new background image (Ctrl+O).Open the Layers dialog (Ctrl+L) and make sure that the new (background) image is listed in the drop-down box. Right-click on the Background layer and select New Layer (there is also a button for this in the bottom button bar of the Layers dialog ().

Here I named the new layer “Subject”. Choose “Transparent” for the fill.

Go to the new image window containing your desired background and Paste (Ctrl+V).Choose the Move tool () in the GIMP toolbox. Move the pasted image to the desired location in the image. In the Layers dialog, anchor the pasted image by clicking on the anchor button.

Voila! Finished image.


  • For this kind of fine paint work, a pen tablet like this Wacom one is very useful. For photo retouching you don’t need any bigger than a 4×5 model.


  1. Hi does gimp have pucker tool and freeze mask tool in photoshop. What is the equivalent…Also is it possible to enlarge the image working with the enlarged image when liquifying/iwarping it?

    Comment by Andrew Plesner — June 27, 2011 @ 9:21 am

  2. I’m not sure what a pucker tool or freeze mask tool is so I can’t help you there, but as to the enlarged image question, look into opening a new view on the image and enlarge the window and/or increase the zoom in it. This function is available as “New View” from the View menu.

    Comment by Eric Jeschke — June 28, 2011 @ 3:09 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: