Thursday, August 5, 2010

How to resize photos with Bash shell script

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:


#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).jpg; done

Save the document and give it a sensible name to recognize it later on.

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”

Examples Scripts:


# 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

6 comments:

  1. Thanks for the script, it helpt a lot
    Greatings smaans

    ReplyDelete
  2. Hi, awesome!

    One question: how can i run the script from one folder to affect some other folder? I mean, i dont want to keep copying the script to every folder I need to resize, i want to have the script in one folder say /home/alex and run it from there every time.

    How to do that?
    Alex

    ReplyDelete
    Replies
    1. Try to modify the shell script for your needs. Happy Hacking!

      Delete
  3. it is actually very easy. You can go in the directory where you want to modify the files and run the script with a realtive reference.
    EG the script is in ~ and the files are in a directory like ~/pics
    $/home/user cd pics/
    $/home/user/pics ../script.sh

    ReplyDelete
  4. Really handy and easy to manipulate script... thanks! =)

    ReplyDelete

You can leave a comment here using your Google account, OpenID or as an anonymous user.

Popular Posts