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:

  1. #!/bin/bash
  3. for img in `ls *.JPG`
  4. do
  5.   convert -sample 20% $img lowres_$img
  6.   echo "$img converted"
  7. done

For those not familiar with Bash I will explain this script line by line.

Line 1:
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”.

Line 3:
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 img.

Line 4 and 7:
The do done construction is the scope of what has to be done in one iteration of the loop.

Line 5:
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 lowres_.
So myfile.JPG will be converted to lowres_myfile.JPG.

Line 6:
This is an informational line echoing text to the console saying which image is converted.

Cheers and a happy New Year!

Rating 3.00 out of 5