ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Chika on August 10, 2021, 08:57:19 AM

Title: How to sort images by aspect ratio
Post by: Chika on August 10, 2021, 08:57:19 AM
I want to sort images by aspect ratio, and I got some codes like this:
identify * | gawk '{split($3,sizes,"x"); print $1,sizes[1]/sizes[2]}' | sed 's/\[.\]//' | sort -gk 2
This is a output:
28.webp 0.698404
1.webp 0.699544
27.webp 0.706956
10.webp 0.707061
25.webp 0.707061
9.webp 0.707061
2.webp 0.707241
22.webp 1.41431
23.webp 1.41431
24.webp 1.41431


It works, and the first queue is exactly what I want, anyway, but identify is too slower than exiftool especially when handling WebP format, and exiftool have a -r option, so I want to use exiftool to get this output instead, but I don't know how to deal with the output of exiftool -r -s -ImageSize, anyone could help me?
Title: Re: How to sort images by aspect ratio
Post by: StarGeek on August 10, 2021, 12:03:11 PM
You can find an Aspect Ratio config file in this post (https://exiftool.org/forum/index.php?topic=7275.0) but it returns a ratio, e.g. 16:9, 4:3, 1:1, etc.

You can use this command to get a decimal number, but the api call would also affect other tags.
exiftool -config Exiftool_Aspect_Ratio.config -api "Filter=m/(\d+):(\d+)/;$_=$1/$2" -AspectRatio file.jpg

Example output:
C:\>exiftool -config Exiftool_Aspect_Ratio.config -s -api "Filter=m/(\d+):(\d+)/;$_=$1/$2" -AspectRatio -AspectRatio# y:/!temp/Test_All_QT_tags_x.mp4 "y:/!temp/test3 (2).jpg" y:/!temp/Test2.tiff
======== y:/!temp/Test_All_QT_tags_x.mp4
AspectRatio                     : 1.77777777777778
AspectRatio                     : 16:9
======== y:/!temp/test3 (2).jpg
AspectRatio                     : 1.33333333333333
AspectRatio                     : 4:3
======== y:/!temp/Test2.tiff
AspectRatio                     : 1
AspectRatio                     : 1:1



If you need a decimal output without affecting other tags, then I can work on that later today.
Title: Re: How to sort images by aspect ratio
Post by: Chika on August 10, 2021, 12:48:50 PM
Thanks for your replay. But all I need are only a sequence of files name, I don't care what the aspect ratio is.
Title: Re: How to sort images by aspect ratio
Post by: StarGeek on August 10, 2021, 02:10:46 PM
Try this then.  It will output in about the same format as your example image magick command and then you can just pass through to sort
exiftool -p '$filename ${ImageSize;m/(\d+)x(\d+)/;$_=$1/$2}' /path/to/files | sort -gk 2