ImageMagick can output text data that can be assigned to variables in Windows BAT or Bash scripts.
Here are some cookbook examples in both languages.
I use Windows with Cygwin, so Microsoft and Unix tools are both available on my computer, accesible from both BAT and bash scripts. Examples on this page are "pure"; either Windows or Unix, without mixing the two. The Windows BAT examples use only standard Microsoft tools, and the bash examples use only Unix tools.
Most of my scripting is in Windows BAT. I am not an expert in Unix tools or bash.
For basic information about escape characters and doubling percent sgns, see IM with Cygwin: Translating between Windows and Unix.
I use multiple versions of ImageMagic. They are contained in different directories, so I specify which version I want by giving the full path to the program.
for /F "usebackq" %%L in (`cygpath %IMG7%`) do set CYGIM=%%L
If the required data comes from a -format setting, we don't need to remove superfluous characters, and we can name the destination variables within the format itself. Putting the variable name with the expression means we don't need to keep counting, and debugging/maintainance is simpler.
For example: how many pixels, and what proportion of pixels, in the built-in rose: image are within 25% of pure red?
First, the BAT script:
for /F "usebackq" %%L in (`%IMG7%magick ^ rose: ^ -fuzz 25%% ^ -fill Black +opaque Red ^ -fill White -opaque Red ^ -format "NUM=%%[fx:int(mean*w*h+0.5)]\nPROP=%%[fx:mean]" ^ +write info: ^ td_rose_blue.png`) do set %%L echo NUM=%NUM% PROP=%PROP%
NUM=772 PROP=0.239752
Format strings can become very long, so I often break them up like this for clarity:
set FMT=^ NUM=%%[fx:int(mean*w*h+0.5)]\n^ PROP=%%[fx:mean] for /F "usebackq" %%L in (`%IMG7%magick ^ rose: ^ -fuzz 25%% ^ -fill Black +opaque Red ^ -fill White -opaque Red ^ -format "%FMT%" ^ info:`) do set %%L echo NUM=%NUM% PROP=%PROP%
NUM=772 PROP=0.239752
In bash, we use the same convert statement, inside backticks or $(), preceded by "declare". The format string is readily split into lines.
bash declare `${CYGIM}magick \ rose: \ -fuzz 25% \ -fill Black +opaque Red \ -fill White -opaque Red \ -format 'NUM=%[fx:int(mean*w*h+0.5)]\n\ PROP=%[fx:mean]' \ info:` echo NUM=$NUM PROP=$PROP $ exit
NUM=772 PROP=0.239752
Using identify as a calculator.
Suppose we want a value from identify -verbose, such as the rendering intent.
BAT version:
for /F "usebackq tokens=3" %%L in (`%IMG7%magick identify ^ -verbose rose: ^| ^ findstr /C:"Rendering intent:"`) do set REND=%%L echo %REND%
ECHO is on.
Bash version:
bash set -- `${CYGIM}magick identify \ -verbose rose: | \ grep "Rendering intent:"` REND=$3 echo $REND $ exit
In Windows BAT, the for command reads all the lines of its input, breaking each line into words which it assigns to temporary single-letter variables that persist only to the end of the for command. It can treat unwanted characters as delimiters between words.
compare sends text output to stderr. We can redirect this to stdout, and redirect stdout to a file.
%IMG7%magick compare ^ rose: ^ xc:blue ^ -similarity-threshold 0 -dissimilarity-threshold 1 ^ -subimage-search -metric RMSE ^ NULL: >td_comp1.lis 2^>^&1 cmd /c exit /B 0
The file td_comp1.lis contains:
23474.9 (0.358203) @ 7,26
This compare command returns a non-zero error code, setting ERRORLEVEL to that value. which causes a failure in the process that builds this page. The effect of "cmd /c exit /B 0" is to reset ERRORLEVEL to zero.
Instead of redirecting to a file, we can put the four values into four temporary variables, and assign from there to more persistent variables:
for /F "usebackq tokens=1-4 delims=()@, " %%A in (`%IMG7%magick compare ^ rose: ^ xc:blue ^ -similarity-threshold 0 -dissimilarity-threshold 1 ^ -subimage-search -metric RMSE ^ NULL: 2^>^&1`) do ( set TD_COMP=%%A set TD_COMP_F=%%B set TD_COMP_X=%%C set TD_COMP_Y=%%D ) echo TD_COMP=%TD_COMP% TD_COMP_F=%TD_COMP_F% TD_COMP_X=%TD_COMP_X% TD_COMP_Y=%TD_COMP_Y%
23474.9 (0.358203) @ 7,26
In bash, we can use the $(...) (or backtick) command-expansion to put the entire output into a single variable:
bash result=$(${CYGIM}magick compare \ rose: \ xc:blue \ -similarity-threshold 0 -dissimilarity-threshold 1 \ -subimage-search -metric RMSE \ NULL: 2>&1) echo $result $ exit
23474.9 (0.358203) @ 7,26
To get the four values nto four environment variables, sed can remove the superfluous characters "()@," splitting the output into words, which "set --" assigns to positional parameters, which we then assign to more persistent variables:
bash set -- $(${CYGIM}magick compare \ rose: \ xc:blue \ -similarity-threshold 0 -dissimilarity-threshold 1 \ -subimage-search -metric RMSE \ NULL: 2>&1 | \ sed -e 's/[()@,+]/ /g') TD_COMP=$1 TD_COMP_F=$2 TD_COMP_X=$3 TD_COMP_Y=$4 echo TD_COMP=$TD_COMP TD_COMP_F=$TD_COMP_F TD_COMP_X=$TD_COMP_X TD_COMP_Y=$TD_COMP_Y $ exit
TD_COMP=23474.9 TD_COMP_F=0.358203 TD_COMP_X=7 TD_COMP_Y=26
Instead of "set --", we could use arrays:
bash declare -a myarray myarray=( $(${CYGIM}magick compare \ rose: \ xc:blue \ -similarity-threshold 0 -dissimilarity-threshold 1 \ -subimage-search -metric RMSE \ NULL: 2>&1 | \ sed -e 's/[()@,+]/ /g') ) TD_COMP=${myarray[0]} TD_COMP_F=${myarray[1]} TD_COMP_X=${myarray[2]} TD_COMP_Y=${myarray[3]} echo TD_COMP=$TD_COMP TD_COMP_F=$TD_COMP_F TD_COMP_X=$TD_COMP_X TD_COMP_Y=$TD_COMP_Y $ exit
TD_COMP=23474.9 TD_COMP_F=0.358203 TD_COMP_X=7 TD_COMP_Y=26
All images on this page were created by the commands shown, using:
%IMG7%magick -version bash --version |head -n 3
Version: ImageMagick 7.1.0-42 Q16-HDRI x64 396d87c:20220709 https://imagemagick.org Copyright: (C) 1999 ImageMagick Studio LLC License: https://imagemagick.org/script/license.php Features: Cipher DPC HDRI OpenCL Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib Compiler: Visual Studio 2022 (193231332) GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG or TIFF or MIFF to JPG.
Source file for this web page is txtdata.h1. To re-create this web page, run "procH1 txtdata".
This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.
Anyone is permitted to re-publish this page, but only for non-commercial use.
Anyone is permitted to link to this page, including for commercial use.
Page version v1.0 6-September-2015.
Page created 14-Aug-2022 19:11:11.
Copyright © 2022 Alan Gibson.