Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a hundred images and I want to average them 5 at a time.

The imagemagick command for the first 5 is:

convert -background transparent %d.jpg[1-5] -average output1.jpg

The imagemagick command for the second 5 is:

convert -background transparent %d.jpg[2-6] -average output2.jpg

The imagemagick command for the third 5 is:

convert -background transparent %d.jpg[3-7] -average output3.jpg

How can I do this in Windows programmatically? In other words, can I write a batch file to run imagemagick to do this with a loop? Or is there a better language/method/alternative?

share|improve this question
2  
Hi, welcome to Super User! Things work a bit differently here from how they are on traditional forums you might be used to. If your problem has been solved, you don't need to edit that into the title - just click the checkmark by the answer that solved your question, and it'll automatically accept that answer and mark your question as solved! You can learn more about how those types of things work here on the tour page. –  nhinkle May 5 '13 at 1:28
add comment

2 Answers

up vote 1 down vote accepted

This should be easy enough to do with a batch file like the following:

@echo off
setlocal enabledelayedexpansion
for /l %%f in (1,1,5) do (
    set /a cnt=%%f+4

    REM If the output looks ok, remove the "echo" from the following line to actually run convert
    echo convert -background transparent %%d.jpg[%%f-!cnt!] -average output%%f.jpg
)
endlocal

Here's the output with the "echo" in place:

convert -background transparent %d.jpg[1-5] -average output1.jpg
convert -background transparent %d.jpg[2-6] -average output2.jpg
convert -background transparent %d.jpg[3-7] -average output3.jpg
convert -background transparent %d.jpg[4-8] -average output4.jpg
convert -background transparent %d.jpg[5-9] -average output5.jpg

Tip: Be sure to specify the full path to imagemagick's convert.exe, otherwise Windows' own convert command (converts a FAT volume to NTFS) is likely to run by default.

share|improve this answer
    
Hey Karan - good answer, but would you mind adding an little extra info about what the batch file is actually doing? Delayed expansion is confusing (even as someone who's used it before), and it would be great to have some explanation of what the logical flow of the program is. –  nhinkle May 5 '13 at 1:32
    
Umm, I'm not even sure what sort of explanation about the flow I should include! %%f gets values from 1-5 in the for loop, and cnt gets %%f's current value + 4 (so you get 1-5, 2-6 etc. in the output). As for delayed expansion, hoo boy that's a whole can of worms. :) I'd highly encourage anyone interested to check out ss64.com/nt/for_l.html and ss64.com/nt/delayedexpansion.html for more (to my surprise the latter page coincidentally has a very similar example to the loop I used above!) –  Karan May 5 '13 at 1:49
    
That's basically what I was looking for - just a description of which loop does what. –  nhinkle May 5 '13 at 2:52
add comment

Here is the code that I cobbled together that works as a Windows batch file for thousands of images:

  1. rem we will combine 5 images
  2. set /a U=0
  3. set /a V=1
  4. set /a W=2
  5. set /a X=3
  6. set /a Y=4
  7. rem we will only look at the first 15 images
  8. set /a terminator=15
  9. :loop
  10. set /a W= %W% + 1
  11. set /a V= %V% + 1
  12. set /a U= %U% + 1
  13. set /a X= %X% + 1
  14. set /a Y= %Y% + 1
  15. if %Y%==%terminator% goto close
  16. goto open
  17. :close
  18. pause
  19. exit
  20. :open
  21. convert %%d.jpg[%U%-%Y%] -average -auto-level %U%out.jpg
  22. goto loop

There is some funny business using a for next loop where one needs to remember to set delayed local expression and to use the bang (!) character around the variable name instead of percent signs. I learned that independently before I came back here and saw that you had solved my question. A simple goto with a loop is more transparent, although the for statement is more elegant.

share|improve this answer
1  
I tried to upvote Karan's solution but i dont have enough reputation :( –  aquagremlin May 5 '13 at 1:24
1  
Accepting Karan's answer will give him reputation points for the answer, and mark your question as solved. You also get +2 reputation for yourself, just for accepting an answer on a question. I edited the formatting in your answer here; check out the formatting reference for more info. –  nhinkle May 5 '13 at 1:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.