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: 11193
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

Re: Process images by aspect ratio Mon Aug 16, 2021 12:24 pm • by deevinityplayfair
its not working for me, help please..

I also found this, only I don't know nothing about scripting:


try
tell application "Finder" to set the this_folder to (folder of the front window) as alias
on error
set the this_folder to path to desktop folder as alias
end try
tell application "Finder"
set selected_items to selection
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
repeat with aFile in selected_items

set img_ to quoted form of POSIX path of (aFile as text)

if the name extension of aFile is "JPG" then
set my_dimensions to (do shell script " sips -g pixelWidth " & img_)
set width_ to text item 2 of my_dimensions
set my_dimensions to (do shell script " sips -g pixelHeight " & img_)
set height_ to text item 2 of my_dimensions
if (width_ = height_) then
do shell script "mv " & img_ & " " & img_ & ".SQ.JPG"
end if
end if
end repeat
set AppleScript's text item delimiters to oldDelims
end tell
deevinityplayfair
 
Posts: 8
Joined: Thu Feb 14, 2019 2:23 pm


Return to Support