Skip to content

Instantly share code, notes, and snippets.

@johnxx
Created May 6, 2016 18:05
Show Gist options
  • Save johnxx/bc2ac1b6fb18ac61a3d2930f1db77f0e to your computer and use it in GitHub Desktop.
Save johnxx/bc2ac1b6fb18ac61a3d2930f1db77f0e to your computer and use it in GitHub Desktop.
Links pictures over a certain width (and ratio) to a new directory
#!/bin/sh
[ $1 != '' ] || exit 1
source=$1
width=$2
IFS='
'
debug=1
mkdir -p $width
for n in $(find $source -type f -regextype posix-egrep \
-regex '.*.(png|jpg|jpeg|gif)') ; do
filename=$(echo $n | cut -d/ -f2)
if [ -f $width/"$filename" ] ; then
if [ $debug ] ; then
echo "$filename: Already linked."
fi
continue
fi
result=$(gm identify -format %f:%w:%h "$n" 2> /dev/null)
w=$(echo $result | cut -d: -f2)
h=$(echo $result | cut -d: -f3)
# Has a width? wider than our minimum? ratio between 1:1 and 2:1?
if ! [ -z "$w" ] && [ $w -ge $width ] && [ $(echo $w/$h | bc) -eq 1 ]; then
if [ $debug ] ; then
echo "$filename ($w x $h): Linking"
fi
ln -sf ../"$n" $width
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment