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
link|improve this question

feedback

1 Answer

up vote 2 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.

link|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 at 16:24
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.