Bulkresizing images

One of my biggest pet peeves with the modern era is that despite how far we’ve come, we still don’t have an easy way of sharing large files with people. Sure you can use dropbox, ftp sites, cds, usbs and the like, but why can’t we have an email-style application that transfers the large files?

The reason this irks me is because I like sharing pictures with friends and family, but hate the idea of putting them on facebook or some other public site where they are visible to everyone. What happens if I want to share pictures that my online friends would bawk at? Or pictures of an embarrassing night out with someone I trust? The problem is that sending 20 pictures over email, each ~5 MB in size really, well, sucks.

So I wrote a script to help with that.

What it does is resize all my pictures to 1/9 their original size, taking a 6 MB picture down to a few hundred KB in size. Now emailing 20 pictures is now problem at all. It wouldn’t look so good printed on the receiver’s end, but 99% of the time the receiver just looks at the picture for a few seconds, clicks to the next, and then deletes the email after clicking past the final picture.

The script creates two folders, one for the original pictures so I can edit, print, or modify the originals later, and a folder for the resized images. Sure, storing two sets of the pictures takes up more space, something that 10 years ago would have been cause for pause (remember how much a 12 GB disk cost in the mid nineties? $350!)

Anyways, on with the script. In its current incarnation, it is run from within the directory where the pictures you want to resize are. After each resize operation, it appends the filename so you know it was resized, moves the original and resized to folders of the same name, then moves on to the next picture until it runs out of pictures in the current directory.


shopt -s nullglob

if [[ ! -d original/ ]]
 mkdir original/

if [[ ! -d resized/ ]]
 mkdir resized/

for file in *.jpg *.JPG *.jpeg *.JPEG
 convert "$file" -resize 33% "${file%.*}.resized.jpg"
 mv -f "$file" original/
 mv -f "${file%.*}.resized.jpg" resized/

A few notes:

  • The ‘shopt -s nullglob’ option isn’t strictly necessary, but prevents warnings if the glob does not match a file
  • You need to have imagemagick installed, which comes with the convert tool, which among other things, can resize images
  • If you want different size images spit out at the end, simply change the “33%” parameter to you option of choice. Remember that that percentage applies to both the horizontal and vertical dimensions, so 50% is 1/4 the original size, 33% is 1/9 the original size, and 25% is 1/16 the original size.

If you don’t like the whole ‘for . . .’ construct, you can you the ‘find’ command in its place as shown below.

find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | while read -d $'\0' file; do convert "$file" -resize 33% "${file%.*}.resized.jpg"; mv "$file" original/; mv "${file%.*}.resized.jpg" resized/; done

And that’s it. Bulk resize your vacation photos, your drunken work party photos, or other embarrassing pictures and email away!

Tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam