Ceci est une ancienne révision du document !
I’ve looked at using clipping paths in Inkscape on several occasions in this series. This time, I’m going to hark all the way back to part 32, which appeared in FCM #92 in December 2014! In that article, I looked at some clipping techniques that could be applied to any type of object, but which were particularly suited to bitmap images. I used this familiar lady as my subject.
The highest resolution version of this file weighs in at nearly 90MB, so it’s not the sort of thing you’d want to embed into an SVG file – especially not multiple times. One solution to this is to link the image into your Inkscape document rather than embed it, but there are occasions when embedding is the better option. The classic example of this is when you want to share the file with somebody else. Inkscape will also sometimes use absolute rather than relative paths when embedding images, which makes them less practical to move between systems. You can manually edit the paths to make them relative, which helps the situation a lot, but you’re still left with an Inkscape document that actually consists of two or more files that have to be moved as one. Embedding images solves all these problems, but this comes at the cost of a larger file, since it now contains a copy of the bitmap data in a text-based encoding that bloats it even further.
The main subject of part 32 was therefore how to avoid embedding multiple copies of an image in your document when you need to reference different parts of it. The example provided was of an information leaflet that contains clipped images of La Giaconda’s head and hands.
Despite appearances, the document in this screenshot contains only a single copy of the original image file. The secret is well known to Inkscape veterans, but is perhaps the single most useful tip I can provide when working with clipping paths (or masks): the content to be clipped is placed inside a group.
That alone isn’t enough to get this effect, but it’s a prerequisite that makes for a good tip in general. With the clipping path applied to the group rather than the image itself, you can still double-click to enter the group, then move the image within it to change the point of focus. What you can also do is clone the image within the group, then cut it to the clipboard, exit the group, and paste it somewhere else (even on another layer). Because you’ve cloned the image itself, not the clipped group, you get a complete copy of the image that can be further manipulated. In this case, the requirement was to clip the clone to show only the hands, and position it beside the head.
While we’re on the subject of clipping tips, another useful tip is to always convert your clipping shape into an actual path if it’s not already one. That means any rectangles, ellipses, stars, or regular polygons, that you want to use as a clipping path, should generally be converted using the Path > Object to Path menu entry before you apply the clip. The reason? It means that the path can be edited using the Node tool (F2) without unclipping first, which is not only a time-saver but also gives you a more representative view of how the final clipped object will look while you’re editing the path. Note that this capability can be toggled on and off when the Node tool is selected, by using this button in the toolbar; so make sure it’s enabled if necessary.
This is all well and good, but this month’s column isn’t really about repeating some tips from a decade ago, no matter how useful they still are. Instead, I want to talk about a new feature in Inkscape 1.3 which might make the previous approach redundant… at least in some cases.
Looking back at the original requirement, the aim was to cut out two sections from the original image which could then be moved around and manipulated independently. Although the group-and-clone technique achieved this visually, it didn’t really “cut out” the sections at all. The final pieces were just clipped versions of the full-sized image – which becomes very obvious when you look at the bounding box when I select one.
Obviously there’s at least one copy of the entire image embedded into the Inkscape document, even though the vast majority of the pixels don’t appear in the final output. All those invisible pixels still contribute to the file size.
The most obvious way to avoid this is to edit the original image using a bitmap editor such as The GIMP or Krita, but that assumes you know how to use such programs to slice your image into individual parts, and save them as separate files. Wouldn’t it be nicer if Inkscape could take care of this for you, using the tools you already know? Well now it can.
To demonstrate I’m going to drag and drop my image into the Inkscape window. By default, you will be prompted as to whether you wish to link or embed the file, and I’ll choose to link it.
If you don’t see that dialog then you likely clicked the “Don’t ask again” checkbox on a previous occasion. It doesn’t really matter too much whether the image is linked or embedded at this point (it will end up embedded by the end anyway), but I prefer to link it to avoid bloating the SVG file when I save incremental work-in-progress copies of the file. If you do want to reinstate this dialog, then open the Inkscape preferences (Edit > Preferences), select the “Imported Images” entry on the left, and ensure that you check the “Ask about linking and scaling when importing bitmap images” option.
In this particular case, I want to use two sections of this image, but this technique is just as applicable to a single clipping path on a single image, should you want to remove the extraneous pixels that take up space in your SVG file. But, for two sections, I need two copies of the image: not clones this time, but real copies. Selecting my imported image and duplicating it (Ctrl-D) does the job. If you embedded the image, then saving at this point will result in a very large file, with the content of your image stored twice in the SVG file. If you linked the file there will be no such concerns, and your SVG file remains almost as svelte as ever.
Now I’ll create the clipping paths that mark out the sections of the images that I want to keep. The tip about converting any primitive objects to paths still applies. Here’s how my document looks so far with my clipping paths given a bright green stroke as is my usual preference.
The next step is to select one image and its associated clipping path, and apply the clip. I usually right-click and select ‘Set Clip’ from the context menu, but Object > Clip > Set Clip does the same thing if right-clicking isn’t practical for you. Repeat for the other image and path.
This is the time to get the shape, position and size of your clipping paths absolutely perfect. We didn’t group the image first, so the trick of double-clicking to move the focus won’t work here. But we can switch to the Node tool and move the entire clipping path if necessary – just select all the nodes (Ctrl-A will do the job) and you can drag the entire thing around. Of course you can also edit individual nodes to change the shape of the path. If you need to change its size, but not its shape, select all the nodes and use the ‘<’ and ‘>’ keys. ‘[‘ and ‘]’ will similarly rotate the path.
Right, are you happy with the clipping? Make sure you are, because once you take the next step, Inkscape will throw away much of the original image, so there’s no turning back. (Okay, Ctrl-Z works to undo as usual, but what I mean is that this is a destructive change that can’t be undone once you’ve closed and reopened the file later).
Right-click on one of the clipped images and you should find a new entry in the context menu: “Crop Image to Clip”. Select that, and Inkscape will throw away the hidden pixels… sort of. The reason for that caveat is that there aren’t any widely used bitmap formats that support non-rectangular images, so in the case of a non-rectangular clipping path, you’ll still find that some hidden pixels remain (you can remove the clip to see them). But for all practical purposes, your image has been reduced to just the clipped area, even if a few extraneous pixels are technically still present in the corners.
Now it would be a big problem if Inkscape clipped a linked image in this way. You wouldn’t want Inkscape to destructively modify your original image. What happens if that same image is linked into another document, perhaps in a completely different application? For that reason, Inkscape embeds the destructively edited image file if it was originally linked. In the case of our example document, therefore, this means we’ve embedded two small images – one of the head and one of the hands – but the combined size of those two pictures is still likely to be significantly less than our single embedded file using the previous approach.
Our final cropped and clipped images can now be positioned and manipulated as normal, with the advantage that the bounding boxes look a lot more sensible.
This new feature will be useful in a lot of cases where users want to use Inkscape to clip or crop bitmap images, but it’s not a panacea, and needs to be used with consideration for its merits and its drawbacks. Where your clipping path encompasses most of the image – just removing a small amount from around the edges – it’s probably not worth using this technique. The savings in file size will be minimal, and the destructive nature of the editing means you’ll lose some ability to tweak and refine your clipping path or image position later on.
The example I used here does make sense… but if we’d wanted larger areas, or boxes that overlap the same part of the original image, the file size might not actually get much smaller. Each embedded cropped image carries some overhead, so it’s even possible that taking multiple views in this way could result in a larger SVG file than if the entire original image was just embedded once.
The feature itself is not very discoverable. You have to have already applied a clip to an image directly (not an image in a group), and only then does it appear in the context menu. Why can’t we have a “Crop and clip” context menu entry that does both steps as one?
It’s also worth noting that this applies to only bitmap images. For simple vector shapes, the equivalent method is to use the Boolean operations, but for complex groups of objects, there is no easy equivalent that will cut away the parts of objects that are not visible due to clipping. While the file size argument is less pressing with vector objects, there are times when it would be nice to be able to ‘tidy up’ an image in a similar way. I suspect there are too many edge cases for that to be an easy thing for the developers to achieve, though.
Overall I think this is a very useful addition to Inkscape’s arsenal. It’s just a shame that the lack of discoverability means that the people who will most benefit from it – the new users who also aren’t comfortable with bitmap editors – are those most likely to miss it. If you know someone like that, point them in the direction of this article.
Image Credits
“La Gioconda” (aka “Mona Lisa”) by Leonardo da Vinci http://en.wikipedia.org/wiki/File:Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg