How to create detailed vector traces in Inkscape
I recently read a great tutorial over at Paul Hill's Creative PSD blog in which he demonstrates a technique for using Adobe Illustrator to achieve a detailed vector trace of a source photograph of a tree. My first thought when reading really good tutorials like this is "How would you do it using Open Source software?", and here is my interpretation of Paul's method using Inkscape and GIMP.
There are a few techniques for doing this, including some quick-and-dirty tactics which will get you a good outcome faster than I'll show you here, but what I describe is a solid method and should get you a useable result for a variety of images.
Although the end goal is to get an accurate vector object from Inkscape, a lot of the up-front work is done in GIMP, to maximise our chances of getting a faithful reproduction of the tree outline, including the all-important internal spaces which define the shape. Again, this is a practice which can be applied to a range of images you choose, and isn't constrained to this tutorial. Hey, I'm trying to equip you with life skills, here.
Let's take a look at what we'll be producing at the end of this tutorial.
Tall Oak Tree image Credit: Philip Halling; Wikimedia Commons.
Skills you'll be developing in this tutorial
- Raster image manipulation and workflow planning for vector conversion
- Using GIMP's colour management tools for adjusting contrast output levels
- Bitmap tracing in Inkscape and controlling the precision of the result
Step 1 - Find a tree photo and adjust the contrast in GIMP
It's important to realise that there are limitations to any software's Bitmap Trace tool, and certain photos will be more difficult to achieve a good result from than others. In general, if you have the opportunity to use a photo where your subject and its background have a reasonable degree of separation in either colour or brightness, you should opt for that one.
So with that in mind, locate the tree photo listed in the Resources section, and open it in GIMP using File --> Open or by dragging into one of GIMP's windows straight from your download area. We'll use GIMP to establish the right conditions for Inkscape's trace tool to do its job well.
Locate the Layers panel (Ctrl and L to make it appear). Duplicate the image layer using Ctrl, Shift and D on the keyboard, or by clicking the Duplicate Layer button at the bottom of the panel.
We're going to use this layer copy later, so hide it for the time-being by clicking the eye icon to make it invisible for now.
Next, make sure you've got the original layer activated by clicking on it in the Layers panel so it's highlighted. Then select Colors --> Brightness-Contrast from the menus and, in the dialog box that appears, move both the sliders to the right to boost the contrast of the photo quite a bit.
This may result in your photograph becoming 'false' in the sense of true colours being spoiled, but what we're looking to do is to establish good, clear separation between the tree and its background, without disturbing the clarity of the edges of the outline and inner spaces.
Step 2 - Use a copy of the image to create a layer mask
Make the copy of your original image visible again by clicking the space where the eye icon was, and click the layer name itself to make it the active one, then click Colors --> Desaturate and select Luminosity from the three radio button options.
Now we need to enhance the contrast on this layer even more than before, to create a very strong black-and-white version of the image. We will use the result as a Mask for our initial cut-out.
The most reliable way to do this is to use the Levels feature, which is located under the Colors --> Levels menu. Drag the mid-point and white-point arrows down to around half way across the Input Levels range. You can then move the black-point arrow right slightly.
You'll see that almost all of the grey tones become either clearly white or clearly black, depending on which side of the mid-point they fall. By closing up the levels, we've created a sharply-contrasted monochrome version of the tree, which we'll use as a mask in the next step.
Copy the pixels in this layer with Ctrl and C (or Edit --> Copy), then hide the layer so that the colour version is visible again. Right-click on the visible layer in the Layers panel and select Add Layer Mask.
Now Paste (Ctrl and V) the black and white image of the tree into this Layer Mask (as you've just added the mask, it will be active by default). You'll see a new, temporary layer appear at the top of the layer stack. Press Ctrl and H or right-click and select Anchor Layer to anchor the pasted image to the Layer Mask area.
Whoa! That doesn't look right at all. But don't worry: it's just the inverse of the cut-out we need, according to the rules of GIMP's layer masks. With masks, black pixels are like a piece of opaque card laid over a painting, and white ones are like holes cut out of it which allow us to see the picture underneath.
Therefore, if we swap the blacks and whites we'll have what we're after. So, with the Layer Mask still active, hit the Colors menu and select Invert to do exactly that.
Ah-ha! Much more like it and, as you can see, there's just a little bit of work left to do to get a great cut-out ready to be traced.
Pick a soft brush and paint (keyboard P to activate the Paintbrush tool) the layer mask with some more black pixels to remove the areas which, because they had little or no contrast against the tree's colours, are still present. I chose a circle brush with a Hardness of 50%, and a size of around 30 pixels.
Zoom into the areas you need to and brush around until the excess areas become transparent around the central subject of our tree.
Usefully, because we're not painting on the image itself or deleting bits of it, we can always go back and improve the selection by changing the paint colour and brushing over an area again. Keep brushing until you've isolated the tree nicely from all the background regions; lower the size slightly for fiddly regions and raise it again when you're in the clear and need to brush large areas quickly.
When this is done, activate the Crop tool (Shift and C on the keyboard) and drag a crop area around the tree, just so we can prune off (ha!) all the unneeded blank space around the borders. Press Enter or click inside the crop area with the mouse to complete the crop.
That's looking like something we can use, so Export the image with Ctrl, Shift and E or File --> Export and save the output file to a memorable location in PNG format. Remember to add the .PNG extension to tell GIMP we want to keep that transparent background. You can accept the default settings in the PNG export dialog box that pops up after the Export Image one closes.
After that, it's time to move over to Inkscape to finish the job.
Step 3 - Import the photo to Inkscape and trace the bitmap image
Open up Inkscape and bring up the Import Image dialog box with Ctrl and I or File --> Import. Locate the PNG you exported from GIMP and click OK. In the next box that appears, choose Link - this will enable you to keep GIMP open in the background, make changes to the photo and re-export, then see the image update in-situ within Inkscape straight away, which is obviously very useful.
You should see your roughly-cut out photo on the page. With it selected, open up the Trace Bitmap dialog box with Shift, Alt and B on the keyboard, or Path --> Trace Bitmap in the menus. You'll see the box pop up with a range of settings and options split across two tabs. I'll go through what they mean below.
On the first tab, Brightness Cutoff is the Mode we will use in this instance. It instructs the trace process to look for the edges of the tree where pixels are darker than a certain threshold; that's why we picked this image originally and why we edited it in GIMP first. A high number in the box gives you a shape which covers most of the non-white areas, whereas a lower setting only traces the very darkest regions and leaves the rest alone.
You can see the effect that changing the Brightness Cutoff threshold value has, and that's the majority of the work, to be honest, but there are some useful settings on the Options tab of the Trace Bitmap dialog box that are worth some attention, too.
- Suppress Speckles reduces the amount of very small areas which are included in the traced result. A high value produces a result with fewer nodes, at the expense of detail. 1 is a sensible value to use here
- Smooth corners determines how rounded the curves through the output's nodes are. A high value means more rounded edges to your resulting shape; a low value can result in overly jagged traces. 0.30 works well here
- Optimise paths attempts to keep the number of nodes in the resulting trace down (higher value), at the risk of becoming less faithful to the original image. A lower setting results in a far greater number of nodes (thereby penalising performance), but potentially a more accurate trace. 0.2 is a good value to use here
The settings I show you should be used as a guide, and you'll need to use your own judgement as to what looks best according to your own requirements. The Brightness Cutoff value I settled on in the end was 0.420.
Once you've got your trace, it's worth looking around for any parts of the result that need some extra attention. For example in mine, I've got a few regions that have been traced but fall outside the main tree shape, which looks a bit untidy.
Simply activate Inkscape's Node tool, Zoom in substantially so you can work up close, and drag a selection around the offending floaters. Alternatively, click on them in turn while holding down the Shift key if a rubber band selection isn't practical. Then hit the Delete key to remove them from the trace. Repeat until you've got a clean shape without artefacts.
And there you are - a totally vector, monochrome trace of your complicated source tree image. By all means experiment with the steps in this process. For example, using Multiple scans of the source image's Colours can produce a nice effect.
You might also like to edit the trace shape slightly, to elongate the tree trunk and make it appear less stumpy and more elegant. This can be done with the Node tool like before, but instead of deleting the nodes, I selected the nodes at the bottom of the trunk and dragged them downwards slightly.
In isolation, we should have something like:
That's not looking too bad at all - I can now take this tree silhouette and incorporate it into a project where it's needed, scaling it to any size I like without losing the detail because it's a vector image.
And if you want to know how to use your silhouette vector to make something like that, well, you'll just have to stay tuned and see what I've got in store for you soon. :-)
Has this tutorial left you with a great impression, or did it just disappear without a trace? Let me know by leaving a comment in the section below.
comments powered by Disqus