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...
Batch composing multiple files
Re: Batch composing multiple files
Try this: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...
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
- 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.
Code: Select all
echo \
Re: Batch composing multiple files
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...
However, trying to run your code from a .bat file produces this error message:

However, trying to run your code from a .bat file produces this error message:
i was unexpected at this time.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Batch composing multiple files
the code I believe was unix not dos. see http://www.imagemagick.org/Usage/windows/
Re: Batch composing multiple files
Should have mentioned I use Windows. Is there a DOS equivalent to do that?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Batch composing multiple files
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.GiladD wrote:Should have mentioned I use Windows. Is there a DOS equivalent to do that?
Sorry I am not a Windows user. And cannot help further.
Re: Batch composing multiple files
In a DOS CMD window (direct command) you would do:GiladD wrote:Should have mentioned I use Windows. Is there a DOS equivalent to do that?
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
(Note: this is from memory -- these days I don't have a Windows system around any more to verify...)
Re: Batch composing multiple files
You were almost there... Some research revealed that it actually needs to be:
Thanks for everyone for their help, though!
Code: Select all
for /L %i in (1,1,12) do ...