Help:Image extraction/With GIMP

From Wikisource
Jump to navigation Jump to search
Image Extraction Image extraction with GIMP
A guide for extracting a greyscale image from a paper background using GIMP.

GIMP is a widely used and powerful free image editing program, available for all platforms. This guide uses GIMP, but the principles are the same for other image editors, such as Photoshop.

This guide will walk you though the process of editing an image with a paper-coloured background to clean it up for use at Wikisource.

The process[edit]

Initial image[edit]

This is the image we will be working with. The original page image is from the National Library of Scotland. This page assumes the image is rotated, un-warped and cropped.

When downloading the initial image, be sure to use the highest-quality and most original image you can find. Screen-shotting from a website or extracting from a PDF or DjVu are last resorts, because the image is already heavily damaged by compression at that point. H:EXTRACT provides some guidance for finding the best images. In this case, the NLS provided a page JPG of size 1290 x 2057px, which was cropped to the image of interest:

Wikisource GIMP image extraction guide - Original image.png

This image is a fairly challenging image to extract for two reasons: firstly, the paper colour is very strong and uneven. Secondly, the ink is not very dark and the print quality is low. So we may find we have to make some compromises while processing this image. Some images will be much easier to extract than this, but the overall principles will remain the same.

Image extraction always involves a certain amount of judgement as to the amount of damage to the original image that is acceptable.

Removing the paper colour[edit]

The basic aim here is to erase everything that is "paper-coloured" and leave it either transparent or very pale.

Add an alpha channel to the image (right click on the layer in the Layers panel) and add a white background layer so you can see what you are doing (or set your background to white, not checkerboard). Reselect the top image.

With the dropper tools (shortcut O), click a darker area of the paper colour. Working from dark to light makes it less likely that you'll end up with grey background area that can't be distinguished from genuine grey image content on colour alone. In this case, I chose a point just above the centre of the roofline. I did not choose the darkest point in the background, because there is an area of the roof which is light enough that it would be removed by that. This means that there will some particularly dark areas of the background that we have to adjust manually later on.

Select the "bucket" tool (Shift+B). Set it to "Color Erase" mode in the tool options box. This will erase everything that is the same colour as what we just selected. Click on the image to erase the colour.

Once a colour erasure is done, the remaining background might have quite a strong colour and a low opacity (so it appears pale). This means that the next colour erase, which uses the colour value without the opacity will not work well. In this case, you might find you need to right click the layer and "Remove alpha channel" and then re-add it. Repeat the colour erasure step until most of the background is gone.

After 1 colour erasure step
After 2 colour erasure steps
After 3 colour erasure steps

We now have an image where most of the background in the image layer is either transparent or very close to it.

Adjusting levels[edit]

We will now adjust the "levels" of the image to wash out the remaining (very light) background colour.

Open the Colors > Levels... dialog. You will see a histogram of the values in the image. Note that this is in "logarithmic" mode (Gimp-histogram-logarithmic.svg)—the right hand button, otherwise the white background will dominate the graph:

Wikisource GIMP image extraction guide - Level adjust 1 - histogram.png

We can see a few things here:

  • There is a large spike at the right edge - this represents the many white pixels. Sometimes, if the colour erasure has left more light-coloured non-white pixels behind, you may see a small "hump" at the top end.
  • There are no pixels darker than a certain value (the graph is empty below about the 1/5 mark)
  • The brightness of the non-white pixels is fairly evenly spread.

To "blow out" the remaining background bring the white marker leftwards until it is left of any "hump" at the far-right edge. The further left you bring it, the more valid light pixels you will destroy, so you have to find a balance. This is why it is important to get the background as light as possible. In this case, the lines on the roof are very light, so they are vulnerable to being deleted along with the background.

Then, to "stretch" the contrast so that the darker pixels in the image are closer to black, move the black marker right-wards.

The result looks something like this:

After white and black point adjustments
After grey point adjustment

At this point, you may choose to further tweak the image. Often, a small adjustment of the grey point to the right will make the greys "pop" a bit, but too much will damage the image by "compressing" what used to be multiple colours into a single colour, making the picture look "flat" and "edgy".

Tidying up[edit]

Now, is the fiddly bit. You have to scout around the image with the erase tool (usually a small, quite hard-edged, round brush works well) and erase splotches and defects from the top (image) layer. Sometimes you have to make a judgement about whether a dark area is image, or if it's background that has been darkened during processing. Generally, accidentally-enhanced background looks like a mid-grey or yellow/red tinted, soft-edged splodge, compared to darker-grey harder-edged areas that were originally ink. This shouldn't take too long, unless the image is very big, very damaged or very complex.

Refer to the original (look in the Undo History pane) when in doubt, sometimes it's then clear what is and isn't in the image.

You can also use the "Contrast" display filter (View -> Display Filters...) to highlight areas that are hard to see. Very light areas code out as grey splodges, and "false" ink (probably remnants of foxing missed by the colour erase step) looks like a soft-edged blob, whereas ink has a black border and a pale interior (representing the sharper transition from white to black in the real image). You can edit the image with the display filter on.

A good example of a "false ink" blob is on the wall to the left of the bottom-left window.

The light yellow areas indicate that the levels adjustment was not quite aggressive enough and we left some very light areas behind, so use the levels tool again to and move the white point left until the yellow areas vanish.

With contrast display filter

After tidying, the image looks like this:

Wikisource GIMP image extraction guide - After tidying.png


If your image is black and white, you should desaturate it to remove any residual colour casts (due to the yellowish erased colour, the image is often slightly blue by this point). Use Colour -> Desaturate -> Desaturate.... If your image is not black and white, skip this step.

Next, flatten the image to turn the lacy mess that is the top layer after all the erasing in the "tidying step" into a simple, flat greyscale image. Right click on the top layer and "Flatten Image".

Wikisource GIMP image extraction guide - After desaturate.png


If you want your image to show the background behind it (common for diagrams and decorations), you should now convert "white" to transparency. Note that this will only work well if the image is substantially dark grey or black on white. Any mid-grey will end up semi-transparent.

This is important if you want to display the image on a coloured background. Many non-eink ebook readers use an off-white background, even if the Wikisource website background is white by default.

The finished file, also showing the result on a coloured background:

Wikisource GIMP image extraction guide - Finished.png
Wikisource GIMP image extraction guide - Finished.png


Save as PNG (to retain the transparency and so as not to re-introduce compression artifacts: most graphics like this have hard edges, which means high-frequency image data, which means JPG compression will be quite obvious, even at high quality levels.

Below is an image that shows all the pixels that JPG compression distorts as red (with a quality of 92):

Wikisource GIMP image extraction guide - JPG compression noise.png

If the image is greyscale, saving as greyscale PNG (8bpc GREYA in the GIMP export drop-down) can save a few kB of image size.

Don't scale the image dimensions down and don't worry the file size too much, Commons is not short of disk space, and the image will be automatically served at a appropriate resolution when embedded at smaller sizes. Even if you have produced a 10MB 5000×8000px PNG, it will be only a few hundred kB at most when embedded at 400px.

See also[edit]

Similar processes: