MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat

Wed, Dec 16, 2015 11:27 pm

OS X sips command and image dimensions

On a Mac OS X system, you can use the file command to determine the width and height of an image in pixels.
$ file example.png
example.png: PNG image data, 69 x 91, 8-bit/color RGBA, non-interlaced

You can also use the sips command with the -g parameter followed by pixelWidth or pixelHeight.

$ sips -g pixelWidth example.png
  pixelWidth: 69
$ sips -g pixelHeight example.png
  pixelHeight: 91

Make sure you capitalize those as shown above; if you don't and use PixelWidth or PixelHeight, i.e., you use an uppercase "P" rather than a lowercase "p", the result will be "<nil>".

$ sips -g PixelWidth example.png
  PixelWidth: <nil>

If you only want to see the number, you can pipe the output of the command to the tail command, instructing that command to show only the last line of output, and then pipe the output of the tail command to the cut command, instructing that command to use the colon as the delimiter for separating items on the line and to show just the second field, which will be the number preceded by a space:

$ sips -g pixelWidth example.png | tail -n1 | cut -d":" -f2
$ sips -g pixelHeight example.png | tail -n1 | cut -d":" -f2

Or you can omit the tail command and just use cut by adding the -s argument to that command to suppress lines with no field delimiter character, i.e., lines with no colon in this case, assuming there will be none on the first line of output from the sips command.

$ sips -g pixelWidth example.png | cut -d":" -f2 -s
$ sips -g pixelHeight example.png | cut -d":" -f2 -s

If you don't want the space preceding the number, you can use a space character as the delimiter and specify f4 rather than f2, since there are two spaces output before "pixel" on the second line of ouput and a third one after the colon.

$ sips -g pixelWidth example.png | cut -d" " -f4 -s

If you wished to put that in a window that will be displayed to the user, you can use AppleScript.

set w to do shell script "sips -g pixelWidth /Users/jdoe/Documents/example.png | cut -d':' -f2 -s"
set h to do shell script "sips -g pixelHeight /Users/jdoe/Documents/example.png | cut -d':' -f2 -s"
display alert "Width:" & (w as text) & "
Height:" & (h as text)

Hit Enter after the double quote at the end of the second-to-last line to have a newline character appear in the ouput after the line with the width is displayed so that the height appears on a separate line. The ouput will be as shown below:

AppleScript display of sips output

[/os/os-x] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo