Page 1 of 1

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

Posted: 2018-04-07T22:21:39-07:00
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

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

Posted: 2018-04-08T06:13:16-07:00
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?

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

Posted: 2018-04-08T08:38:32-07:00
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?

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

Posted: 2018-04-08T11:01:49-07:00
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.

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

Posted: 2018-04-08T17:48:49-07:00
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

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

Posted: 2018-04-08T18:10:56-07:00
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.

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

Posted: 2018-04-08T19:32:42-07:00
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

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

Posted: 2018-04-08T20:47:44-07:00
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"?

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

Posted: 2018-04-08T22:37:08-07:00
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

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

Posted: 2018-04-08T23:35:27-07:00
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.