Here’s a typical scenario: I go to a birthday party where everyone knows I’m a shutterbug, so I have to bring my camera. As time goes by and blood alcohol concentration rises and attention to detail declines, the photos become increasingly blurry. The trick is to take a lot of them and then, hopefully, some will come out well. But which ones?

Going through hundreds (or, since I got a mirrorless camera – thousands) shots the next morning – while nursing a hangover – is not a fun way to spend one’s morning, or afternoon. But it has to be done, as friends are getting impatient about seeing photographic evidence of all the shennanigans that transpired the night before.

I don’t really need or want to look at all the photos I took. At this point I just want to concentrate my attention on the probably very few that are in-focus – I don’t even care about their artistic merits. But finding them is a tedious process.

It then occurred to me (well after the same thought occurred to many other much smarter people) that ImageMagick is a great tool for detecting blurry photos. And so I decided to give it a shot.

Some important considerations

ImageMagick – unless restricted by its policy config file – will use up as much oomph as your computer can provide. And analyzing high-resolution photos taken my modern cameras requires a lot of system resources. Unless you can wait, you should run this process on a serious multi-core system with lots of RAM. And good network performance, if your photos reside on a network-mounted share. If this is a physical computer, make sure it is being properly cooled.

My setup

For everything below I was using a Dell XPS-15 with Windows 10, WSL2 running Ubuntu 18.04 LTS. The photos were located on a CIFS-mounted share on a QNAP NAS. And the network is 5GHz WiFi.

Mounting NAS share

This took a little bit to figure out, but the easiest way to mount a CIFS share on your WLSL Linux instance is to first mount it in Windows as a letter drive (W:, in my case) and the mount it in Linux:

Basic CLI syntax for mounting a network share under WSL Linux is this:

However, at the moment there is no way to pass credentials, meaning you’ll get ‘access denied’ unless your current Windows account has access to the share.

Installing/upgrading ImageMagick

This may require a bit of a personal touch, depending on the condition of your WSL Ubuntu, or whatnot, but the general process for me was this. And it will take some time to run. Being a sysadmin, I do most things under root ID, which is a bad idea, but here we are. So just add sudo wherever appropriate.

Identify blurry photos

The process below will not tell you definitively if a photo is blurry or not. But it will assign a four-digit number to each photo: the smaller the number, the more likely it is that the photo is blurry. You can then sort your photos and fairly quickly figure out a good cutoff point where the photos are very likely blurry and you should concentrate your attention elsewhere.

This little script will analyze all photos in the current folder and copy them to a subfolder with new filenames reflecting the “bluriness index”. Naturally, it would’ve been better if I could soft-link instead of copying, but WSL currently does not support soft or hard links. Microsoft is working on this, though.

The output should look something like this:

From here you can easily tell that 1109_DSCF2858.JPG is definitely a lot more blurry than 4200_DSCF2859.JPG.

File-naming convention considerations

In the example above, I prepend the “bluriness index” to the original filename. This makes it easy to identify photos that are most likely to be blurry and, therefore, ignore them for the time being.

Having said that, most photos are taken in a sequence: when a bunch of friends are posing for a shot, you will not take just one photo, right? From a practical standpoint, perhaps it would make it more sense to append the “bluriness index” to the original filename. This really depends on your workflow preferences. But here’s an example anyway:

Making the process faster

There are a couple of ways you can make this process run faster. One option is to replace 5x5 in the command above with lower values, like 3x3. Another way is to create copies of your original photos with lower resolution (this will also save you on bandwidth).

Quality control considerations

Obviously, this process is not perfect. The statistical methods used by ImageMagick – or, rather, my assumptions when interpreting the results – can be skewed by certain photos where a large background are is blurred and a relatively small subject is in focus. A good example would be a wide-aperture shot – say, a portrait – where the subject’s face is in focus, while most of the background is blurry. Do watch out for those.

When you have two photos of the same scene at the same exposure, the sharpest one will have the highest standard deviation. The issue here, of course, is that we’re trying to analyze a variety of rather dissimilar photos. The end result, therefore, is more of a quick guide and not a definitive metrics.

Plan B

There are many other ways to approach this issue of detecting relative sharpness of dissimilar photos. One possibility is to do Canny Edge Detection. Here’s a quick example:

Depending on the specifics of your photos, inevitably you may run into the same problem: you’re trying to analyze and compare very dissimilar photos. So what if photo01.jpg has less detail that photo23.jpg? Maybe photo01.jpg had intentionally shallow focal zone and photo23.jpg is just out of focus.

Plan C

No matter how you slice it, the key here is to compare similar photos – the same scene taken at the same exposure. So how do you identify those? The simplest option is by timestamp. If you went “click-click-click”, the resulting files will have similar or even identical timestamp. If you group those together into “scenes” and analyze each “scene” separately, the results will be much more accurate.

So, the first step would be to separate all the photos into “scenes”. For this example I will define a “scene” as any number of photos takes no more than five seconds apart:

The next step is to analyze photos in each “scene” to determine their relative sharpness:

The final step is to grab the least blurry photo from each “scene” (or the only photo, if the “scene” contains just one image) and copy it to the “selected” folder:

Conclusion

None of these methods are perfect. But, when you have an overwhelming amount of photos and no time to visually inspect every one of them (and you’re hungover, and your equally-hungover friends demand the results), this is definitely good enough.

Whatever ends up in the “selected” folder, I just ran through a Lightroom batch job geared toward the “drunken birthday party at an obnoxious Russian restaurant” scenario and shared the results to nearly-universal acclaim. Having said that, I think I’m a better sysadmin than a photographer…

At this point I was not qualified to stand up, let alone operate a camera…

Leave A Reply

Please enter your comment!
Please enter your name here