Panorama to square images for Instagram #

Instagram recently added a feature to post up to 10 photos side by side. I saw someone cleverly used it to post a single panoramic photo. You split the panorama into square photos and then post them all at once to Instagram, which then arranges them side by side. Here's a quick & dirty script I wrote to split up my own panoramas into square photos. It uses a few Mac commands to get the size of the image, and ImageMagick to break up the images into smaller ones:

#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 filename.jpg"
    exit
fi

filename="$1"

# Original image size
width=$(mdls -raw -name kMDItemPixelWidth "$filename")
height=$(mdls -raw -name kMDItemPixelHeight "$filename")

# Each of the square pieces will be 1/Nth the width
N=$[$width / $height]
if [ $N -gt 10 ]; then N=10; fi  # maximum 10 allowed by instagram

size=$[$width / N]
if [ $height -lt $size ]; then size=$height; fi

    
# Use the center of the x,y range
x_offset=$[$[$width - $size * N] / 2]
y_offset=$[$[$height - $size] / 2]


echo  "$filename : ${width}x${height}, $N parts"

if [ $x_offset -gt 0 ]; then
    echo "Wide image; truncating $[2 * $x_offset] pixels"
else
    x_offset=0
fi

if [ $y_offset -gt 0 ]; then
    echo "Tall image; truncating $[2 * $y_offset] pixels"
else
    y_offset=0
fi

for part in $(seq $[$N - 1] -1 0); do
    crop="${size}x${size}+$[${x_offset} + ${size}*${part}]+${y_offset}"
    echo "  part $part : $crop"
    convert "$filename" -crop "$crop" $part.jpg
done

I'm sure there are lots of things I could do better but I just wanted to get something working quickly.

Labels: