The church web site I run has a box the displays random pictures from our photo album. The formatting of the page is easiest if these pictures are no wider than 530 pixels and no higher than 340 pixels. Of course most pictures are taken with a camera these days are a couple of thousand pixels wide, so each picture needs to be resized. You can do that by hand, of course, but I found it easiest to write a script. If you're running Linux, this uses the ImageMagick package, particularly the identify and convert programs. On a Mac, it uses sips, which has been available at least since 10.3. I suppose it could be modified for Windows or run under Cygwin, but I don't use Windows enough to make it worth my time.
Save the file as websize, make it executable, and save it to a directory in your path. Then run it with the command
websize [list of image files]
This will take a file named, say, picture1.jpg, and create a new file named picture1_web.jpg that fits into the box defined by MAXWIDTH and MAXHEIGHT. If the picture already fits into the box it will duplicate the file.
#! /bin/bash
# Resizes a picture or pictures to make sure it fits into the UPB
# frontpage box of MAXWIDTH pixels wide by MAXHEIGHT pixels tall,
# keeping the same format (PNG, JPEG, BMP, etc.)
# usage
# websize [pictures]
# If a picture is named "picturename.ext", the shrunken picture
# will be named "picturename_web.ext".
# If a picture already fits into the website, it will simply be
# copied with its new name. This makes it easier to find the
# pictures that should be posted on the web.
# Define the maximum dimensions of the picture
let MAXWIDTH=530
let MAXHEIGHT=340
# For Linux machines we need "convert" from the ImageMagick package.
# We'll assume it's in the standard location:
# For Macs the standard program is sips, at least since 10.3
if [ -f /usr/bin/sips ]
then
OS="Mac"
elif [ -f /usr/bin/convert ]
then
OS="Linux"
else
echo "Cannot find a program to do the file conversion"
exit 1
fi
for PICTURE
do
if [ "$OS" = "Linux" ]
then
# identify is also part of the ImageMagick package
let WIDTH=`identify $PICTURE | sed "s/x/ /" | awk '{print $3}'`
else
let WIDTH=`sips -g pixelWidth $PICTURE | tail -1 | awk '{print $2}'`
fi
# Need new picture name:
HEADER=${PICTURE%.*}
EXT=${PICTURE##*.}
NEWPICT=${HEADER}_web.$EXT
echo Converting $PICTURE to $NEWPICT
# Is the picture too wide?
if (( $WIDTH > $MAXWIDTH ))
then
if [ "$OS" = "Linux" ]
then
convert -resize ${MAXWIDTH}x $PICTURE $NEWPICT
else
sips --resampleWidth $MAXWIDTH $PICTURE --out $NEWPICT
fi
else
cp $PICTURE $NEWPICT
fi
# Note that the new picture may still be too large:
if [ "$OS" = "Linux" ]
then
let HEIGHT=`identify $NEWPICT | sed "s/x/ /" | awk '{print $4}'`
else
let HEIGHT=`sips -g pixelHeight $NEWPICT | tail -1 | awk '{print $2}'`
fi
if (( $HEIGHT > $MAXHEIGHT ))
then
# echo Resizing height of $PICTURE
if [ "$OS" = "Linux" ]
then
convert -resize x$MAXHEIGHT $NEWPICT 1_${NEWPICT}
else
sips --resampleHeight $MAXHEIGHT $NEWPICT --out 1_$NEWPICT
fi
mv 1_${NEWPICT} $NEWPICT
fi
done
The obvious modification is to allow MAXWIDTH and MAXHEIGHT to be read from the command line, maybe using -w and -h flags. That's for another day.

0 comments:
Post a Comment