Page 1 of 1

Offering script to separate stacks into folders

Posted: 02.04.2025 18:34
by willsher
Below is a Windows cmd language .bat script which we're sharing with everyone.

Because we do product shoots that require custom and unusual setups, all aspects of creating a stack of consecutive images are unique and are best separated by hand-over-lens black/blank separator images. (e.g., time-proximity of the images won't tell the full story as a criteria for separating stacks).
We've found it's easiest to just take an extra shot at the end of each stack sequence. Even tho the strobes fire, hand (or other block) over the lens is a quick indicator to insert into the image sequence. Place this script (example filename: sort.bat ) alongside your images to be sorted.
Below is a simple script that goes thru all the jpgs in the current directory and creates sub directories named after the first jpg filename in the stack. Blank jpgs are collected in a directory blank/
(If you use other file types instead of jpg, it's easy to substitute that file type in the script... as long as imageMagick can parse that format)

Code: Select all

@echo off
::  windows cmd batch script to identify and move groups of jpgs that are between "blank" jpg (hand over lens) images to form stacks for stack-focusing.
::  each separated stack is placed in a sub directory named by the filename portion of the first jpg in that group
::  when shooting, you separate stacks with a blank image (hand over lens).  Set the darkness threshold in THRESHOLD variable
::  place this .bat file in the directory where the jpg files are
::   (the script requires (free) imagemagick to be installed on Windows. Note that version 7 or greater is required due to the deprecation of the "convert" command)

setlocal enabledelayedexpansion

::  catcher directory for blank/black separator files (that have no further usage)
if not exist blank mkdir blank

:: Set threshold for considering an image as mostly black (0 is completely black) (or mostly white: change IF stmt below)
set "THRESHOLD=150"

:: init flag for files evaluated since last black jpg.
set /a COUNT=0   

echo "---------------------------Starting processing--------------------------------"

:: Loop through all .jpg files in the directory
for %%F in (*.jpg) do (
    echo Checking %%F...

    :: Get the mean brightness of the image using ImageMagick
    for /f "tokens=*" %%A in ('magick "%%F" -colorspace Gray -format "%%[mean]" info:') do set "BRIGHTNESS=%%A"

    :: Convert floating-point brightness to an integer 
    set /a "BRIGHTNESS_INT=BRIGHTNESS"

::    if !BRIGHTNESS_INT! GTR %THRESHOLD% (               ***  USE IF WHITE/LIGHT is the separator instead of black
    if !BRIGHTNESS_INT! LSS %THRESHOLD% (
        echo "***    " %%F !BRIGHTNESS_INT!  Blank
		:: reset COUNT flag
		set /a COUNT=0
		move %%F "blank"
    ) else (  
	    :: not a blank image
        echo "+++    " %%F  !BRIGHTNESS_INT!
		if "!COUNT!" == "0" (
			:: we're at the point where the LAST file was black, and this is the first jpg in the new stack
			:: make new directory named after the filename part
			set "NEWDIRNAME=%%~nF"
			::echo COUNTis0 NewDir is !NEWDIRNAME!
			mkdir !NEWDIRNAME!
			:: record that a non-blank image was encountered after one or more blanks
			set /a "COUNT=!COUNT!+1"
		)
		:: continue now with moving the current image %%F to this new directory %NEWDIRNAME%
		echo ">>>   moving "  %%F to !NEWDIRNAME! 		
		move %%F !NEWDIRNAME!
    ) 
	:: end else
	:: repeat for next file in directory
)
:: end for each file in dir

echo Done.
::pause

Re: Offering script to separate stacks into folders

Posted: 19.09.2026 16:38
by Astrophytum
Hello,

Thank you for the script. However, I think only a few users will need it.

After taking the last photo of a stack, I put my hand or fingers in front of the lens as a separator.

Later, when loading the images into Helicon Focus, it is very easy to identify the beginning and end of a stack. The only requirement is that thumbnail view be enabled in the folder. If you remember the image number of the last rendered stack (most cameras use continuous numbering), you can quickly find the correct position in the folder when opening the images of the next stack.

Best regards,
Steffen

Re: Offering script to separate stacks into folders

Posted: 19.09.2026 21:15
by willsher
All good/true. Thank you for your update.

My typical sessions involve 100-200 stacks :)
(each with ~100 images)
thus my need for an automated workflow as shown. it goes pretty fast...

take care
will