Ivo Kaspar wrote this guide to resize multiple images using simple Bash shell script. It uses the convert program from ImageMagick, the swiss-army knife of command line image processing software. The original guide is edited and rewritten in some places.
You'll find this guide interesting if you want to resize a lot of pictures to upload to your website or email them. First you need to install the 'imagemagick' package if it is not installed on your computer. To install imagemagick, copy this line into your terminal application and confirm with your password. Close the terminal after it finished the installation.
sudo apt-get install imagemagick
Open the text editor and paste these lines into it:
Save the document and give it a sensible name to recognize it later on.
#!/bin/bash for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).jpg; done
Note that this script will work only with files that has uppercase '.JPG' extension and renames them to lower case '.jpg'. Some digital cameras follow this kind of file naming schema. You may like to change this to suit your needs.According to its manual page ImageMagick can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF and DPX.
If you want to define the size of the picture replace the "10%" with the amount of pixels you want it to have, 400x600 is a usable resolution. Of course it is also possible to keep the relative scale with any value instead of the “10” followed by "%" postfix. To change the file format to something other than “.JPG” you have to change the last “.JPG” into the format of your choice.
If you want to rename the pictures, you have to add something behind the "(basename $i .JPG)" and the ".JPG". I usually add an "-s" (for small) to indicate the reduced size of the pictures.
When you run the script it will proceed to resize all the pictures in the current folder, but not in any sub folders. Keep in mind that:
- Small pictures are small forever, you can't revert them.
- The filenames must not contain any space
- The filename must match to the first two “.JPG” or “.jpg”
# capital .JPG files, 10% size, no rename, it will overwrite the old pictures #!/bin/bash for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).JPG; done --- # lower case .jpg files, 10% size, renamed to capital .JPG (with ubuntu it's a different file, not with windows!) #!/bin/bash for i in *.jpg; do convert $i -resize 10% $(basename $i .jpg ).JPG; done --- # capital .JPG files, 10% size, adds an -s in the name of the new ones, keeps the old ones #!/bin/bash for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG)-s.JPG; done --- # capital .JPG files, 600x400 size, no rename, it will overwrite the old pictures #!/bin/bash for i in *.JPG; do convert $i -resize 600x400 $(basename $i .JPG).JPG; done