0

I am trying to write an automator app that will take image files and resize them down to a specified width, but maintain the height/width ratio of the original image file.

I have been trying to use sips in bash but I'm not sure where I am going wrong. I can't find anything on google to reference BASH or sips.

I'm trying to take the height and width of the passed image, figure out the ratio and then resize the image using the target width and the target height (Calculated from the target width and ratio)

This is my current shell script and I'm passing the image in as Pass input: as arguments.

 height=`sips --getProperty pixelHeight $@`
 width=`sips --getProperty pixelWidth $@`
 ratio=height/width
 targetWidth=262
 targetHeight=targetWidth*ratio

 sips --resampleHeightWidth targetHeight targetWidth $@

I'm not even sure if this is the right way to do it so any suggestions would be helpful.

Glenn Flanagan
  • 115
  • 1
  • 8
  • I think I've been a total idiot. I didn't realise there was a resampleWidth fucntion which effectively does what I need. sips --resampleWidth 262 $@ Sorry everyone. – Glenn Flanagan Jun 14 '13 at 09:58

1 Answers1

0

You can use --resampleWidth or -Z:

sips --resampleWidth 262 "$@" # make smaller or larger so that the width is 262 px
sips -Z 262 "$@" # make smaller or larger so that the longer sides are 262 px

If you want to prevent upscaling smaller images, see this answer, or use ImageMagick. I think sips often makes images look too blurry without additional sharpening, but ImageMagick also allows choosing different resampling filters:

mogrify -filter lanczos2 -resize '262>' "$@"

mogrify is a version of convert that modifies images in place. lanczos2 (2-lobed Lanczos) makes images slightly less sharp than lanczos (3-lobed Lanczos, which was made the default filter for downscaling at some point). 262> resizes images wider than 262 px so that their width is 262 px.

Community
  • 1
  • 1
Lri
  • 26,768
  • 8
  • 84
  • 82