Skip to content

Instantly share code, notes, and snippets.

@usr-ein
Last active June 28, 2021 13:08
Show Gist options
  • Save usr-ein/d506480292bb1afd19c31f6f6875c9e1 to your computer and use it in GitHub Desktop.
Save usr-ein/d506480292bb1afd19c31f6f6875c9e1 to your computer and use it in GitHub Desktop.
Average Resolution - Computes the average, min and max height/width of all images passed as arguments
#!/usr/bin/env bash
if [ "$#" -lt 1 ] || [ ! -f "$1" ]; then
echo "Usage: $0 1.jpg 2.jpg ..."
exit 1
fi
sizes=$(identify -format "%h %w\n" "$@" | awk -F ' ' '(NR==1){minh=$1;minw=$2;maxh=$1;maxw=$2}; (NR>0){if(minh>$1) minh=$1; if(maxh<$1) maxh=$1;if(minw>$2) minw=$2; if(maxw<$2) maxw=$2;}; {height += $1} {width += $2} END {print height/NR; print width/NR; print maxh; print minh; print maxw; print minw;}')
h=$(echo "$sizes" | sed -n 1p)
w=$(echo "$sizes" | sed -n 2p)
maxh=$(echo "$sizes" | sed -n 3p)
minh=$(echo "$sizes" | sed -n 4p)
maxw=$(echo "$sizes" | sed -n 5p)
minw=$(echo "$sizes" | sed -n 6p)
echo -e "\tMin\tAvg\tMax"
echo -e "Height\t${minh}\t${h}\t${maxh}"
echo -e "Width\t${minw}\t${w}\t${maxw}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment