Reducing image size with sips

On an Apple OS X system, you can reduce the size of an image by using the sips command with the -Z size parameter where size is the maximum value you want for either height or width. E.g., suppose I have a PNG file named system_preferences.png", which is 1,560 pixels wide by 1,604 high. The image height is greater than the image width and I want the maximum dimension to be 780 pixels. By opening a Teminal window - the Terminal utility is found in the Applications/Utilities directory - I can get a command line interface (CLI), aka a "shell prompt", that will allow me to enter the command sips -Z 780 system_preferences.png. The command will ensure that neither dimension will be greater than 780 pixels. In this case the height will be set to 780 pixels and the width will be adjusted to maintain the current aspect ratio, so the new dimensions will be 758 pixels wide and 780 pixels high.

In the example below, the image file has been produced using the screencapture command. Since the sips command will alter the original image to match my new specifications, if I want to maintain the original file, I need to make a copy of it first. You can check the dimensions of PNG files using the file command.

Save on a Computer: Run Windows, Mac, and Linux with VirtualBox
Save on a Computer: Run Windows,
Mac, and Linux with VirtualBox
1x1 px

$ screencapture -T 10 -w system_preferences.png
$ file system_preferences.png 
system_preferences.png: PNG image data, 1560 x 1604, 8-bit/color RGBA, non-interlaced
$ cp system_preferences.png system_preferences_original.png
$ sips -Z 780 system_preferences.png
/Users/jasmith1/Documents/system_preferences.png
  /Users/jasmith1/Documents/system_preferences.png
$ file system_preferences.png 
system_preferences.png: PNG image data, 758 x 780, 8-bit/color RGBA, non-interlaced
$

The options for adjusting image size with sips are shown below:

    -z pixelsH pixelsW
     --resampleHeightWidth pixelsH pixelsW
           Resample image at specified size. Image apsect ratio may be
           altered.

     --resampleWidth pixelsW
           Resample image to specified width.

     --resampleHeight pixelsH
           Resample image to specified height.

     -Z pixelsWH
     --resampleHeightWidthMax pixelsWH
           Resample image so height and width aren't greater than specified
           size.

So, if I wanted the width to be 780 pixels, i.e., half its original value, rather than the maximum of either dimension to be 780 pixels, I could use a command such as sips --resampleWidth 780 system_preferences_original.png .

$ file system_preferences.png
system_preferences.png: PNG image data, 1560 x 1604, 8-bit/color RGBA, non-interlaced
$ sips --resampleWidth 780 system_preferences.png
/Users/jasmith1/Documents/blog/sshserver/system_preferences.png
  /Users/jasmith1/Documents/blog/sshserver/system_preferences.png
$ file system_preferences.png
system_preferences.png: PNG image data, 780 x 802, 8-bit/color RGBA, non-interlaced
$

By maintaining the aspect ratio, the image still looks reasonably clear if I reduce the original image down to 1/4 of the original width with sips --resampleWidth 390 system_preferences.png. For comparison, I've included links to the image at the various sizes below. The first dimension is the width. The difference that the size adjustments make to file size are also listed in kilobytes.

1560x1604 401K (original)
758x780 190K
780x802 201K
390x401 75K

References:

  1. Determining Image Info Under Mac OS X
    Date: October 4, 2011
    MoonPoint Support
  2. OS X sips command and image dimensions
    Date: December 16, 2015
    MoonPoint Support