Stitching 4 photos in 1

ยท 347 words ยท 2 minute read

I realised I had a lot of low-res photos from WhatsApp that were still worth printing. However if you’d send it straight to the printer this way they would look very bad.

So my thinking was: let’s print them quarter-size! But no printing service in the Netherlands can do that, apparently. So the next idea was to combine four photos in 1 and cut them myself.

I tried to do this in Affinity Photo, but I very quickly decided doing this manually for 150 photos was no fun at all. Manual labour sucks, computers are better at this!

And it’s easy peasy with GraphgicsMagick (or ImageMagick).

Usage ๐Ÿ”—

Install GraphicsMagick (brew install graphicsmagick). You can also use ImageMagick, in that case remove the gm from the script. I prefer GraphicsMagick as the image-quality is slightly better and it’s faster.

Copy all the photos you want combined into a single folder. Copy the script to the same folder (on a Mac you can copy the code below and do: pbpaste > crop_and_combine.sh; chmod +x crop_and_combine.sh). Then run the script ;) (./crop_and_combine.sh)

count=0
files=""
mkdir result

for i in *.jpg
do
  let count=count+1
  files=$files" "\"$i\"
  let four=count%4
  cp "$i" /tmp/$four.jpg
  gm convert /tmp/$four.jpg -rotate "90>" -resize "1800x2700^" \
    -gravity center -crop 1800x2700+0+0 +repage /tmp/$four.c.tiff;
  if [ "$four" -eq 0 ]
  then
    gm convert -append /tmp/1.c.tiff /tmp/2.c.tiff /tmp/v1.tiff;
    gm convert -append /tmp/3.c.tiff /tmp/0.c.tiff /tmp/v2.tiff;
    gm convert +append /tmp/v1.tiff /tmp/v2.tiff result/$count.jpg
  fi
done

if [ "$four" -ne 0 ]
then
  gm convert -append /tmp/1.c.tiff /tmp/2.c.tiff /tmp/v1.tiff;
  gm convert -append /tmp/3.c.tiff /tmp/0.c.tiff /tmp/v2.tiff;
  gm convert +append /tmp/v1.tiff /tmp/v2.tiff result/$count.jpg
fi

A quick rundown of the script:

  • it creates a result directory
  • it goes through the photos
  • crops each image to a certain aspect ratio (10/15) and rotates if necessary
  • every 4th photo it:
    • concatenates two photos vertically
    • concatenates the result horizontally
    • moves the result to the result directory
  • if there’s an uneven number of images, some of the last images get reused
  • some files are left in /tmp, you might want to remove those manually

Result! ๐Ÿ”—

Here’s four photos and the resulting stitch:

Resulting images