Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some code that I've been using to resize images in Mac OS X via the command prompt (and/or quicksilver), to a maximum width or height of 500px. The only problem is that if the original image is smaller than 500px width or height, sips will upscale it. I would want it to just skip these images.

How can I make that happen?

This is my code:

#!/bin/bash

for ARG in "$@"
    do
        sips -Z 500 -s format jpeg "$ARG" --out "${ARG%.*}.jpg"
done
share|improve this question
up vote 3 down vote accepted

Process the output of sips --getProperty pixelHeight filename.ext and sips --getProperty pixelWidth filename.ext and use it in a condition, only performing the resize operation if desired.


#!/bin/bash
height=`sips --getProperty pixelHeight url.png | sed -E "s/.*pixelHeight: ([0-9]+)/\1/g" | tail -1`
width=`sips --getProperty pixelWidth url.png | sed -E "s/.*pixelWidth: ([0-9]+)/\1/g" | tail -1`

if [[ $height -gt 500 || $width -gt 500 ]]; then
    growlnotify -m "large file needs reducing"
fi

You can do the rest on your own.

share|improve this answer
    
It doesn't seem to be possible using sips alone. – Daniel Beck Feb 3 '11 at 12:49
    
Thanks! Could you give an example of how to do it with the conditional statement? – cwd Feb 4 '11 at 17:35
    
@cwd See edited post – Daniel Beck Feb 4 '11 at 18:13
    
Thanks so much for inserting the regex to extract the actual size.. saved me a lot of frustration! – Ben Clayton Apr 19 '12 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .