Well, the easiest way to do this would be to use ImageMagick. It should be in the repositories of your Linux distribution, for debian based systems, run:
sudo apt-get install imagemagick
One of the programs of the ImageMagick suite is identify
, this will print the characteristics of a list of input image files. Combining it with sort
will give you a list of images sorted by size (you can change png for whatever extension(s) you have:
identify *png | sort -gk 3
If you actually need the aspect ratio and not just the size, try something like this:
Simple option, assumes your image names have no spaces:
identify *png *jpg *gif | \
gawk '{split($3,sizes,"x"); print $1,sizes[1]/sizes[2]}' | \
sed 's/\[.\]//' | sort -gk 3
The gawk command splits the 3rd field (the image size which has the format LxH) into the array "sizes" and then prints the 1st field (the image name) and the result of dividing the image's length by its height. The sed
command is just beautifying the output and the sort
command sorts the result according to image size ratio.
More complex, this one can deal with spaces in file names:
find . \( -iname "*png" -o -iname "*jpg" -o -iname "*gif" \) -exec identify {} \; |\
perl -ne '/(.+?)\s+[A-Z]{3}\s+(\d+)x(\d+)/; print "$1 ", $2/$3, "\n"' | \
sort -gk 2
Here we are using find
to identify the files we are interested in and run the identify
command, and then piping its output through a little PERL script. The regular expression looks for three capital letters ([A-Z]{3}
) which should be the image format. Once we have found that, it is easy to identify the image name and dimensions.
I am not using gawk here because the presence of spaces in the input file names will confuse the field numbers. Finally, the script will print out the image's name and the result of the length/height division which we sort
numerically.
If simply browsing the available aspect ratios is not enough, if you have at least one image with the desired aspect ratio, just use grep to extract those images whose ratio is closest:
identify *png *jpg *gif | \
gawk '{split($3,sizes,"x"); print $1,sizes[1]/sizes[2]}' |\
sed 's/\[.\]//' | sort -gk 3 | grep -C 10 GOOD_IMAGE.jpg
identify -format "%[fx:w/h]:%M\n" *.jpg
. I dunno how to easily sort by proximity to an arbitrary value.sort
does not seem to support such sorting.