Page 1 of 1
[SOLVED] batch file to average 3 jpgs at a time
Posted: 2013-05-02T15:38:37-07:00
by bwana
I am using this batch file:
for %%U in (*.jpg) do (convert [%%~nU,%%~nU+1,%%~nU+2].jpg -average %%~nUoutput.jpg)
I have 7 test jpgs named 1.jpg, 2.jpg,etc.
the contents of each jpg is a number
1.jpg is a picture of the number 1 in the left margin
2.jpg is a picture of the number 2 slightly more to the right of where the 1 was in the previous picture
3.jpg is a picture of the number 3 slightly more to the right of where the 2 was in the previous picture
etc
when the batch file is invoked in a command window,
C:\Users\Stefan\Desktop\test\minitest>bconvert.bat
this is what i see
C:\Users\Stefan\Desktop\test\minitest>for %U in (*.jpg) do (convert [%~nU,%~nU+1,%~nU+2].jpg -average %~nUoutput.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [1,1+1,1+2].jpg -average 1output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [2,2+1,2+2].jpg -average 2output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [3,3+1,3+2].jpg -average 3output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [4,4+1,4+2].jpg -average 4output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [5,5+1,5+2].jpg -average 5output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [6,6+1,6+2].jpg -average 6output.jpg )
C:\Users\Stefan\Desktop\test\minitest>(convert [7,7+1,7+2].jpg -average 7output.jpg )
C:\Users\Stefan\Desktop\test\minitest>
the output are 7 files named 1output.jpg, 2output.jpg,etc
the contents of the files are
1output.jpg is the number 1
2output.jpg are the numbers 1,2
3output.jpg are the numbers 1,2,3
4output.jpg are the numbers 1,2,4
5output.jpg are the numbers 1,2,5
etc
Well, obviously, i am doing something wrong and the summation is not occcurring in the brackets.
For example, (convert [4,4+1,4+2].jpg -average 4output.jpg ) is being read as
(convert [4,1,2].jpg -average 4output.jpg )
How can I fix this?
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T15:52:00-07:00
by snibgo
You need to do the arithmetic in your script, before "convert", within the FOR brackets. Eg:
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T17:35:03-07:00
by anthony
you could do this all in one command too...
Code: Select all
convert %d.jpg[1-7] ^
( -clone 0-2 -average -write 1output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 2output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 3output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 4output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 5output.jpg +delete ) -delete 0 ^
null:
Each time it averages and writes the first three images,
then one image is removed and it repeats.
All you need is to generate the right number of repeated lines (7-2 times in this case)
NOTE the %d reads images from 0 to the first image where %d failed to match a filesname.
It is not used much, probably as it was not exampled very well,
See File handling, Read Modifiers (the first modifier)
http://www.imagemagick.org/Usage/files/#read_mods
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T17:51:52-07:00
by bwana
snibgo wrote:You need to do the arithmetic in your script, before "convert", within the FOR brackets. Eg:
I used this line
for %%U in (*.jpg) do set /A V=%%~nU+1 set /A W=%%~nU+2 (convert [%%~nU,V,W].jpg -average %%~nUoutput.jpg)
and got these results
C:\Users\Stefan\Desktop\test\minitest>bconvert.bat
C:\Users\Stefan\Desktop\test\minitest>for %U in (*.jpg) do set /A V=%~nU+1 set /A W=%~nU+2 (convert [%~nU,V,W].jpg -average %~nUoutput.jpg)
C:\Users\Stefan\Desktop\test\minitest>set /A V=1+1 set /A W=1+2 (convert [1,V,W].jpg -average 1output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=2+1 set /A W=2+2 (convert [2,V,W].jpg -average 2output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=3+1 set /A W=3+2 (convert [3,V,W].jpg -average 3output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=4+1 set /A W=4+2 (convert [4,V,W].jpg -average 4output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=5+1 set /A W=5+2 (convert [5,V,W].jpg -average 5output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=6+1 set /A W=6+2 (convert [6,V,W].jpg -average 6output.jpg)
Missing operator.
C:\Users\Stefan\Desktop\test\minitest>set /A V=7+1 set /A W=7+2 (convert [7,V,W].jpg -average 7output.jpg)
Missing operator.
scratches head?
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T18:09:30-07:00
by bwana
anthony wrote:you could do this all in one command too...
Code: Select all
convert %d.jpg[1-7] ^
( -clone 0-2 -average -write 1output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 2output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 3output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 4output.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write 5output.jpg +delete ) -delete 0 ^
null:
Each time it averages and writes the first three images,
then one image is removed and it repeats.
All you need is to generate the right number of repeated lines (7-2 times in this case)
NOTE the %d reads images from 0 to the first image where %d failed to match a filesname.
It is not used much, probably as it was not exampled very well,
See File handling, Read Modifiers (the first modifier)
http://www.imagemagick.org/Usage/files/#read_mods
the problem is that i am developing a script to operate on thousands of files. This approach would be thousands of lines long!
Anyway I tried your code and got this
C:\Users\Stefan\Desktop\test\minitest>convert d.jpg[1-7] ( -clone 0-2 -average -write 1output.jpg +delete ) -delete 0 ( -clone 0-2 -average -write 2output.jpg +delete ) -delete 0 ( -clone 0-2 -average -write 3output.jpg +delete ) -delete 0 ( -clone 0-2 -average -write 4output.jpg +delete ) -delete 0 ( -clone 0-2 -average -write 5output.jpg +delete ) -delete 0 null:
convert.exe: unable to open image `d.jpg': No such file or directory @ error/blob.c/OpenBlob/2644.
convert.exe: image sequence is required `-clone' @ error/convert.c/ConvertImageCommand/942.
but changing the %d in your code to %%d
allows it to work.
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T18:33:02-07:00
by bwana
Anthony,
I also tried this
for %%U in (*.jpg) do convert %%U ( -clone 0-2 -average -write %%Uoutput.jpg +delete ) -delete 0
null:
and got this
C:\Users\Stefan\Desktop\test\minitest>for %U in (*.jpg) do convert %U ( -clone 0-2 -average -write %Uoutput.jpg +delete ) -delete 0
C:\Users\Stefan\Desktop\test\minitest>convert 1.jpg ( -clone 0-2 -average -write 1.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 2.jpg ( -clone 0-2 -average -write 2.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 3.jpg ( -clone 0-2 -average -write 3.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 4.jpg ( -clone 0-2 -average -write 4.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 5.jpg ( -clone 0-2 -average -write 5.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 6.jpg ( -clone 0-2 -average -write 6.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>convert 7.jpg ( -clone 0-2 -average -write 7.jpgoutput.jpg +delete ) -delete 0
convert.exe: option requires an argument `-delete' @ error/convert.c/ConvertImageCommand/1182.
C:\Users\Stefan\Desktop\test\minitest>null:
'null:' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Stefan\Desktop\test\minitest>
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T18:38:59-07:00
by snibgo
I said within the FOR brackets, not outside them.
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T19:52:07-07:00
by bwana
snibgo wrote:I said within the FOR brackets, not outside them.
I am tired and dont know what you mean-there are no brackets for me to put the set statements into. I tried these and they all FAILED
for %%U in (*.jpg ) do ( set /A V=%%~nU+1 set /A W=%%~nU+2 convert [%%~nU,%V%,%W%].jpg -average %%~nUoutput.jpg)
or
for %%U in (*.jpg ) (set /A V=%%~nU+1 set /A W=%%~nU+2) do ( convert [%%~nU,%V%,%W%].jpg -average %%~nUoutput.jpg)
or
for %%U set /A V=%%~nU+1 set /A W=%%~nU+2 in (*.jpg ) do ( convert [%%~nU,%V%,%W%].jpg -average %%~nUoutput.jpg)
or
for %%U in (*.jpg ) set /A V=%%~nU+1 set /A W=%%~nU+2 do ( convert [%%~nU,%V%,%W%].jpg -average %%~nUoutput.jpg)
wtf
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T20:46:11-07:00
by snibgo
Code: Select all
for %%U in (*.jpg) do (
set /A V=%%~nU+1
set /A W=%%~nU+2
convert [%%~nU,!V!,!W!].jpg -average %%~nUoutput.jpg
)
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-02T23:39:03-07:00
by anthony
bwana wrote:Anthony,
I also tried this
for %%U in (*.jpg) do convert %%U ( -clone 0-2 -average -write %%Uoutput.jpg +delete ) -delete 0
null:
Mine was a single command that did not require any FOR loop.
But as you say this is for 1000's of images than that would not be suitable.
I can think of a pipelined method for IMv7 (still in alpha testing) where the FOR look would control a single background process (so the last two images are help while thrid is read in and processed), but it is not ready for yet, and I am not certain you can do this from DOS.
Looks like the other method (calculate the other image numbers and one one command for each merge) would be best.
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-03T05:23:31-07:00
by bwana
tnx snibgo but your command also did not work. it's the same issue i ran into. the variables v and w are not being evaluated. here is what I saw:
Code: Select all
C:\Users\Stefan\Desktop\test\minitest>bconvert.bat
C:\Users\Stefan\Desktop\test\minitest>for %U in (*.jpg) do (
set /A V=%~nU+1
set /A W=%~nU+2
convert [%~nU,!V!,!W!].jpg -average %~nUoutput.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=1+1
set /A W=1+2
convert [1,!V!,!W!].jpg -average 1output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=2+1
set /A W=2+2
convert [2,!V!,!W!].jpg -average 2output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=3+1
set /A W=3+2
convert [3,!V!,!W!].jpg -average 3output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=4+1
set /A W=4+2
convert [4,!V!,!W!].jpg -average 4output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=5+1
set /A W=5+2
convert [5,!V!,!W!].jpg -average 5output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=6+1
set /A W=6+2
convert [6,!V!,!W!].jpg -average 6output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=7+1
set /A W=7+2
convert [7,!V!,!W!].jpg -average 7output.jpg
)
i changed the bangs to percents and got this
Code: Select all
C:\Users\Stefan\Desktop\test\minitest>bconvert.bat
C:\Users\Stefan\Desktop\test\minitest>for %U in (*.jpg) do (
set /A V=%~nU+1
set /A W=%~nU+2
convert [%~nU,8,9].jpg -average %~nUoutput.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=1+1
set /A W=1+2
convert [1,8,9].jpg -average 1output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=2+1
set /A W=2+2
convert [2,8,9].jpg -average 2output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=3+1
set /A W=3+2
convert [3,8,9].jpg -average 3output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=4+1
set /A W=4+2
convert [4,8,9].jpg -average 4output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=5+1
set /A W=5+2
convert [5,8,9].jpg -average 5output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=6+1
set /A W=6+2
convert [6,8,9].jpg -average 6output.jpg
)
C:\Users\Stefan\Desktop\test\minitest>(
set /A V=7+1
set /A W=7+2
convert [7,8,9].jpg -average 7output.jpg
)
Now the V,W evaluate to 8 and 9 only.
using SetLocal EnableDelayedExpansion does not help either.
this is so frustrating.
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-03T06:07:42-07:00
by bwana
FINALLY!!!
got this to work
set /a U=0
set /a V=1
set /a W=2
set /a terminator=7
:loop
set /a W= %W% + 1
set /a V= %V% + 1
set /a U= %U% + 1
if %W%==%terminator% goto close
goto open
:close
echo %W%
pause
exit
pen
echo %U% %V% %W%
convert %%d.jpg[%U%-%W%] ^
( -clone 0-2 -average -write %U%out.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write %V%out.jpg +delete ) -delete 0 ^
( -clone 0-2 -average -write %W%out.jpg +delete ) -delete 0 ^
null:
goto loop
thank you for your help snipgo and anthony.
without your hints I never would have figured it out.
i still need to understand the clone command better. even after 4 images are out put properly as a result of the loop(the last image is when U is 4, V is 5 and W is 6), two more images are output:
output 5 contains only the digits 5 and 6
output 6 contains only the digit 6
there is no output image 7
Re: batch file to average 3 jpgs at a time
Posted: 2013-05-03T06:15:26-07:00
by bwana
And this works too - without the clone command
set /a U=0
set /a V=1
set /a W=2
set /a terminator=7
:loop
set /a W= %W% + 1
set /a V= %V% + 1
set /a U= %U% + 1
if %W%==%terminator% goto close
goto open
:close
echo %W%
pause
exit
pen
echo %U% %V% %W%
convert %%d.jpg[%U%-%W%] -average %U%out.jpg
goto loop