you can use photoshop to automate your image processing by creating an action and then running it as a batch or saving it as a droplet. it’s a really powerful way to get lots of repetitive work done quickly. but you probably already knew that.
it’s also possible to do basic image processing without photoshop or whatever image editor you use. this post will show how to resize images with applescript. the advantages? you don’t tie up photoshop for what is essentially monkey-work, you don’t get those flickering windows while photoshop opens and closes the files it’s processing, and this way is usually faster. interested now?
this method uses an application called “image events” which comes standard on the mac and is only accessible through applescript. here’s how you would scale an image selected in the finder. caution: this version of the script overwrites the original file — only use it on a duplicate.
copy this into your script editor (found in applications > applescript) :
tell application "Finder"
set mgFile to selection
end tell
set mgPath to POSIX path of (mgFile as text)
tell application "Image Events"
set mgImage to open mgPath as text
scale mgImage to size 500
save mgImage
close mgImage
end tell
notice that the file goes through the same process in image events as it would in photoshop : open > scale > save > close. the scale command works on the longest edge — so, if your image is portrait, it will now be 500 pixels high; if it is landscape it will now be, you guessed it, 500 pixels wide.
ok, but what if you don’t want to overwrite your original? here’s one way to save the scaled version as a duplicate. this script will create a folder called “500px” for the scaled copy, leaving the original untouched :
tell application "Finder"
set mgFile to selection
end tell
set mgFile to mgFile as alias
set mgPath to POSIX path of (mgFile as text)
tell application "Finder"
set mgFolder to container of mgFile as alias
set mgName to name of mgFile
set mg500folder to "500px"
if (exists folder mg500folder of folder mgFolder) is false then
make folder at mgFolder with properties {name:mg500folder}
end if
set mg500path to (mgFolder as text) & mg500folder & ":" & mgName
end tell
tell application "Image Events"
set mgImage to open mgPath as text
scale mgImage to size 500
save mgImage in mg500path
close mgImage
end tell
this script first creates the folder if it does not already exist and then sets a new filepath to that folder for the final image (“set mg500path to …”). in image events we use the command “save mgImage in mg500path” rather than simply “save mgImage”. if you add “with icon” to the end of that line, your image will have a tiny preview as its icon instead of a generic icon.
alrighty, hopefully that’s all pretty straightforward — now let’s get a little complex. this next version of the script will scale a whole bunch of images to the same height. remember, image events scales the longest edge — so how would you scale a landscape image to 500 pixels high? :
global newHeight
tell application "Finder"
set mgFiles to selection
end tell
repeat with mgFile in mgFiles
set mgFile to mgFile as alias
set mgPath to POSIX path of (mgFile as text)
tell application "Finder"
set mgFolder to container of mgFile as alias
set mgName to name of mgFile
set mg500folder to "500px"
if (exists folder mg500folder of folder mgFolder) is false then
make folder at mgFolder with properties {name:mg500folder}
end if
set mg500path to (mgFolder as text) & mg500folder & ":" & mgName
end tell
tell application "Image Events"
set mgImage to open mgPath as text
copy dimensions of mgImage to {Wdth, Hght}
my processData(Wdth, Hght)
scale mgImage to size newHeight
save mgImage in mg500path
close mgImage
end tell
end repeat
on processData(Wdth, Hght)
set mgComparison to Wdth - Hght
if mgComparison > 0 then -- image is landscape
set mgCalculation to Wdth / Hght
set newHeight to round (500 * mgCalculation)
else --image is square or portrait
set newHeight to 500
end if
end processData
you can see here, we use image events to get the dimensions of the image, then we use a subroutine to do a comparison to work out if the image is landscape or portrait. if the image is landscape we divide the width by the height to get a proportion by which to multiply the scale factor (500). no doubt you can work out how to change that subroutine if you wanted to scale all images to 500 pixels wide instead.
please note that this method is not always 100% accurate — you will sometimes get an image only 499 pixels high.
this workflow was developed to process hundreds of product images for a webshop. however, there’s still one step missing — ensuring all the images are rgb — but that’s going to have to wait until the next post.