Need help batch combining/overlaying different folders of 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
srinivasv
Posts: 2
Joined: 2016-05-15T23:18:37-07:00
Authentication code: 1151

Need help batch combining/overlaying different folders of images

Post 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.
vonbiber
Posts: 13
Joined: 2009-08-18T07:55:47-07:00
Authentication code: 8675309

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

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post 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.
srinivasv
Posts: 2
Joined: 2016-05-15T23:18:37-07:00
Authentication code: 1151

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

Post 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
Post Reply