Page 1 of 1
Batch composing multiple files
Posted: 2012-07-18T02:10:31-07:00
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...
Re: Batch composing multiple files
Posted: 2012-07-18T07:34:16-07:00
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
in order to make it
really run against the number of file pairs you have to process....
Re: Batch composing multiple files
Posted: 2012-07-19T01:20:51-07:00
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...
However, trying to run your code from a .bat file produces this error message:
i was unexpected at this time.
Re: Batch composing multiple files
Posted: 2012-07-19T09:11:47-07:00
by fmw42
the code I believe was unix not dos. see
http://www.imagemagick.org/Usage/windows/
Re: Batch composing multiple files
Posted: 2012-07-19T09:26:42-07:00
by GiladD
Should have mentioned I use Windows. Is there a DOS equivalent to do that?
Re: Batch composing multiple files
Posted: 2012-07-19T10:01:58-07:00
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.
Re: Batch composing multiple files
Posted: 2012-07-20T02:21:59-07:00
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...)
Re: Batch composing multiple files
Posted: 2012-07-20T02:54:41-07:00
by GiladD
You were almost there... Some research revealed that it actually needs to be:
Thanks for everyone for their help, though!