Ubuntu: Sort by image resolution (dimensions)



Question:

Which program have a function to sort images by thier pixel size / resolution / dimensions.

Very strange but even so powerfull DigiKam doesn't have so simple function.

Any suggestions?


Solution:1

If you're looking for a command-line tool, the following command will sort image files where the highest resolution comes first:

find . -iname "*.png" -o -iname "*.jpg" | perl -e '$f=sub{eval(`identify -format %w*%h ${\quotemeta(shift)} 2>/dev/null`)};print sort{$f->($b)<=>$f->($a)}<>'  

I'm basically sorting the files using perl sort sub-subroutine where the key here is to call the identify command and eval its formatted output.

identify is available with the imagemagick package:

sudo apt-get install imagemagick  

UPDATE:

The following command will also output the resolution:

find . -iname "*.png" -o -iname "*.jpg" | perl -e '$f=sub{`identify -format %w*%h ${\quotemeta(shift)} 2>/dev/null`};chomp&&print "$_\t".$f->($_) for sort{eval($f->($b))<=>eval($f->($a))}<>'  

Example:

./foo bar.png   1600*900  ./baz.png       1600*900  ./img_0004.jpg  1280*720  ./img_0006.jpg  1280*720  ./img_0001.jpg  1280*720  ./img_0003.jpg  1280*720  ./img_0002.jpg  1280*720  ./img_0005.jpg  1280*720  ./launcher.png  385*632  ./textfield.png 402*329  ./foo2.png      202*229  


Solution:2

I've no idea how this performs but ImageMagick has an identify application that I've got to learn over the last ten minutes. Better than anything, it has a -format argument where you can do maths!

find -iname '*.jpg' -exec identify -format "%[fx:w*h] %i\n" {} \+ | sort -g  

Unfortunately, if you get above a certain size, you get scientific notation (eg 1.2*10^3) instead of an integer back from identify. Thankfully sort has a -g argument that will parse them out.

On the same files here, this version (not forking out to Perl) only take 60% of the time the Perl version does. That's not to say Perl is slow, but going into perl and subshelling back out is an unnecessary complication that's slowing things down (I think).

Mine still isn't ideal. Needing to parse numbers back to integers for sorting is fairly slow. Ideally you'd do this all internally in one language where the size remains as an integer throughout... But the above is the shortest, sharpest one-liner I can do.


Solution:3

Using find, file --mime-type, identify, awk and sort

Using file --mime-type we find all images, even without an extension.

The one-line at the end of this answer has a problem with newlines in the file name. Therefore here an other version:

find . -type f -exec sh -c 'file --brief --mime-type "$0" |\  grep -q ^image/ && identify -format "%[fx:w*h] %i\n" "$0"' {} \; |\  sort -g  

Sample output

26696 ./OWoHp.png  37975 ./hUXnc.png  47275 ./foo  bar.png  

This is the file with the newline in the filename:

47275 ./foo  bar.png  

Many thanks @terdon, @don_crissti and @glennjackman for the answers here.


Old version with a problem if the filename contains newlines

find . -type f -print0 | \      xargs -0 -I{} file --mime-type {} | \      awk -F$"\0" -F": " '/image/ {print $1}' | \      xargs -I{} identify -format "%[fx:w*h] %i\n" {} | \      sort -g  

Sample output

9216 ./.face  27918 ./Unbenannt.png  1.0368e+06 ./Images/Wallpapers/14-13.jpg  1.57292e+07 ./Images/Wallpapers/wallpaper_19.jpg  1.57292e+07 ./Images/Wallpapers/wallpaper_22.jpg  1.92e+06 ./Images/Wallpapers/black (10).jpg  2.304e+06 ./Images/Wallpapers/1920x1200.jpg  2.304e+06 ./Images/Wallpapers/1920x1200_Seria_Lunar-Melodies.png  2.304e+06 ./Images/Wallpapers/Anime_girl_129177.jpg  2.304e+06 ./Images/Wallpapers/wallpaper-1802661.jpg  2.304e+06 ./Images/Wallpapers/wallpaper-264390.jpg  3.14368e+06 ./Images/Wallpapers/14 - 2.jpg  3.6864e+06 ./Images/Wallpapers/14-1.jpg  4.096e+06 ./Images/Wallpapers/wallpaper-2850337.jpg  4.096e+06 ./Images/Wallpapers/wt4NRqA.jpg  7.0969e+06 ./Images/Wallpapers/14 - 1.png  


Solution:4

Late to the party but here's my version with find,exiftool,and sort:

find . -exec exiftool -S -t -q -FileName -ImageSize {} + | sort -k2n,2

It's a bit slow, but works. Sample output:

$ find Downloads -exec exiftool -S -t -q -FileName -ImageSize  {} + | sort -k2n,2     1433695350670.jpg   1600x1000  1433695350670.jpg   1600x1000  1434999385912.jpg   2212x3318  1434999385912.jpg   2212x3318  1433524966694.jpg   2688x1520  1433524966694.jpg   2688x1520  1433525301504.jpg   2688x1520  1433525301504.jpg   2688x1520  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »