Trouble making an efficient BAT file to overlay images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Taruby
Posts: 3
Joined: 2013-11-03T18:10:57-07:00
Authentication code: 6789

Trouble making an efficient BAT file to overlay images

Post 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.
Last edited by Taruby on 2013-11-03T19:47:42-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trouble making an efficient BAT file to overlay images

Post 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
snibgo's IM pages: im.snibgo.com
Taruby
Posts: 3
Joined: 2013-11-03T18:10:57-07:00
Authentication code: 6789

Re: Trouble making an efficient BAT file to overlay images

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trouble making an efficient BAT file to overlay images

Post 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.
snibgo's IM pages: im.snibgo.com
Taruby
Posts: 3
Joined: 2013-11-03T18:10:57-07:00
Authentication code: 6789

Re: Trouble making an efficient BAT file to overlay images

Post 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. =)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trouble making an efficient BAT file to overlay images

Post 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".
snibgo's IM pages: im.snibgo.com
Post Reply