Ceci est une ancienne révision du document !
1
Over the past five instalments, we've used Inkscape to trace bitmap images, both manually and automatically. In part 17 I introduced a sketch of a cartoon character, “Frankie”, and proceeded to demonstrate a few ways to manually trace it. The first method was simply to draw over the outlines using the normal Bézier Path tool, which resulted in a drawing like this: Seeking some variation in the outlines, I then proceeded to introduce a number of ways to trace the sketch, but they all suffered from one obvious omission: color. Although they introduced some dynamics to the image, they all resulted in the outline being rendered as a single filled path. The best looking result arguably came from automatically tracing an inked and cleaned-up version of the image (see part 19 for details), but that still produced only a nice looking outline consisting of a single complex path with over 1000 nodes. Changing the fill on this image would just result in a colored outline. In order to color the drawing itself, it's necessary to create each area of color as a separate path that can be placed below the outline. In other words you have to manually re-draw each area of the image to create a set of paths that can be filled. If it sounds like a lot of extra work, that's because it is, but Inkscape does have a Paint Bucket tool that can help.
Au cours des cinq derniers épisodes, nous avons utilisé Inkscape pour tracer - manuellement et de façon automatique - des images bitmap. Dans la partie 17, j'ai présenté l'esquisse d'un personnage de BD, « Frankie » et j'ai démontré quelques méthodes pour la tracer à la main. La première était tout simplement de dessiner par-dessus les contours en se servant de l'outil standard des courbes de Bézier (« the Bézier or Pen Tool »), ce qui a donné quelque chose comme ceci :
À la recherche de variations dans contours, j'ai alors présenté plusieurs façons de tracer l'esquisse, mais toutes souffraient du manque évident de la couleur. Bien qu'elles aient introduit quelques dynamiques à l'image, le résultat en était que le contour fut rendu comme un seul chemin rempli. On peut soutenir que le traçage automatique d'une version encrée et nettoyée de l'image a donné le meilleur résultat (voir la partie 19 pour les détails), mais cela n'a produit qu'un joli contour composé d'un seul chemin complexe avec plus de 1000 nœuds.
Si l'on changeait le remplissage de cette image, on n'aurait comme résultat qu'un contour coloré. Pour pouvoir colorer l'esquisse même, il faut créer chaque zone de couleur comme un chemin distinct qui peut se placer sous le contour. Si ça a l'air d'être pas mal de travail supplémentaire, c'est parce que ça l'est, mais Inkscape possède un outil pour remplir des zones bornées (« the Paint Bucket tool ») qui peut vous aider.
2
The Paint Bucket – sometimes referred to as a Fill or Flood Fill tool – is commonly found in bitmap editors such as The GIMP, so you may already be familiar with its basic operation. In Inkscape, it's present in the main tool palette and can be selected using the Shift-F7 keyboard shortcut, by pressing the “u” key on the keyboard (easier to remember if you imagine it as a sort of bucket shape), or by clicking on the icon. Once selected, clicking the mouse inside a “bounded region” in your drawing will create a path that fills it with the currently selected fill and stroke. In simple terms, a bounded region just means an area of a single color that is completely surrounded by a differently colored border, with no breaks. In practice the color of the border must be significantly different from the area you are filling. The region to be filled is actually calculated based on the color of each pixel in the area, so it's a bitmap operation rather than a vector one. The pixel you click on is taken as the starting point, then the algorithm tries to expand outwards by considering adjacent pixels. If a pixel's color is close to that of the starting pixel, then it gets added to the fill region and the algorithm continues by considering the adjacent pixels of the newly enlarged region. If the pixel's color is significantly different from that of the starting pixel (i.e. it's the boundary color), then it's not added to the fill region and the process stops trying to grow in that direction. This is repeated until the fill region can't grow any further because it has hit the boundary on all sides. Finally Inkscape creates a path that surrounds most of the pixels in the fill region, converting the bitmap-based search into a vector result.
The Paint Bucket - parfois appelé l'outil de remplissage ou « Flood Fill tool » - se trouvent fréquemment dans des éditeurs de bitmap tels que GIMP et il se peut que vous sachiez déjà comment il fonctionne. Dans Inkscape, il se trouve dans la barre d'outils principale et peut être sélectionné au moyen du raccourci clavier Shift-F7, en appuyant sur la touche « u » du clavier (la forme de cette lettre ressemble un peu à un seau (ou « bucket »), enfin en cliquant sur l'icône.
Une fois l'outil sélectionné, si vous cliquez à l'intérieur d'une zone limitée (« bounded region ») un chemin qui le remplit du Remplissage et contour actuellement sélectionné. En termes simple, une zone limitée signifie tout simplement une zone d'une seule couleur entièrement entourée, sans rupture, par une bordure d'une autre couleur. En fait, la couleur de la bordure doit être sensiblement différente de celle de la zone que vous remplissez.
La zone à remplir est en fait calculé sur la couleur de chacun de ses pixels et ainsi c'est une opération bitmap plutôt qu'une opération vectorielle. Le pixel sur lequel vous cliquez est censé être le point de départ ; ensuite l'algorithme essaie de s'étendre vers l'extérieur en analysant les pixels contigus. Si la couleur d'un pixel est proche de celle du pixel du départ, il est rajouté à la zone de remplissage et l'algorithme continue en analysant les pixels voisins de la nouvelle zone élargie.
3
If all that talk of algorithms has got you confused, a simpler way to imagine it is as though you're trying to pour ink into a shallow dish. The ink will spread out along the base of the dish – but only where the base is flat and even enough – and stop when it reaches the edges. Similarly the bucket-fill algorithm tries to spread the fill region out – where the colors are similar enough – and stops when it reaches the contrasting boundary.
Taking a closed star as our object to be filled, zooming it to fill the screen, then clicking anywhere in the white interior, will produce something like this:
In this case the tool was set to a dark red fill and no stroke, and we've managed to create a new path which approximately fills the outline. It's only approximate because the new path doesn't actually reach the edges and corners of the star, leaving a thin gap that shows up when zooming in within Inkscape. This is a common problem with the bucket fill tool, but it can be alleviated to some extent by setting the “Grow/shrink by” option on the tool control bar to a positive number. This will cause the calculated path to grow outwards so that it overlaps the boundary a little. Set it too high and it will extend beyond the boundary, so a little trial and error is often needed. You can also use a negative number in order to shrink the path away from the boundary if you want to. Here's a close-up of the top point of the star with different grow/shrink values.
4
As our aim is to color a comic sketch, it's the middle option – a positive grow/shrink value – that interests us at this stage. As you can see, the new path extends well into the border but we can send it to the back of the z-index, bring the outline to the front, or draw our color fills on a lower layer, to give us our original outline with the appearance of a filled interior. It's a quick and easy way to fill some of the larger areas of a character like Frankie.
It's not a bad start, but there are a few problems. Some of the fills don't quite get into all the corners, or leave gaps near the outlines, and the bucket fill didn't work at all on the jumper and the nut on the right hand side. Let's start by looking at the fill for the face in isolation, temporarily changing the color so it stands out a little more.
There are 96 nodes in this path, but many of them aren't really necessary. We don't need it to follow the outline of the mouth and nose, and we certainly don't need to trace every bit of stubble. Even the eyes aren't necessary as we can simply stack their own bucket-filled paths on top of the face path. With a bit of node editing it's easy to simplify this complex path. Adding and subtracting rectangles, ellipses or other paths using the Boolean operations is a quick way to deal with lots of nodes at once. In this case I'll simply draw an approximate path using the Bézier tool (shown in green), then use Path > Union to combine it with the face.
5
Putting this path back into place shows that there are still some gaps around the edges. We can correct this just by dragging a few nodes and handles into position – with the path now significantly simplified this is a much easier job than it would have been previously.
The bucket tool often has a problem with concave corners. One way to reduce the issue is to zoom in on the object you're filling. This results in more pixels being used in the flood algorithm, giving a more accurate fill. Unfortunately it only works for small objects because the whole, unbroken boundary has to be visible in the Inkscape drawing window, or at least only slightly outside it. Otherwise you can fill at a low zoom level, then get in closer and fill again to finish off the corners. If the first fill is still selected you can hold Shift to cause the second fill to be added to the same path, or you can simply create them as two paths then use Path > Union to merge them into one. Most commonly, though, it's easiest just to do a little manual node editing to get the path to fill the corners.
Taking a closer look at the nut and bolt, it's clear that the problem here is a gap in the sketch which means we don't have a completely bounded region to fill.
6
If your boundary has small gaps in it, then don't despair: the bucket fill tool has a “Close gaps” option on the tool control bar that allows it to automatically deal with such issues. This feature can be set to close small, medium or large gaps, or turned off entirely. Be aware that closing gaps might also prevent it filling legitimate parts of the drawing if you have a complex boundary that pinches together in some areas. Even the “large” setting only closes gaps of a few pixels in size, so you may find it works only if you zoom out a bit – which in turn gives you less accuracy in the corners.
An alternative approach is to manually close the gaps. Remember that the fill algorithm is concerned only with how different each pixel's color is from the initial start point. That allows you to use a contrasting color to draw lines or other objects to plug any gaps before filling. I usually draw these plugs in a color that also contrasts with the boundary so that they stand out afterwards and are easy to find and remove. In this case I've drawn a red triangle using the Bézier tool as my plug: it takes only a few clicks to create, but the size and color makes it obvious that it's an object to remove once it's served its purpose. The color is different enough from the white background that the algorithm will just consider it to be part of the boundary.
Manually plugging gaps lets you use the bucket fill while still being zoomed in close enough for it to get into the corners. In this case, however, there are enough separate areas that some extra manual work would be needed to color it all anyway, so it's just as easy to manually fill the whole shape. By using the Bézier tool to lay down straight lines that follow the center of the outline it's quite simple to color a small region such as this. Once the Bézier path has been drawn, sending it to the back will hide the straight edges behind the outline to retain the hand drawn final appearance.
7
The bucket fill tool also has a few other tricks up its sleeve. Because it works on pixel values, it can be used to fill areas of a bitmap image, even without tracing it first. In that case the background color can often be less than uniform due to artifacts introduced with JPEG compression, so the bucket fill has a Threshold setting on the tool control bar to adjust the amount that a pixel can deviate from the initial starting color for it to still be considered as part of the background. Adjusting this setting lets you fine tune the fill to only very specific colors, or broaden it to allow quite a range. As well as filling bitmaps, this can be handy when you want to fill an area that has a gradient or other color variations.
You can also change the basic rule of the algorithm entirely using the “Fill by” pop-up menu. Instead of looking for general changes in pixels' colors you can choose to focus on only the red, green or blue components, the hue, saturation or lightness, or even the alpha channel. These options are rarely used, but could be invaluable when your background and boundary aren't distinct or contrasting enough for the standard algorithm to notice the difference between them.
After another manually drawn path for his jumper, and a little node work to remove and re-create the veins in his brain, it's finally time to reveal the finished version of Frankie. Having to separately color each section of a sketch can certainly be time consuming, but if you compare this version with the simple manual trace presented at the start, I hope you'll agree that the extra effort required to maintain some variation in the outline has been worth it. Whilst the manual trace has a decidedly vector feel to the image, this final version retains a lot more of the hand-drawn style.