MoonPoint Support Logo

 

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



Advanced Search
December
Sun Mon Tue Wed Thu Fri Sat
   
16
   
2015
Months
Dec


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
/Users/jdoe/Documents/example.png
  pixelWidth: 69
$ sips -g pixelHeight example.png
/Users/jdoe/Documents/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
/Users/jdoe/Documents/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
 69
$ sips -g pixelHeight example.png | tail -n1 | cut -d":" -f2
 91

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
 69
$ sips -g pixelHeight example.png | cut -d":" -f2 -s
 91

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
69

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

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo