Page 1 of 1

prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T13:36:06-07:00
by josephaaroncampbell
Hello!

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:
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:

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
  

Re: prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T13:51:23-07:00
by Bonzo
What happens if you change the backticks to " but you will then need to escape them here "%%[fx:maxima*100]"

Re: prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T14:27:45-07:00
by josephaaroncampbell
So I can remove the error following your advice and using:

Code: Select all

for /f %%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
But now my variable 'maxN' is equal to the word 'convert' . As if its evaluating the command as a string now?

Re: prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T14:33:23-07:00
by snibgo
Referring to your OP:

I suggest you copy-paste that "for" command. In the first copy, remove the "for" stuff, so the convert command just runs by itself, so you can see the exact command that is executed, and any error messages.

(Incidentally, you use "gamma = 1-pow", but I wonder if you intended "gamma = 1/pow".)

Re: prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T15:06:35-07:00
by josephaaroncampbell
Hey!

So removing the for command and just leaving the ImageMagick command works with out a problem. Right now I am working around this by breaking up what I want to happen into a couple steps instead of one for loop:

Code: Select all

  convert %name%%ext% -brightness-contrast %bright%,%Con% -level 0%%,%max%,%gamma% %name%-Temp.tif

  for /f %%d in ('convert %name%-Temp.tif -gravity center -crop 50%%x50%%+0+0 -format "%%[fx:maxima*100]" info:') do (set maxN=%%d)

  for /f %%n in ('identify -format "%%[fx:(%max%-%maxN%)]" xc:') do (set nBright=%%n)

I would like to avoid creating the temporary tiff to read the data from but if there isnt a work around then this will do


And Youre right, I could be dividing 1 by 'pow' but I am trying to acheive a gamma less than 1.0 for some tests and it was the only way i knew how.

Re: prevent negative number in 'for /f' loop from causing command to break? -brightness-contrast

Posted: 2016-05-24T15:27:06-07:00
by snibgo
josephaaroncampbell wrote:-brightness-contrast %bright%,%con%
You might try the correct syntax. See http://www.imagemagick.org/script/comma ... s-contrast. Separate with "x", not comma. (Command-processors will often separate comma-separated arguments into two separate arguments, but IM here expects only one argument.)