Page 1 of 1

Need help batch combining/overlaying different folders of images

Posted: 2016-05-16T00:18:11-07:00
by srinivasv
I have a slightly different problem, I would like to append the first image in Folder A with the first image in Folder B. Append the 2nd image in Folder A with the 2nd image in Folder B and repeat the process for all files. Both folders have the same number of images and are of the same size and resolution. I would like the output to be saved in Folder C. I have over 1000 images and I would like the output to serially numbered, starting with "merged0001.png" and saved in Folder C. I use ubuntu 14.04. any help with the script will be very helpful. Thanks.

Re: Need help batch combining/overlaying different folders of images

Posted: 2016-05-16T10:48:35-07:00
by vonbiber
something like this, assuming your images are all *.png
replace A, B, C to the actual paths of your folders A, B, C
Test it before using it (use echo, e.g.,

Code: Select all

echo "convert $f $g -append $C/$(printf "merged%04d.png" $i)

Code: Select all

#!/bin/sh

A=/path/to/folderA
B=/path/to/folderB
C=/path/to/folderC

ls $B/*.png > listB.txt

i=0
for f in $A/*.png
do
        i=$((i+1))
        g=$(head -n$i listB.txt | tail -n1)
        convert $f $g -append $C/$(printf "merged%04d.png" $i)
done

Re: Need help batch combining/overlaying different folders of images

Posted: 2016-05-16T18:57:41-07:00
by GeeMack
srinivasv wrote:I have a slightly different problem, I would like to append the first image in Folder A with the first image in Folder B. Append the 2nd image in Folder A with the 2nd image in Folder B and repeat the process for all files. Both folders have the same number of images and are of the same size and resolution. I would like the output to be saved in Folder C.
I'm using ImageMagick 6.9.3-9 on Windows 7 64, so I haven't tested it on *nix shell, but something like this should get you going in the right direction...

Code: Select all

convert \
   FolderA/*.png \
   -gravity north \
   -extent 100%x200% \
   null: \
   FolderB/*.png \
   -gravity south \
   -layers composite \
   -scene 1 \
      FolderC/merged%04d.png
That will bring all the PNG images from FolderA into a stack.

It sets the gravity to "north" and increases the canvas size of those FolderA images to 200% of the height by adding more canvas to the bottom of each.

Then it calls in the "null:" image to separate the stack you just created from the next stack coming up. <-- Very important.

Then it will bring in all the PNG images from FolderB.

Next it sets the gravity to "south" so the composite operation places the FolderB images on the lower half of the now extended FolderA images.

The "-layers composite" operation alternately takes the images from FolderA and FolderB stacks, compositing each FolderB image onto the bottom half of its companion FolderA image. That's what the "null:" between the two stacks of images does.

It finishes by putting all the output images in FolderC, numbered to 4 places, starting at 1 with "-scene 1", and with filenames like "merged0001.png", "merged0002.png", ... "merged9999.png".

You may have to swap the gravity north/south if you want the FolderB image on top.

You can even change it to east/west if you want them appended side by side instead of over/under. You'll need to change the extent operation to "-extent 200%x100%" to double the width instead of height of the canvas if you make them go side by side.

Re: Need help batch combining/overlaying different folders of images

Posted: 2016-05-17T06:05:38-07:00
by srinivasv
Thanks. Tested it on a small set. Worked perfectly!

[quote="vonbiber"]something like this, assuming your images are all *.png
replace A, B, C to the actual paths of your folders A, B, C