Automated image processing on Mac OS X
If you’ve ever had to convert a few hundred images from tiff to png, create thumbnails of them you’ll know how time consuming and boring it can be. It can also be surprisingly error prone as your mind drifts off halfway through to think about what you’re up to at the weekend. Computers were supposed to relieve us of this kind of drudgery, right?
Luckily, every Mac has a powerful system lurking under the hood called ‘sips’ (Scriptable Image Processing System). You can use it to do all sorts of interesting things to raster images (eg. jpeg, tiffs, png etc) without ever having to fire up Photoshop. In this article I’m going to show you some simple uses of sips, and how to roll them up into a handy Automator workflow.
A quick into to sips
First, lets fire up the Terminal. It’s just an app that lets us work on the command line and you’ll find it in /Applications/Utilities. Type the following at the prompt:
sips -s format png
…next, drag any image file other than a png file into the Terminal. You should now have something in your Terminal that looks like this:
sips -s format png /path/to/your/file.jpg
That tells sips that we want to change the format of the image we dropped to a png. All we need to do now is to tell it where to put the resulting file. We can do this by adding –out followed by the location of the file we want to be created, so add that into your Terminal like this:
sips -s format png /path/to/your/file.jpg --out /path/to/your/file.png
Note that for the output file path, I just copied and pasted the input file path and changed the extension to “png”. Alternatively, I could have used something like “~/Desktop/file.png” to save the file to my desktop, or I could have used any other path that suits my purpose. Now we have a full sips command, so hit return to execute the command and a file should be created at the location you specified with the –out parameter.
The sips command we just created is a fairly typical one. Note that the word ‘sips’ is followed by some parameters (in our case, “-s format png”), then the path to the source file, then we used –out to tell sips where to put the resulting file. One thing to remember is that having your input file path the same as the output file path is generally a Bad Thing.
There are many other things you can do with sips, including resizing images, changing resolution, applying ICC profiles and so on. The manual (aka man page) is available by typing “man sips” in the Terminal or, if you prefer, click here to view it online.
Using SIPS from Automator
There’s nothing wrong with using SIPS as shown above, but the Terminal isn’t particularly user friendly. It’d be better if we could just select a load of files in the Finder and process them, right? That’s what we’ll look at now. First, launch Automator (you’ll find it in your Applications folder). Create a new workflow, being sure to select “Service” as shown below.
Next, set the “Service receives selected” popup menu to “image files” and select “Finder.app”. This means that our workflow will be available when we select images in the Finder.
Next, type “shell script” into the search field and drag the “Run Shell Script” action into the area under the popups we just set. Change the “Pass input” drop down to “as arguments”. You workflow should now look like this:
Next, replace the default text “cat” with the following:
for theFilePath in "$@"; do
done
In case you’re not familiar with shell scripting, this is a simple loop. When we execute the Run Shell Script action, it will be passed a list of files called “$@”. What we’ve written is a script to loop through this list. The loop starts at “do” and ends at “done”. Every time the loop repeats, the variable “theFilePath” will be the next file name in the list. Your workflow should now look like this:
At the moment, our script doesn’t do anything because there is nothing between “do” and “done”. We need to add some sips magic! In between “do” and “done” add the following:
sips -s format png "$theFilePath" --out "${theFilePath%%.*}.png"
Note that I’m using the theFilePath variable as the input. The confusing stuff as the output just knocks the file extension off the theFilePath variable and adds “.png”. Your workflow should now look like this:
Next, save your workflow and give it a name. I chose “Convert Images”.
Okay! Now for the fun bit. Find one or more images (not a PNGs!) in your Finder. Click the file whilst holding “ctrl” down and scroll down to Services, then select your workflow.
Your selected files will be converted magically into PNG files! NB. If nothing happens then check you selected “Pass input: as arguments” in the Run Shell Script action.
If I want to create multiple images, then it’s fine to add extra sips commands. So if I want to create a thumbnail image then I can change my shell script to:
for theFilePath in "$@"; do
sips -s format png "$theFilePath" --out "${theFilePath%%.*}.png"
sips -s format png -Z 100 "$theFilePath" --out "${theFilePath%%.*}_thumbnail.png"
done
…see the sips man page to see how the -Z option works!
Hope that gives some you ideas and saves you some time!