Batch composing multiple files

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
GiladD
Posts: 4
Joined: 2012-07-18T01:54:32-07:00
Authentication code: 15

Batch composing multiple files

Post by GiladD »

Hi all,

This is my first attempt at scripting IM, so please forgive me if it's a very silly question.
Basically, my task is to take a folder of multiple pairs of images, overlay them and then save under a new name.
So the file names are:
p1_bg.jpg
p1_fg.png
p2_bg.jpg
p2_fg.png
And I want the combined files to be named p1.jpg, p2.jpg, etc.

Overlaying the images is simple enough using the composite command, like so:
composite -gravity center -quality 100 p1_fg.png p1_bg.jpg p1.jpg

What I'm struggling with is converting it into a batch process and naming the output files sequentially. I keep getting a single file with all of the images overlaid one on top of another, instead of multiple ones. I've tried various things, both in the IM syntax and using loops in the batch file itself, but to no avail.
Any help is greatly appreciated...
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: Batch composing multiple files

Post by pipitas »

GiladD wrote:Hi all,

This is my first attempt at scripting IM, so please forgive me if it's a very silly question.
Basically, my task is to take a folder of multiple pairs of images, overlay them and then save under a new name.
So the file names are:
p1_bg.jpg
p1_fg.png
p2_bg.jpg
p2_fg.png
And I want the combined files to be named p1.jpg, p2.jpg, etc.

Overlaying the images is simple enough using the composite command, like so:
composite -gravity center -quality 100 p1_fg.png p1_bg.jpg p1.jpg

What I'm struggling with is converting it into a batch process and naming the output files sequentially. I keep getting a single file with all of the images overlaid one on top of another, instead of multiple ones. I've tried various things, both in the IM syntax and using loops in the batch file itself, but to no avail.
Any help is greatly appreciated...
Try this:

Code: Select all

for i in $(seq 1 12); do 
   echo \
       composite -gravity center -quality 100 p${i}_{fg,bg}.png p${i}.jpg; 
done
Explanation:
  • for ...; do ... ; done : this is called a loop. It causes the command(s) following the do to be run for each iteration of the loop.
  • seq 1 12 : this results in a list of numbers, from 1 to 12. Change '12' if you want another number.
  • echo \ : echo is included as a temporary safeguard. It only prints the command instead of running it. (The \ character is a 'line continuation' sign only inserted for better structure and means: "Pretend I'm not here and the next line is one the same one as I am..."))
  • ${i} : expands into the current number from the sequence defined in the for part.
  • {fg,bg} : tells the shell to include first 'fg', then 'bg' into the 'p${i}...png' names.
If the above loop prints out the 12 commandlines you are looking for, then it is save to remove the line saying

Code: Select all

echo \
in order to make it really run against the number of file pairs you have to process....
GiladD
Posts: 4
Joined: 2012-07-18T01:54:32-07:00
Authentication code: 15

Re: Batch composing multiple files

Post by GiladD »

Thanks for your reply, pipitas. I should maybe have mentioned that I do have a lot of experience programming and scripting, just not with IM... :D
However, trying to run your code from a .bat file produces this error message:
i was unexpected at this time.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch composing multiple files

Post by fmw42 »

the code I believe was unix not dos. see http://www.imagemagick.org/Usage/windows/
GiladD
Posts: 4
Joined: 2012-07-18T01:54:32-07:00
Authentication code: 15

Re: Batch composing multiple files

Post by GiladD »

Should have mentioned I use Windows. Is there a DOS equivalent to do that?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch composing multiple files

Post by fmw42 »

GiladD wrote:Should have mentioned I use Windows. Is there a DOS equivalent to do that?
I believe that in Windows, BAT (batch) file scripts are in the DOS language. Best I can suggest is read the reference regarding IM usage in Windows. It has a section of scripting and loops.

Sorry I am not a Windows user. And cannot help further.
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: Batch composing multiple files

Post by pipitas »

GiladD wrote:Should have mentioned I use Windows. Is there a DOS equivalent to do that?
In a DOS CMD window (direct command) you would do:

Code: Select all

for %i in (1,1,12) do  composite  -gravity center  -quality 100  p%i_fg.png  p%i_bg.png  p%i.jpg
If you put this in a Batch file (indirect command), you have to use '%%i' instead of '%i'.

(Note: this is from memory -- these days I don't have a Windows system around any more to verify...)
GiladD
Posts: 4
Joined: 2012-07-18T01:54:32-07:00
Authentication code: 15

Re: Batch composing multiple files

Post by GiladD »

You were almost there... Some research revealed that it actually needs to be:

Code: Select all

for /L %i in (1,1,12) do ...
Thanks for everyone for their help, though!
Post Reply