Skip to content

Instantly share code, notes, and snippets.

@cstrouse
Created October 22, 2012 19:14
Show Gist options
  • Select an option

  • Save cstrouse/3933471 to your computer and use it in GitHub Desktop.

Select an option

Save cstrouse/3933471 to your computer and use it in GitHub Desktop.
Resizes images so that the shortest side is 250px
#!/bin/bash
# change this to the path where you have the images stored
src_path='/Users/tzayad/Pictures/test'; cd $src_path
# change this to the length you want for the smallest side
t_size=250;
# for each image of the type jpeg/jpg/gif/png loop through and resize
for img in `find . -iname \*.jpeg -o -iname \*.jpg -o -iname \*.png -o -iname \*.gif`; do
# grab the height and width
h=`sips -g pixelHeight $img | tail -1 | sed "s/.* //"`;
w=`sips -g pixelWidth $img | tail -1 | sed "s/.* //"`;
# decide which is the shortest side to get the factor to scale by
if [[ $w -lt $h ]]; then
scale=`echo "(($h*$t_size)/$w) + 1" | bc -l | xargs printf "%1.0f"`;
else
scale=`echo "(($w*$t_size)/$h) + 1" | bc -l | xargs printf "%1.0f"`;
fi
# scale the image
sips -Z $scale $img
done
echo "\n ***** RESIZING DONE *****";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment