Score:1

how to find all pictures with a certain aspect ratio in a directory and its subdirectories

jp flag

I need a method to find all pictures with a certain aspect ratio, for example 16:9 , such as 1920x1080 1280x720 944x531.

I found this page How to find all images with a certain pixel size using command line? but it only says how to find certain sizes not aspect ratios.

I use bash.

Marco avatar
br flag
Divide the sizes to get the aspect ratio? landscape: 16/9 = 1.77... and in portrait: 9/16=0.5625.
Usermaxn avatar
jp flag
how do I divide the sizes in command line and how do I pass the result of the check to grep? maybe I should make it a function and have it have a if statement
Score:2
br flag

You can modify the command from your link
How to find all images with a certain pixel size using command line?

This will print filenames with pictures approximatly landscape 16/9:

find . -iname "*.jpg" -type f -exec identify -format '%w %h %i\n' '{}' \; | awk '$1/$2>1.7 && $1/$2<1.8 {print $3}'

This will do the same with approximatly portrait 9/16:

find . -iname "*.jpg" -type f -exec identify -format '%w %h %i\n' '{}' \; | awk '$1/$2>0.5 && $1/$2<0.6 {print $3}'

Of course you can use the math in awk in more sophisticated way (for better reading only the awk part):

awk 'sqrt((($1/$2)-(16/9))^2)<sqrt(0.5^2) {print $3}'

As awk has no "abs" function, I just use created it using "^2" and "sqrt" to check if the ratio is inside a "delta" (in this example 0.5).

Usermaxn avatar
jp flag
this has a bug. the format has to be `-format '%w %h %i\n'`
Usermaxn avatar
jp flag
and also it only prints upto first space in the name. it works otherwise.
Usermaxn avatar
jp flag
a good fix for that is {print substr($0, index($0,$3)) }
Marco avatar
br flag
Thanks, updated it.
Usermaxn avatar
jp flag
even better way to print the name in full if it has spaces: `{$1=$2=""; print $0}` instead of `{print $3}`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.