ImageMagic

montage - create a composite image by combining several separate images

#!/bin/bash

for i in *a.jpg; do
F=${i//a.jpg/}
echo $F
montage -label ${F} -compress "Lossless" -geometry +4+4 ${F}a.jpg ${F}b.jpg ./2/${F}.jpg
done