Skip to content

Instantly share code, notes, and snippets.

@Krzysiu
Last active March 21, 2026 16:40
Show Gist options
  • Select an option

  • Save Krzysiu/f0ec2822ff0aeb9010582080b1f04869 to your computer and use it in GitHub Desktop.

Select an option

Save Krzysiu/f0ec2822ff0aeb9010582080b1f04869 to your computer and use it in GitHub Desktop.
Blazing fast chainner configurable batch processing
:: Runs chaiNNer file with CLI for multiple files.
::
:: This script or just your own command, it's worth - for my PC it makes things about 30 seconds quicker
:: due to faster work and fast loading without GUI.
:: Could be configured to change depth to 8 bit (useful for RawTherapee, where files opened externally
:: are always 16 bit). For that, you need ImageMagick in your system path.
:: Also supports Append Text node, i.e. if you use suffix for your file name! Before proceeding, be sure
:: to check configuration section!
:: At last, but not last, it supports one OR MORE file(s) in command line arguments. This is not
:: recommended, tho as you'd be getting short (but still) overhead of running Chainner for each file.
:: It's better to use directory iterator. In this case you should turn off 8 bt conversion mode.
:: At the end, if everything succeeds, it uses my own beep command to play a short melody. If you want,
:: you can get it here:
:: https://github.com/Krzysiu/cmdwinutils - it's just <40 kB! Without it, it will still work, just
:: without sound feedback
:: https://buymeacoffee.com/krzysiunet
@echo off
setlocal enabledelayedexpansion
:: --- CONFIGURATION ---
:: 1 = ON (convert to 8-bit), 0 = OFF (use original)
set "CONVERT_8BIT=1"
:: If your chaiNNer project uses an "Append Text" node for the output filename,
:: enter that text here (e.g., -focus). Leave empty if no suffix is added.
set "OUTPUT_SUFFIX="
:: Paths to tools and project
set "CHAINNER_EXE=%LOCALAPPDATA%\chaiNNer\app-0.25.1\chaiNNer.exe"
set "CHN_PROJECT=E:\Models\Chainner\focus_rt.chn"
:: To get the ID, right-click on the control in the chaiNNer GUI and choose "Copy Input Override ID"
set "CONTROL_ID=#a78d8e56-c20e-44c2-af45-d1a4bfc257f1:0"
:: ---------------------
if "%~1"=="" (
echo No files provided. Drag and drop files onto this script.
pause
exit /b
)
:ProcessLoop
if "%~1"=="" goto EndLoop
set "OLD_PATH=%~1"
set "TEMP_JSON=%TEMP%\in_%RANDOM%.json"
echo.
echo Processing: "%~nx1"
if "%CONVERT_8BIT%"=="1" (
echo Converting to 8 bit...
set "PROC_PATH=%TEMP%\%~n1_8b.tiff"
magick -quiet "!OLD_PATH!" -depth 8 "!PROC_PATH!"
set "RESULT=%TEMP%\%~n1_8b%OUTPUT_SUFFIX%.tiff"
) else (
echo Using original file...
set "PROC_PATH=!OLD_PATH!"
set "RESULT=%TEMP%\%~n1%OUTPUT_SUFFIX%.tiff"
)
:: Escape backslashes for JSON
set "JSON_PATH=!PROC_PATH:\=\\\\!"
:: Create JSON override file
echo {"inputs":{"%CONTROL_ID%":"!JSON_PATH!"}} > "!TEMP_JSON!"
echo Running chaiNNer...
"!CHAINNER_EXE!" run "%CHN_PROJECT%" --override "!TEMP_JSON!"
set "EXIT_CODE=%ERRORLEVEL%"
:: --- CLEANUP ---
if exist "!TEMP_JSON!" del /f /q "!TEMP_JSON!"
if "%CONVERT_8BIT%"=="1" (
if exist "!PROC_PATH!" del /f /q "!PROC_PATH!"
)
:: --- FINALIZE SINGLE FILE ---
if %EXIT_CODE% EQU 0 (
if exist "!RESULT!" (
:: Optional: Beep for each file or only at the very end?
:: Default: beep for each to know it's moving forward.
beep 440 50 && beep 659 50
start "" "!RESULT!"
) else (
echo Error: Expected output file not found: !RESULT!
pause
)
) else (
echo Error during chaiNNer execution for "!OLD_PATH!".
pause
)
:: Move to next file
shift
goto ProcessLoop
:EndLoop
echo.
echo All files processed!
beep 440 100 && beep 554 100 && beep 659 100 && beep 880 300
timeout /t 3
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment