How to Convert and Compress Images in the Mac Terminal
My Mac takes crisp, detailed screenshots. These pngs can be >5 MB — much larger than the recommended image size <500 KB for the web. Ever since Adobe limited the options on their online image resizer, I’ve been looking for a new way to compress images for my websites. I don’t want to fuss with image editing software, and I’m wary of the online image compression tools with wait times and ads that look like phony submit buttons.
Then I remembered my old boot camp adage: do everything possible in the terminal. With a little googling, I learned about the
sips is an image processing library that can convert, rotate, and scale images in the terminal. This library is preinstalled and has been included with MacOS since 2003.
To preform a simple conversion, the script is simple:
Please note that the “ — out" flag begins with a double dash as such and is currently autoformatted by Medium as one character.
-s flag sets the options for
formatOptions . Since the goal is to get the image under 500KB while preserving visual quality, I am converting this image to a jpeg because it is a lossy compression format.
With this command, the image was reduced to 203 KB, but the quality isn’t up to par. The call to action button looks fuzzy around the edges.
Instead of using the
formatOptions, I tried a percentage to resize the image next.
The image is 333 KB, and the image is visibly more crisp.
Edit: When this story was first published, it displayed the scripts above as plain text, which auto-formatted the double dash to the double dash character. This has been fixed in the embedded gists.