#! /bin/bash # Ratio.sh # This script displays the image ratio of an image (or images) # It relies on ImageMagick (or possibly GraphicsMagick) being available # It can also selectively copy images for a range of values # (c) Guillaume Dargaud - http://www.gdargaud.net/ # Last modification: 20010/11/25 # Options to pass to the copy command (see 'man cp') CpOpt=-vi # You shouldn't have to change anything below that ShowName=1 ShowRatio=1 Min=0 Max=9999999 function usage() { cat << EOF USAGE: $0 [Options] File [Files...] Gives the aspect ratio of an image (100*width/height), 100 being square and more than 100 for landscape frames. OPTIONS: -n Display filenames but not the aspect ratio -x Display aspect ratio but not the filenames -r Display 'Ratio Filename' instead of 'Filename Ratio', allowing you to pipe to 'sort -n' -g N Only display/copy if the ratio is greater then the value given (inclusive) -l N Only display/copy if the ratio is lower then the value given (inclusive) -c Dir Copy images that are within requested -g/-l values to a specific directory EXAMPLES: $0 -g \$((100*16/9)) *.jpg to list panoramic images above the 16:9 ratio $0 -c /tmp/tv -g 133 -l 133 *.tif to copy all 4:3 images to /tmp/tv EOF } while getopts hnxrg:l:c: opt; do case $opt in n) unset ShowRatio;; x) unset ShowName;; r) Reverse=1;; g) Min=$OPTARG;; l) Max=$OPTARG;; c) Dest=$OPTARG;; h) usage; exit 1;; ?) echo "Invalid option: -$OPTARG" >&2; exit 1;; esac done shift $(($OPTIND - 1)) if [ $# -eq 0 ]; then usage; exit 1; fi if [ "$ShowName" -a "$ShowRatio" ]; then Join=" "; fi while [ "$1" != "" ]; do Ratio=$(($(identify -format "100 * %w / %h\n" "$1"))) if [ $Min -le $Ratio -a $Ratio -le $Max ]; then # Deals with the displayed line if [ $ShowName ]; then Name="$1"; fi if [ $ShowRatio ]; then R=$Ratio; fi if [ "$ShowName" -o "$ShowRatio" ]; then if [ $Reverse ]; then echo "$R$Join$Name"; else echo "$Name$Join$R"; fi fi # Deal with the file copy if [ $Dest ]; then cp $CpOpt "$1" "$Dest/"; fi fi shift done