Search code examples
imagemagickzsh

How to loop sequentially through images in one folder and compare with the latest image in another folder


I am trying to use the shell to loop sequentially through images in one folder and compare with the latest image in another folder.

One error i'm getting is -

zsh: no matches found: /TiffOut/*.tiff(.om[1])

Though I wouldn't be surprised if that's not the only problem.

What I'm hoping to acheive is:

  • Loop through all source images in current directory:
  • Put most recent file from TiffOut subdirectory in variable 'latest'
  • Compare current source, and latest output with '-evaluate-sequence max' to create new tiff with the brightest parts from each image_file
  • Save to TiffOut subdirectory

Here's my full script -

#! /bin/zsh -
filelist=$(ls | grep '.tiff')
for image_file in $filelist
do
latest=$(/TiffOut/*.tiff(.om[1]))
magick $image_file $latest -evaluate-sequence Max '/TiffOut/out_${imagefile}.tiff'
done

Thanks for reading


Solution

  • There are a few issues with your script - give this one a try:

    #!/bin/zsh
    
    for image_file in *.tiff
    do
      latest=(./TiffOut/*.tiff(.om[1]))
      magick "$image_file" "$latest" -evaluate-sequence Max "./TiffOut/out_${image_file}"
    done
    

    Some notes:

    • You don't need grep to narrow down the file list; just use a glob pattern (*.tiff).
    • Subdirectories are relative to the current directory, which is ., hence ./TiffOut/*.tiff. Paths beginning with / are absolute, starting at the root directory.
    • The assignment that gets the latest file is using another glob pattern, with the .om glob qualifiers. It's not a command, which is what $(...) indicates. With just the parentheses, it's an assignment of an array, with a single member.
    • Variables (e.g. $something) are only expanded in strings quoted with double-quotes, not single quotes.
    • I don't know enough about imagemagick to tell you if the magick command does what you want.

    You may be able to use the terms in my notes to find more information about the pieces of the script.
    Good luck!