prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast
Posted: 2016-05-24T13:36:06-07:00
Hello!
So in a normal command prompt this line evaluates just fine:
However, in a batch file and within a for loop, the '-5' in -brightness-contrast causes the command to think '-5' is a file or function instead of part of -brightness-contrast. So this:
returns the error "unable to open image -5" and so forth for the rest of the function values
I am not sure how to get around this after a few hours of research and was hoping you may be able to help.
Windows 7 64bit
ImageMagick 6.9.2-0 Q16 x64 2015-08-15
delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
the full batch code ( still in progress! ):
So in a normal command prompt this line evaluates just fine:
Code: Select all
convert filename.tif -gravity center -crop 50%x50%+0+0 -brightness-contrast 4,-5 -level 0%,100%,1.0 -format "%[fx:maxima*100]" info:
Code: Select all
for /f "usebackq" %%o in (`convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -brightness-contrast 4,-5 -level 0%%,%max%,%gamma% -format "%%[fx:maxima*100]" info:`) do set maxN=%%o
returns the error "unable to open image -5" and so forth for the rest of the function values
I am not sure how to get around this after a few hours of research and was hoping you may be able to help.
Windows 7 64bit
ImageMagick 6.9.2-0 Q16 x64 2015-08-15
delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
the full batch code ( still in progress! ):
Code: Select all
FOR %%a in (*.tif) DO (
call :dequote %%a
)
:dequote
set fileName=%~n1
set fileExt=%~x1
set filePath=%~dp1
set name=%fileName%& set npath=%filePath%& set ext=%fileExt%
echo %name%
CD /d %~dp1
echo source=%cd%
set npath=%npath:\= %
set Last_Word=
for /f %%d in ('convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -format "%%[fx:maxima*100]" info:') do (set max=%%d)
for /f %%e in ('convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -format "%%[fx:minima]" info:') do (set min=%%e)
for /f %%f in ('convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -normalize -format "%%[fx:mean]" info:') do (set MN=%%f)
for /f %%h in ('convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -normalize -format "%%[fx:standard_deviation]" info:') do (set SDN=%%h)
for /f %%g in ('identify -format "%%[fx:%SDN%*10]" xc:') do (set Bright=%%g)
for /f %%g in ('identify -format "%%[fx:(%MN%-%min%)*-10]" xc:') do (set Con=%%g)
echo Max = %max%
echo Min = %min%
echo Mean = %MN%
echo bright = %bright%
echo contrast = %con%
echo SDN = %SDN%
for /F "usebackq" %%i in (`identify -format "%%[fx:abs(%SDN%-%MN%)]" xc:`) do (set diff=%%i)
for /F "usebackq" %%j in (`identify -format "%%[fx:pow(%diff%,0.5555)]" xc:`) do (set pow=%%j)
echo diff = %diff%
echo pow = %pow%
for /F "usebackq" %%k in (`identify -format "%%[fx:1-%pow%]" xc:`) do (set gamma=%%k)
echo gamma = %gamma%
for /f "usebackq" %%o in (`convert %name%%ext% -gravity center -crop 50%%x50%%+0+0 -brightness-contrast %bright%,%con% -level 0%%,%max%,%gamma% -format "%%[fx:maxima*100]" info:`) do set maxN=%%o
for /f %%n in ('identify -format "%%[fx:(%max%-%maxN%)]" xc:') do (set nBright=%%n)
pause
convert %name%%ext% -brightness-contrast %nbright%,%Con% -level 0%%,%max%,%gamma% %~dp0output\%name%.tif