Process images by aspect ratio

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Process images by aspect ratio Thu Nov 07, 2019 1:47 am • by zkarj
I see there is an Orientation value which will provide "landscape" or "portrait" values, but how do I detect an image that is (or is close to) a square?

Specifically, I wish to apply one rule to images that are square (or close to square) and another rule to images which are not. I have pixel width and pixel height but I don't know if I can combine those in a ratio somehow? If I could do that, then a ratio between, say 0.9 and 1.1 could be considered square and anything else not square.

Can I provide some sort of script to provide this value for Hazel to check, or do I need to build a script that decides which command to use and just have Hazel feed it all images?
zkarj
 
Posts: 9
Joined: Thu Nov 22, 2018 4:50 pm

Re: Process images by aspect ratio Thu Nov 07, 2019 10:45 am • by Mr_Noodle
You can write an AppleScript for use in the conditions. You can pass in the width and height and the script can say whether or not it matches or not.
Mr_Noodle
Site Admin
 
Posts: 11870
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Process images by aspect ratio Fri Nov 08, 2019 3:47 am • by zkarj
Thanks, I did eventually find the "passes shell script" and managed to figure out a bash script to get the job done. This uses an ImageMagick command (identify) to get the dimensions. As the goal of the rule was to run an ImageMagick conversion it seemed appropriate.

# Exit = 0 if close to square
width=$(/usr/local/bin/identify -format "%w" "$1")> /dev/null
height=$(/usr/local/bin/identify -format "%h" "$1")> /dev/null
ratio=$( echo "scale=1; $width/$height" | bc )
if [ $ratio -lt 0.9 ] || [ $ratio -gt 1.1 ]
then
exit 1
else
exit 0
fi

To get the opposing rule, I repeated the script but reversed the exit 1 and exit 0.
zkarj
 
Posts: 9
Joined: Thu Nov 22, 2018 4:50 pm


Return to Support