5

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
4

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.

  • 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
0

This reads the width and height of the image in one command

#!/bin/bash
size=($(sips -g pixelWidth -g pixelHeight $theFile | grep -o '[0-9]*$'))

if [[ ${size[0]} -gt 500 || ${size[1]} -gt 500 ]]; then
    growlnotify -m "large file needs reducing"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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