UNIX Scripts Bash Tips: batch resizing images for a web site
Here’s a simple script I wrote to do batch resizing of images.
Scenario: you have A LOT of large pictures in one directory that need to be resized to be suitable for viewing on the web.
Start up a text editor in a terminal or on your console - nano, pico, mcedit, etc. - and save to a file, e.g. convert.sh
#!/bin/bash
for i in *.jpg
do
convert -resize 300 $i dir/$i
echo $i processed
done
Do ” chmod 700 convert.sh ” to make it executable and private for your viewing, editing and executing.
There are a number of assumptions here in this file, so you should edit it to suit your fancy.
1. you have ImageMagick installed. Hence, the “convert” command
2. the pictures’ filenames all end with .jpg
3. the resulting images will be 300 pixels wide, have the same aspect ratio and file name.
4. all the new images will go in a subdirectory called “dir”
5. all the original images will still be there.
6. the ” echo ” command lets you know when each file has been converted.
Now just ” mv ” or ” cp -a ” convert.sh into the directory with all the images that need to be converted. Next, ” ./convert.sh ” to run the program.
Naturally, if you run this a lot, you’ll probably want to put it in your $PATH.

Leave a Comment