How to create a loop to batch combine 2 images into 1

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
monicam
Posts: 5
Joined: 2018-04-07T21:33:41-07:00
Authentication code: 1152

How to create a loop to batch combine 2 images into 1

Post by monicam »

Hi,

I'm trying to find a way to batch combine multiple pairs of images into a single image. I have a folder of hundreds of photos -- img01, img02,... -- and I'm trying to merge pairs of these into 1: img01 + img02 --> img01_02
img 519 + img 520 --> img519_520

The images all have the same dimensions.

I have seen a couple of threads that are pretty similar and have tried the code below, but these only work for the first 6-10 images and I need to be able to loop the commands over hundreds of photos.

Code: Select all

arr=(`ls *.jpg`)
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
j=$((i+1))
k=$((i+2))
convert ${arr[$i]} ${arr[$j]} +append newimage_${j}_${k}.jpg
done

I'm using a Mac OS High Sierra 10.13.4 and my imagemagick is version 6.9.1-0 Q16 x86_64 2015-03-22. I am running the commands on terminal.

Any help would be really appreciated. Thanks a lot in advance!

Similar threads I've seen:
viewtopic.php?t=32333
https://imagemagick.org/discourse-serve ... hp?t=24302
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to create a loop to batch combine 2 images into 1

Post by snibgo »

monicam wrote:...but these only work for the first 6-10 images...
What happens after 6-10 images? What stops it from continuing?
snibgo's IM pages: im.snibgo.com
monicam
Posts: 5
Joined: 2018-04-07T21:33:41-07:00
Authentication code: 1152

Re: How to create a loop to batch combine 2 images into 1

Post by monicam »

Hi @snibgo, I’m not sure why it only works for the first 6-10 photos. Would you know other ways to loop?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a loop to batch combine 2 images into 1

Post by fmw42 »

What do you mean by the first 6-10? Do you mean sometimes it stops at 6 and other times at 10? Are all your images valid? Do you get any error messages when it stops from convert?

If you have 10 images, then your code works:

Code: Select all

for ((i=0; i<10; i=i+2)); do
> j=$((i+1))
> echo "$i $j"
> done
0 1
2 3
4 5
6 7
8 9
0 is the first image and 9 is the 10th image.

But if you have 11 image, then it ask for one too many:

Code: Select all

for ((i=0; i<11; i=i+2)); do
> j=$((i+1))
> echo "$i $j"
> done
0 1
2 3
4 5
6 7
8 9
10 11
0 is the first image and 10 is the 11th image and 11 is the 12th image. But you do not have image 12, only 11 images.

Perhaps you should do a While loop or Until loop, testing on j. Or put a test for j in your for loop and break.
monicam
Posts: 5
Joined: 2018-04-07T21:33:41-07:00
Authentication code: 1152

Re: How to create a loop to batch combine 2 images into 1

Post by monicam »

Hi @fmw42, thanks for your reply. Actually I’m wondering if there is a way to loop over maybe a hundred or more photos. Would you know how to do that? Thanks a lot
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to create a loop to batch combine 2 images into 1

Post by snibgo »

What goes wrong with the original script? Try replacing the convert command with:

Code: Select all

echo [${arr[$i]}]  [${arr[$j]}]
This should list the paired filenames.

Do your filenames have spaces or other weird characters? If they have spaces, you need to quote them.
snibgo's IM pages: im.snibgo.com
monicam
Posts: 5
Joined: 2018-04-07T21:33:41-07:00
Authentication code: 1152

Re: How to create a loop to batch combine 2 images into 1

Post by monicam »

Hi @snibgo,

When I replace the echo command with the convert command or when I run both, the result is still that only 8 images get merged to 4 photos. The rest of the photos get ignored.

The commands I tried are:

Code: Select all

arr=(`ls *.jpg`)
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
echo “$i $j”
j=$((i+1))
k=$((i+2))
convert ${arr[$i]} ${arr[$j]} +append newimage_${j}_${k}.jpg
echo [${arr[$i]}]  [${arr[$j]}] 
done
Also:

Code: Select all

arr=(`ls *.jpg`)
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
echo “$i $j”
j=$((i+1))
k=$((i+2))
echo [${arr[$i]}]  [${arr[$j]}] 
done
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to create a loop to batch combine 2 images into 1

Post by snibgo »

Did the echo give a list that includes all the files? Is that list the same as "ls *.jpg", but with files paired together? What is the value of "num"?
snibgo's IM pages: im.snibgo.com
monicam
Posts: 5
Joined: 2018-04-07T21:33:41-07:00
Authentication code: 1152

Re: How to create a loop to batch combine 2 images into 1

Post by monicam »

Hi @snibgo,

The terminal gives out the following:

“0 ??
[img1.jpg] [img15.jpg]
“2 ??
[img2.jpg] [img21.jpg]
“4 ??
[img24.jpg] [img36.jpg]
“6 ??
[img4.jpg] []

I actually tried renaming the photos and it merges together the same photos regardless of what they're named. And it skips over everything else. Do the photos need to be a certain size/aspect ratio so that is why the others are skipped? I thought initially that maybe it skipped over images that were not 900 x 1200, but it skipped some of those as well
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a loop to batch combine 2 images into 1

Post by fmw42 »

Try renaming them with leading zeros, e.g img001.jpg ---- img035.jpg. Did you see my note above about your looping asking for too many images.
Post Reply