Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

A forum to ask questions, post setups, and generally discuss anything having to do with photomacrography and photomicroscopy.

Moderators: rjlittlefield, ChrisR, Chris S., Pau

plantfan123
Posts: 27
Joined: Tue Dec 08, 2020 12:39 pm

Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

Post by plantfan123 »

Hi. I am doing a photo stacking timelapse project. The Stackshot controller is controlling the rail and the camera is capturing photos tethered, with each stack having 100 photos. Before the next set of 100 photos there is a 30 second gap from the rail returning to the start.

Helicon Focus has a great feature where it can take a folder of images and split them up based on detecting whether there's been a gap of a number of seconds between images. I could just use a basic script to split the folder of, say, 30,000 images into 300 stacks, but this type of approach makes it so that it works perfectly even if the stacks have been interrupted. For instance, maybe I redid the settings on the controller, so then the stacks would go 100/100/48/100/100 - rendering the basic script not as useful unless I went in and tediously deleted that incomplete stack. Doing this isn't hard of course, but with big projects stuff like this really adds up.

Does anyone have any pointers as to how this functionality would be recreated with a watchfolder script where those 30 second gaps between stacks could be detected so that my images could be put into folders and processed with ZS? An example script here or elsewhere would be very helpful - I can poke around with code but can't write it myself. Thanks. :)

rjlittlefield
Site Admin
Posts: 23564
Joined: Tue Aug 01, 2006 8:34 am
Location: Richland, Washington State, USA
Contact:

Re: Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

Post by rjlittlefield »

What operating system are you running?

--Rik

plantfan123
Posts: 27
Joined: Tue Dec 08, 2020 12:39 pm

Re: Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

Post by plantfan123 »

I am running Windows 10 on my main machine but I also use Debian VMs and a Mac from time to time, so it's flexible.

rjlittlefield
Site Admin
Posts: 23564
Joined: Tue Aug 01, 2006 8:34 am
Location: Richland, Washington State, USA
Contact:

Re: Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

Post by rjlittlefield »

Triggering off the gap in modification times is a good idea, and I cannot find that function in any standard folder splitting program.

So, here is a script to move photos into new folders via detecting 30 second gap between batches. I'll let you wrestle with the "watchfolder" aspect yourself.

This bash script works OK on Windows+cygwin and on CentoOS Linux. It also runs OK on macOS, with one small change described below. I do not have a Debian to test.

Note that the script as written here has a symbol "echocmd" that is set so as to show the mkdir and mv commands that will be executed when the script is tweaked to actually do its job. You'll have to de-comment one line of the script, defining "echocmd=" to get the commands executed instead of shown. I've done this as a defensive programming technique. My experience is that shell scripts are easily disrupted by slight differences between systems, so it's a good idea to run them in some sort of nondestructive simulation mode before turning them loose to change files.

Code: Select all

#!/bin/bash

# Split all *.jpg files in current folder into subfolders,
# each subfolder containing no more than specified number of files,
# with no longer than specified seconds between successive files.
# Each subfolder is named as the first file that it contains, with "_dir" added.

MAXFILESPERBATCH=100
MAXSECONDSBETWEENFILES=20

## Initialize $echocmd so as to show what the script will do when run.
echocmd=echo
## If the following line is commented, then mkdir and mv commands will be shown rather than executed.  
## You should check the script behavior, before un-commenting this line to actually do the work.
##echocmd=

lastmtime=0
numfilesinbatch=0
while IFS= read -r fname
do
  
  if [ -f "$fname" ]; then 
    mtime=`stat --format=%Y "$fname"`
    if [ $(( mtime - lastmtime )) -gt $MAXSECONDSBETWEENFILES -o $numfilesinbatch -ge $MAXFILESPERBATCH ]; then
      dirname="${fname%.*}"_dir
      $echocmd mkdir "$dirname"
      numfilesinbatch=0
    fi
    lastmtime=$mtime
    $echocmd mv "$fname" "$dirname"
    numfilesinbatch=$(( numfilesinbatch + 1 ))
  fi
done < <(ls -1tr *.jpg)

Note that the file extension ".jpg" is specified in the ls command at the bottom of the script. The extension will probably be case sensitive, so you may need to change that to be ".JPG" (or whatever other extension you want to target).

For macOS, the format argument of the stat command must be modified as follows:

Code: Select all

    mtime=`stat -f %m "$fname"`
Note also that the default shell for macOS is now zsh, not bash, and this script is not compatible with zsh. If you want to run it interactively, get yourself into a bash shell first.

If you have any trouble, I suggest to start by contacting me by email, support@zerenesystems.com . Then we can clean this thread up after the problem is fixed.

--Rik

plantfan123
Posts: 27
Joined: Tue Dec 08, 2020 12:39 pm

Re: Watchfolder script to move photos into new folder via detecting 30 second gap between batches for use in ZS?

Post by plantfan123 »

Wow, thank you so much for going to the trouble to write this script, it's incredibly kind of you. It will be very helpful as I migrate from HF to ZS. I'll start testing it soon and will be in touch. :)

Post Reply Previous topicNext topic