Page 1 of 1

Trouble making an efficient BAT file to overlay images

Posted: 2013-11-03T18:45:32-07:00
by Taruby
I'm trying to create a BAT file to overlay a specific series of transparent .png files over a specific series of background .png files.

This is what the BAT file will look like with my current knowledge:

Code: Select all

convert Original\image_001.png Overlay\image_001.png -gravity center -composite Result\image_001.png
convert Original\image_002.png Overlay\image_002.png -gravity center -composite Result\image_002.png
..
convert Original\image_999.png Overlay\image_999.png -gravity center -composite Result\image_999.png
I don't want to write 999 individual lines, but I don't know enough about coding to figure out what terms I need to google to figure out how to make it so I can perform the above using fewer lines of code.

EDIT: Thank you snibgo for the correction regarding the right order of the operators.

Re: Trouble making an efficient BAT file to overlay images

Posted: 2013-11-03T19:13:26-07:00
by snibgo
You have "-composite" in the wrong place. It should be the last thing before the output name, thus:

Code: Select all

convert Original\image_001.png Overlay\image_001.png -gravity center -composite Result\image_001.png

Re: Trouble making an efficient BAT file to overlay images

Posted: 2013-11-03T21:38:59-07:00
by Taruby
Thank you very much for the correction regarding the correct order of the operators, snibgo! Despite being in the incorrect position, the BAT still worked when I tested it earlier, so I didn't notice I made a mistake.

This is my current progress to making an efficient BAT file:

Code: Select all

for /L %%n in (1,1,999) do convert Original\image_%%n.png Overlay\image_%%n.png -gravity center -composite Result\image_%%n.png
I need to figure out how to get it to read file names with padded zeros, i.e. "001, 002.. 999". For some reason, it wants to read them numbered as "1, 2.. 999". I'm having a difficult time wrapping my head around this.

Re: Trouble making an efficient BAT file to overlay images

Posted: 2013-11-04T10:03:42-07:00
by snibgo
I don't know of any way to force "for" variables to have leading zeros. You can do it manually, in three steps.

Code: Select all

for /L %%n in (1,1,9) do convert Original\image_%%n.png Overlay\image_%%00n.png -gravity center -composite Result\image_%%00n.png

for /L %%n in (10,1,99) do convert Original\image_%%n.png Overlay\image_%%0n.png -gravity center -composite Result\image_%%0n.png

for /L %%n in (100,1,999) do convert Original\image_%%n.png Overlay\image_%%n.png -gravity center -composite Result\image_%%n.png
Or you could do it with a single "for" loop, creating a variable within the loop, setting it to %%n with the appropriate number of leading zeros.

Re: Trouble making an efficient BAT file to overlay images

Posted: 2013-11-04T12:27:12-07:00
by Taruby
The three step method works satisfactorily for what I'm trying to accomplish. Thank you very much for your help, snibgo! It's appreciated. =)

Re: Trouble making an efficient BAT file to overlay images

Posted: 2013-11-04T12:46:12-07:00
by snibgo
Here is a more elegant method of adding leading zeros.

Code: Select all

for /L %%n in (1,1,999) do (
set NUM=000%%n
set NUM=!NUM:~-3!
echo !NUM!
convert Original\image_!NUM!.png Overlay\image_!NUM!.png -gravity center -composite Result\image_!NUM!.png
)
This ensures we have 3 digits. To ensure we have (x, where x is an integer > 1) digits, put at least (x-1) zeros in the first "set NUM", and change "3" to "x" in the second "set NUM".