Resizing images using the command line

If you happen to find yourself in the situation that you want to resize a large amount of images in OS X, you basically have two options:

  • Buy an App that does it for a couple of quid.
  • Use the tools that came with your OS.

If own a copy of Pixelmator you already have bought an App that’s capable of batch resizing images. Just take a look at Automator, Pixelmator added a few new actions to it.

But this is about using the tools Apple gave you, specifically sips.

scriptable image processing system

The man page states,

This tool is used to query or modify raster image files and ColorSync ICC profiles. Its functionality can also be used through the “Image Events” AppleScript suite.

This is exactly what we’re looking for and so much more. But let’s not get carried away and just focus on resizing images for now. In order to resize or rather to resample an image in a lower resolution you have to use the right parameter. Depending on your intentions, these are:

IMPORTANT: Using sips images files are altered, you cannot reverse the effects. It’s important to make copies of the original files if you want to keep them

  • -z <height in pixels> <width in pixel> this one alters the aspect ratio of your image if the specified size has a different ratio
  • -Z <max. heightwidth in pixels> this defines the maximum dimensions and if the original images don’t fit the specified value in either height or width, they will be resized to the point where they fit both. Keeps the aspect ratio.

In most cases -Zshould do the trick but in case you only want or only can provide either height or width you can use the following parameters, both respecting aspect ratio:

  • --resampleWidth <width in pixel>
  • --resampleHeight <height in pixels>

Examples

resizing an image of any size to 1024×768 (ignoring aspect ratio)
sips -z 768 1024 example.png
Note: Keep in mind the right order

resizing an image of 1423×1066 to fit in 1024×768 (retaining aspect ratio)
sips -Z 1024 example.png
*Note: Resulting image is 1024×759. As it is retaining the aspect ratio we only provide one pixel value, if you’re decreasing the size of the image use the bigger value, usually the width.

resiszing an image of 825×978 to a height of 489px (retaining aspect ratio)
sips --resampleHeight 489 example.png
Note: Resulting image is 413×489.

resizing all PNG-images in your working directory to 1024×768 (ignoring aspect ratio)
sips -z 768 1024 *.png

resizing any images in a specified folder to fit a width of 1024 (retaining aspect ratio)
sips --resmpleWidth 1024 /path/to/directory/*

You might also like

Comments are closed.