Not the usual topic for this blog and not the most comprehensive guide on the subject out there, but here I go anyway. Focus stacking combines multiple photos taken at different focal distances into an image of greater depth of field.

Focus stacking is used primarily in close-up photography: macrophotography and microscopy. In a nutshell, you have your subject – a flower, a piece of jewelry, a beetle – and a camera (preferably mounted on a tripod). You point your camera at the subject and take a sequence of photos, changing only the focal point.

You can do this manually if you’re careful, but most modern cameras offer focus bracketing. You tell the camera how many images to take in a row and the focal point adjustment for each consecutive photo. And the camera goes click-click-click and quickly fills up the memory card.

Taking the Photos

Each camera has its particular focus bracketing settings, so grab the user guide and figure it out. Generally, using a tripod is an excellent idea. It goes without saying that the subject of your photography must be perfectly stationary. So should be the source of your lighting – no sparkling highlights or moving shadows.

Since you’re going to be combining multiple photos anyway, use a wider aperture. This will allow you to minimize exposure time and take many frames quickly. I will further suggest you set ISO, exposure, dynamic range, and aperture manually. For the best result, you don’t want those parameters to change from frame to frame.

A word of advise: most cameras out there use a consecutive file naming convention and have no method for grouping bracketed photos into sets. This means figuring out where each set ends and another set begins will not be easy unless your memory is like a steel trap.

My suggestion is that between each set, you take a spacer photo. Just temporarily switch your camera into the single shooting mode, point it up and take a blank image of the sky or the ceiling.

If you already screwed this up, and now you have thousands of photos and no idea what bracketing set they belong to, you have a couple of options: you can try using Lightroom’s auto-stacking feature (right-click → Stacking → Auto-Stack by Capture Time) or the script below.

Lightroom auto-stacking feature will ask you to set the minimum time interval between each set of bracketed photos. Unless I was using a tripod to take long-exposure shots (anything over 1 second), I would set this interval to 1s.

Both methods rely on the same basic assumption. When using automatic bracketing, the photos are taken in quick succession. Unless you’re doing something bizarre, there won’t be more than a one-second difference between any two consecutive images in a set. Furthermore, more than one second would likely elapse between taking two consecutive sets of bracketed photos.

Therefore, we can take a look at the “DateTimeOriginal” EXIF value for each photo and be able to figure out which of them belong to the same sequence. Whether this was AE bracketing or focus – you’ll have to deduce from looking at other EXIF data, like aperture, ISO, and shutter speed. At least now you know which photos belong together.

f="$(mktemp)"; find . -mindepth 1 -maxdepth 1 -type f -name "DSCF*.JPG" -printf '%f\n' | \
while read i; do 
 t="$(exiftool -s -s -s -DateTimeOriginal -d "%s" "${i}")" && echo "${i},${t}" | tee -a "${f}"

awk -F, '{print $2}' "${f}" | sort -u | \
awk 'NR==1{first=$1;last=$1;next} $1 == last+1 {last=$1;next} {print first,last;first=$1;last=first} END{print first,last}' | while read line; do
  read -r s e <<<"${line}"
  sf="$(grep ",${s}$" "${f}" | head -1 | awk -F, '{print $1}')"
  ef="$(grep ",${e}$" "${f}" | tail -1 | awk -F, '{print $1}')"
  echo -e "Set ${j}:\t ${sf} - ${ef}"
  (( j =j + 1 ))
/bin/rm -f "${f}"

Set 1:   DSCF3568.JPG - DSCF3572.JPG
Set 2:   DSCF3573.JPG - DSCF3577.JPG
Set 3:   DSCF3578.JPG - DSCF3582.JPG
Set 4:   DSCF3583.JPG - DSCF3585.JPG
Set 5:   DSCF3586.JPG - DSCF3588.JPG
Set 6:   DSCF3649.JPG - DSCF3678.JPG
Set 7:   DSCF3679.JPG - DSCF3708.JPG
Set 8:   DSCF3709.JPG - DSCF3738.JPG

Importing Photos into Lightroom

You will likely end up with a lot of files. Like, really a lot. Most laptops wouldn’t have enough free disk space left, and you will need fast local expansion storage, such as a USB SSD. 1TB should be sufficient (unless your beetle collection is worthy of a leading insectarium).

While Lightroom is chewing on your SSD, consider popping into Photoshop’s preferences and making this small but hugely important change: Edit → Preferences→ File Handling→ check Disable Compression of PSD and PSB Files.

Why waste disk space? When saving a focus stack consisting of many layers, Photoshop would want to use the PSB format for files over 4GB. By default, this file format supports compression, which is exceedingly slow. It will take minutes to save a single file vs. just several seconds if you disable compression. And in the end, lossless compression doesn’t save all that much space anyway but does waste a lot of system resources.

Exporting into Photoshop

This part is straightforward: select the photos in a given set and right-click → Edit In → Open as Layers in Photoshop. This will open Photoshop, and it will import all the selected photos into separate layers. Then in Photoshop, in the Layers tab, select all the layers and then click Edit → Auto-Align Layers → Auto → OK. Once that’s done, click Edit → Auto-Blend Layers → Stack Images → check Seamless Tones and Colors → OK.


That last step will take a bit of time. Once it’s finished, hit CTRL-W and answer “Yes” when asked if you want to save the image. If the image is too large for the default TIFF format, you will be prompted to save in PSB format, so do that.

Now you can edit this photo further either in Lightroom or directly in Photoshop.