ImageMagick command line: create smaller version of images

I wanted to create smaller versions for many large images in a single directory and

  • have a file name for the new file that is derived from the original file name (to be able to easily identify the image or link them to full size versions later)
  • keep the original aspect ration of the image (unlike many thumbnail programs)
  • never scale the images up (i.e., only change them if they are larger than target size)

It turned out it was easiest to do this on the command line, using the convert program from ImageMagick. Here is what I did:

for FILE in *; do BN=${FILE%.*}; TARGET="${BN}_smaller.jpg"; echo "Converting file: $FILE to: $TARGET"; convert "$FILE[800x>]" "$TARGET"; done

 

Here is a detailed step by step explanation of what this does:

for FILE in *; do                        # Loops over all files in directory
    BN=${FILE%.*};                       # Extract part of file name before the first dot
    TARGET="${BN}_smaller.jpg";          # Construct output file name (and define output format with file extension)
    echo "Converting file: $FILE to: $TARGET";     # Tells user what we are doing atm
    convert "$FILE[800x>]" "$TARGET";    # perform scaling to width of 800 pixels (only shrinks images, never scales them up)
done                                     # ends the loop

 

Hope you find this useful!

Advertisements

About dfspspirit

PhD student in bioinformatics, interested in photography, level design, digital image manipulation, architecture and, of course, bioinformatics.
This entry was posted in general, Photography and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s