#!/bin/bash
# /usr/local/bin/imagescape
# https://crystalfaeries.net/posix/bin/imagescape
# celeste:crystalfaery IMAGESCAPE 2017-12-02 22:25:26+00:00
# from Dave Taylor http://www.linuxjournal.com/content/resizing-images-imagemagick
# arguments are image directory paths to process
# output is images sorted into portrait or landscape subfolders
# DEVELOPMENT INTENT: we would like to add a new feature,
# that imagescape can be extended to work recursively,
# and that as it descends a directory structure,
# it looks for files which may control the recursion,
# .include and .exclude, in the format of rsync.
# While we're hacking new code, we should do some sanity checks
# to NOT produce landscape or portrait subdirectories within
# a landscape or portrait directory, and if we do find such,
# we should intelligently correct the situation.
# An obvious step in that direction, is to DEFAULT to
# auto excluding landscape and portrait directories from recursion,
# yet some luser may manually create such a mess without our help :-)
let help=22
# CONFIGURATION
identify=/usr/bin/identify
dedupe=/usr/local/bin/fdedupe
name_tidy="/usr/local/bin/name_tidy -r"
# HELP
if [ "${1}" = "-h" ]
then # treat it as a help request
head -n ${help} $0
exit
fi
# SORT IMAGES
launchdir="`pwd`"
while [ $# -ne 0 ]
do
cd -P "${1}" || exit 1 # enter the pictures metadirectory
imagedir="`pwd`"
${name_tidy} # name_tidy
# clone sorted-level images into metadirectory
for f in {portrait,landscape}/*;do
ln $f 2>/dev/null
done
# clone metadirectory files into sorted subdirectories
for image in *.gif *.jpg *.png # what else?
do
let width=$("${identify}" "${image}" | cut -d\ -f3 | cut -dx -f1) 2>/dev/null \
&& let height=$("${identify}" "${image}" | cut -d\ -f3 | cut -dx -f2) \
&& if [ $width -le $height ]
then
mkdir -p portrait
ln "${image}" portrait/"${image}" 2>/dev/null
else
mkdir -p landscape
ln "${image}" landscape/"${image}" 2>/dev/null
fi
done
# delete top-level images
cd "${imagedir}"/portrait 2>/dev/null && for f in *.{gif,jpg,png};do rm ../"${f}" 2>/dev/null;done
cd "${imagedir}"/landscape 2>/dev/null && for f in *.{gif,jpg,png};do rm ../"${f}" 2>/dev/null;done
cd "${imagedir}" # back to metadirectory
shift # NEXT ARGUMENT
cd "${launchdir}" # in case arguments are relative to launchdir
done
exit $?
syntax highlighted by Code2HTML, v. 0.9.1