I have a directory full of thumbnails of the rental tour I went on the other day while looking for a new apartment, and I want to put them online. Instead of putting each photo online, which I thought might be boring, I wanted to build a stack on images on a background.
I’m not really describing the effect well, so here’s the output so you know what I mean:
This was remarkably easy to create with a simple script that uses ImageMagick. Here’s the code:
-
#!/bin/bash
# Setup the random number stuff
LOBOUND=1
HIBOUND=600
RANDMAX=32767
# Create a white image to start with
convert -size $HIBOUND"x"$HIBOUND xc:lightgray output.png
for img in img*jpg
do
echo "Processing $img"
# Rotate the image if needed
rotate=""
if [ `exif $img | grep Orientation | grep bottom | wc -l | tr -d " "` -gt 0 ]
then
convert -rotate -90 $img img.png
else
convert $img img.png
fi
imgwidth=`identify -ping img.png | cut -f 3 -d " " | cut -f 1 -d "x"`
imgheight=`identify -ping img.png | cut -f 3 -d " " | cut -f 2 -d "x"`
# For more information on bounded numbers with bash, see
# http://www.stillhq.com/bashrand/
x=$(( $LOBOUND + (($HIBOUND - $imgwidth) * $RANDOM) / ($RANDMAX + 1) ))
y=$(( $LOBOUND + (($HIBOUND - $imgheight) * $RANDOM) / ($RANDMAX + 1) ))
# Put the image on top of the accumulative image
mv output.png input.png
convert -draw "image atop $x,$y $imgwidth,$imgheight img.png" input.png output.png
rm img.png
done
First the script creates an empty image to place the thumbnails over. Then for each image it checks if the image needs to be rotated, and what size it is. I use the size of the image to determine what the maximum value for the two random values I need are (the random values from the coordinates of the top left of the thumbnail image, and I don’t want any images falling off the edge of the new image). The the image is placed on top of the accumulating image.
It’s just a little ImageMagick hack I thought I would share.