Scaling images in batch mode
I have lots of folders with images uploaded from digital cameras. Average image size is 3MB which is way too large for uploading to web based image sites. What I need to do is to rescale the images but doing them one at a time is a pain so I wrote a little script to do them one folder at a time.
The script makes use of a program called ‘convert’. This is part of the ImageMagick suite of tools.
Install ImageMagick
Once installed try to verify the location of the convert program
convert is /usr/bin/convert
The way the script works is to create a new folder called ‘resized’ under the folder you want to process. It then places rescaled versions of the images it finds in the source folder.
The scaling is performed by passing a width x height parameter to the convert program. The question is how do you know what value to provide without screwing up the ratio of your image. What I do is open a sample image in Gimp. Go to the ‘Image|Scale’ menu option. I pick a value for the width, hit tab and it provides you with the correct value for the height. This is fine if all your images are of the same size. You’ll have to put in a bit of work if they are different sizes.
Here’s the script..it’s really simple as you can see, but I had an itch and I had to scratch it.
convert_command=/usr/bin/convert
#get the folder name to be procesed
if [ $# -ne 2 ]
then
echo "Error in $0 - Invalid number of arguments."
echo "Syntax: $0 <folder name> <new size>"
echo "Example $0 ./images 1024x680"
exit 1
fi
folder=$1
newsize=$2
resized="resized"
resized_dir="$1/$resized"
echo "Resized images will be placed into the folder $resized"
if [ ! -d $resized_dir ]
then
mkdir $resized_dir
fi
cd $folder
for file in $( ls . )
do
if [ -f $file ]
then
echo "Processing $file..."
convert -resize $newsize $file "$resized/$file"
fi
done
Save the script as image_resize.sh. Do a chmod a+x so you can execute it. Then create a sample folder with a few images copied just for testing. Run it as follows:
That’s all folks!


No Responses to “Scaling images in batch mode”
Leave a Reply