OS X: Getting Image-Dimensions on the Command-Line (sips)

If you need to get the dimensions of an image-file via the command-line, let’s say in a script or an AppleScript, then there is a way to do so easily. Enter the scriptable image processing system or short sips. You didn’t know you got that installed on your OS X system alreay? Me neither.

Assume, that we need to get the dimensions of an image for further handling of said image. The command

sips -g pixelWidth -g pixelHeight [image-file]

returns the values (and a lot more if asked correctly).

Give it a try and take a look at the man-page (man sips) for further information. A really handy tool for all scripters.

3 Comments

  1. Posted March 9, 2008 at 11:01 am | Permalink

    Hi Erik,

    I just wanted to add, that AppleScript has its own built-in sips-based scripting addition named Image Events.

    Image Events is basically just a wrapper for sips. So when scripting in AppleScript, there is often no need to use sips via the «do shell script» command, you can just gain informations or manipulate images with Image Events.

    Best regards from Germany!

  2. Erik
    Posted March 28, 2008 at 10:43 am | Permalink

    Hi Martin,
    thanks for the info. I wasn’t aware that “Image Events” is using sips.

    BTW: Sorry for answering so late, I’ve been very busy the last couple of weeks.

  3. Jamie
    Posted February 17, 2010 at 5:07 pm | Permalink

    sips is very useful but also frustratingly not quite powerful enough at times.

    1. When getting image size info it’s often useful to have the data in a spreadsheet to highlight images below a certain size. Ideally sips would therefore output “filename \t width \t height”, i.e. one line per file, with each piece of information separated by tabs.

    At the moment I’ve directed the output to a file (e.g. sips -g pixelWidth -g pixelHeight [image-file] > results.txt) then opened in TextWrangler and done some find and replace operations:
    a) Find pixelWidth, plus the line break and spaces before and the space after. Replace with \t (in TextEdit press alt and tab)
    b) Do the same for the pixelHeight.

    You could probably do this find and replace on the command line. This example gives an idea, although it does not work because sed doesn’t recognise the \n and \t characters.
    sips -g pixelWidth -g pixelHeight /path/to/folder/* | sed -E “/\n( pixelWidth: | pixelHeight: )/s//\t/” > /path/to/textfile.txt

    2. The –resampleHeightWidthMax option is useful to make an image fit within a certain size such as 160 pixels. But if you want to fit the image within a rectangular shape such as 120×160 you can’t. Using both –resampleWidth and –resampleHeight options together gives some odd results, although doesn’t seem to stretch the picture even though the man page suggests it will.

    3. It doesn’t work with EPS files. You need to use pstopdf first.

    4. The pad images option always centres the image within the padded area. There’s no way to change this.

    Many of sips’ features are accessible via Automator, too, such as the Pad Images command. If you need to use the command line sips tool via Automator, add a Run Shell Script action that takes input ‘as arguments’ and does something like this:

    for f in “$@”
    do
    sips –addIcon –setProperty format tiff –setProperty formatOptions best –setProperty formatOptions lzw –resampleHeightWidthMax 160 “$f” –out “`dirname “$f”`/`basename “$f”|cut -d ‘.’ -f1`”.tiff
    done

    This takes the current item Automator is looking at, makes a TIFF no bigger than 160×160 and outputs it with the same filename as the original but with .tiff on the end.

Post a Comment

Your email is never published nor shared. Required fields are marked *, comments are moderated.

*
*