The Robservatory

Robservations on everything…

 

Use sips to quickly, easily—and freely—convert image files

Quite often, I find myself with a number of images (screenshots, typically) that I’ll want to convert from one format to another. If you search the Mac App Store, there are probably 300 apps that will let you do this; many are probably free. You could also use Automator, which has some good image conversion abilities, but can’t (for example) specify the quality of a JPEG conversion.

But the best way I’ve ever found is to use a tool that’s been included with every copy of macOS since the release of Mac OS X 10.3 (Panther) in October of 2003: A command line tool called sips. Yes, it requires using Terminal, but it’s quite easy to use. sips can modify one file, or any number of files, converting from one format to another. You can also use sips to resize images, rotate images, and more.

Basic usage of sips is straightforward. (The following is written for Terminal neophytes, so apologies for any over-explaining). Assume you have an image named Beach party.tiff that you’d like to convert into a smaller JPEG, but with a relatively high quality setting. Here’s how you’d do it using sips:

  1. Open Terminal, in Applications > Utilities.
  2. Type cd, then press the Space Bar, then drag in the folder that contains the image(s) to be converted. (Alternatively, you can use this tip to directly open the selected Finder folder in Terminal.)
  3. Type this, then press Return: sips -s format jpeg -s formatOptions 80 "Beach party.tiff" --out "Beach party.jpg"

When you press Return, sips will convert your image file—and it’s really fast, even on larger files. The formatOptions item lets you set the quality of the JPEG in either percentage (as I used), or you can use words: low, normal, high, or best. Hopefully obviously, you specify the new filename after the --out string.

Note that the filename is enclosed in quotes. Those quotes are required, otherwise any spaces in your filenames will cause the command to break.

The real power of sips isn’t in converting one file, though; it’s in batch converting many files. Here’s how to do that…

Batch conversions are essentially the same as single-file conversions, but it’s a bit trickier due to how Terminal processes multiple files. Here’s a real-world example: I have a folder of 24 large (1.5MB to 2MB each) PNG files that I want to convert to JPEGs while keeping the original files intact. Once I’m in the proper directory (steps one and two in the above process), I need to use a loop to read all the PNG files, and convert each one with sips. That loop command looks like this:

for i in *.png; do sips -s format jpeg -s formatOptions 70 "${i}" --out "${i%png}jpg"; done

This looks a bit scary, but it’s not too bad, given that there’s very little that needs to change when you use it: The only bits that you need to modify are:

  • for i in *.png; – This is the loop, and in my case, it’s looking for files with the png extension. Change to match the type of the “source” images you want to modify.
  • -s format jpeg – This sets the format for the new file to JPEG. Format can be any of jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga.
  • -s formatOptions 70 – This sets the JPEG quality to 70%. If the chosen image format doesn’t support a quality setting (i.e. PNG), you can just leave this off.
  • "${i%png}jpg" – This bit of magic removes the old file extension (png) and adds the new one (jpg). If you convert from .tiff to .png, you’d want it to read "${i%tiff}png"

sips is really fast, too—much faster than it’d be if I had to open an app to use its batch convert feature. Here’s how quickly it converted 24 PNGs totalling 40MB in size:

That took only 1.332 seconds for all 24 files; just launching a batch image editor would take roughly that long. Speed is good!

I recommend you play around with sips and copies of your images to see what it can do. I’d also normally recommend reading the sips man page (man sips), but it’s really not all that helpful.

Instead, try searching the web for examples of how to put sips to use. You can start with some examples on my old macosxhints.com site. This page explains how you can use sips to resize and constrain images to a bounding box. Finally, this one discusses rotation and flipping.

I’m sure there are GUI apps built on top of sips, such that you wouldn’t have to touch Terminal. But for me, it’s easy enough to switch in, do what I want, and switch out that I haven’t bothered looking for a GUI solution.

3 Comments

Add a Comment
      1. Before I actually do anything with find, I have “print” as the only action so I can make sure that only the files I want are acted on.

Leave a Reply

The Robservatory © 2017 Built from the Frontier theme