Page 1 of 1

Process images by aspect ratio

PostPosted: 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?

Re: Process images by aspect ratio

PostPosted: 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.

Re: Process images by aspect ratio

PostPosted: 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.