Atom Feed
Comments Atom Feed

Similar Articles

2009-06-20 13:09
R1800 Gutenprint tricks

Recent Articles

2019-07-28 16:35
git http with Nginx via Flask wsgi application (git4nginx)
2018-05-15 16:48
Raspberry Pi Camera, IR Lights and more
2017-04-23 14:21
Raspberry Pi SD Card Test
2017-04-07 10:54
DNS Firewall (blackhole malicious, like Pi-hole) with bind9
2017-03-28 13:07
Kubernetes to learn Part 4

Glen Pitt-Pladdy :: Blog

Look Sharp

When printing photos, sharpening is vital to compensate for losses in the capture and printing process. I have long been looking for a simple tool that I could script (ie. non-interactive) for batch processing photos in volume over night. I drew a blank so decided to roll my own.

Magick with Perl

The ImageMagick suite provides an Open Source platform for extremely high quality image processing and includes all the basics with a comprehensive list of supported file formats as well as image processing filters and effects. It has ready made APIs for many popular languages including C, C++, Perl, PHP, Java, Ruby, Python, .Net and many others, making it an ideal platform for building on.

In this case I am using Perl Magick (yes, I know Perl is dirty, but it has a certain rustic appeal to me). Because the underlying ImageMagick library takes care of all the heavy lifting there is minimal overhead from using a scripting language - something that makes this much quicker to throw together.

In my experience, the one big disadvantage of ImageMagick is speed. It is all out quality and speed suffers. As this is a batch tool for running over night, I'm not going to get too upset about that.


What I was aiming for was to automate sharpening including choosing the parameters. This allows things to be completely automated and the script to make autonomous decisions based on the image it is dealing with.

ImageMagick has some useful statistical tools built in which can be exploited for automation.

The other headache that I always have with conventional sharpening tools is that they have hard thresholds: they either sharpen or they don't. This is the case with ImageMagick's unsharp mask, and the result is horrible artefacts on the transitions from sharpened to unsharpened areas.

What I want is nicely blended sharpening which will be artefact free and sharpen detail while leaving noise untouched.

After much experimenting I came up with a layered approach which seems to work for me.

The Algorithm

Initially all I do is sharpen (unsharp) a clone of the image with a set of basic parameters which match the target print resolution. For 360DPI (keeping the DPI a multiple of output resolution will yield the best results by preventing subsampling - in this case this matches Epson inkjet printers), typically I want a sigma of 1 pixel, or at 720DPI (if quality is more important than your time), 2 pixels, and a 1.5 as the amount.

Areas with something to sharpen will result in changes to the pixel values, and minimal changes to smooth graduated areas. By taking the difference from the original image, we immediately have a mask of how much the image can be sharpened in different areas.

At this point we have a very rough mask for sharpening. First off I blur the mask slightly so that the transitions are smoothed out, then desaturate and normalise it. This gives us a mono mask with the values stretched to fill the whole range.

Next I do some statistical analysis: Initial attempts at finding where the noise was where based on simple averaging of the histogram, but I soon found that the square weighted average gave a very close match to the peak of the noise in the image.

From there, I compute the standard deviation against this figure which gives an indication on the spread of the curve.

With some experimentation I found that I get the results I like by setting the levels on the mask to begin blending the sharpened image in from half the standard deviation above the square weighted average up to full at one standard deviation above the square weighted average.

This results in most image noise not being sharpened, but detail getting the full sharpening. Your results may vary, but for my photos this generally works very well.

Have a go

You can download my script under GPL and try it out (remember: you will also need the Perl ImageMagick libraries). Simply add the image file names you want to process on the command line and it will produce versions suffixed with "-out" that have been sharpened. You may need to change the basic sharpening parameters depending on your output resolution.