Last modified: 29/12/2011
Have you ever send some pictures to your friends by email or posted them somewhere on the web? I guess you did!
If these images are of high resolution they will take a lot of bandwidth.
So normally it’s preferable to first downsample those images.
ImageMagick is a very handy commandline tool to do the trick.
I show you here one small example how to downsample your images in batch to 20% with a Bash script:
for img in `ls *.JPG`
convert -sample 20% $img lowres_$img
echo "$img converted"
For those not familiar with Bash I will explain this script line by line.
This is a special line, not a comment line.
A comment line starts with a
A line starting with
#! tells the program loader of a Unix-like operating system to interpret this line as an interpreter directive. In this case saying “the following lines should be executed by Bash”.
This line starts a loop over the list of JPG files.
ls *.JPG is the command that you normally use on your shell to list all the filenames ending with “.JPG”. Be aware that in Bash this command is between `` (backticks).
Every iteration of the loop saves the current filename of the list in the variable
Line 4 and 7:
do done construction is the scope of what has to be done in one iteration of the loop.
This is actually one of the many ImageMagick commands.
Here we downsample the JPG file to 20%.
The value of the variable
img is called by prefixing the variable wit a $ sign.
The inputfile is not overwritten. We gave the output file the prefix
myfile.JPG will be converted to
This is an informational line echoing text to the console saying which image is converted.
Cheers and a happy New Year!