Batch .jpg Image Resizerby dave on 2007-06-11
Last night I was looking all over for a batch image resizer. I needed to make a folder full of images smaller and didn't want to open Gimp for each one. I downloaded 2 that weren't what I wanted and just as I was about to get really frustrated I realized, hey, I've taken two semesters of image processing. I'd been wanting to play around with the Python Image Library (PIL) for a while and took this as my example problem. It turned out to be real easy given their thumbnail example.
I modified it to instead input a max width and max height that you wanted the batch of images to be converted to. It then goes through and shrinks them to within the dimensions. Here's a copy of their thumnails example and my resize source:
#! /usr/bin/env python #A Batch Image Resizer #By Dave Fowler 2007: firstname.lastname@example.org """Image Resizer""" from PIL import Image import glob, os, sys resize_folder = 'resized' maxW = float(sys.argv) maxH = float(sys.argv) for infile in glob.glob("*.jpg"): file, ext = os.path.splitext(infile) if not os.path.exists( os.path.abspath(resize_folder) ): os.makedirs( os.path.abspath(resize_folder) ) im = Image.open(infile) size = im.size if maxW/size < maxH/size: newsize = (int(maxW), int(maxW/size*size)) else: newsize = (int(maxH/size*size), int(maxH)) im = im.resize(newsize, Image.ANTIALIAS) im.save(resize_folder + '/' + infile, "JPEG")
To run it you'll need python and PIL. Sorry, I haven't made an executable version yet. Place the python file into the directory you want to resize or somewhere in your PATH and in the image folder use
$ python resize.py <max Width> <max Height>
A new directory will be made inside the folder called resized and will contain the resized versions of your image. It currently only works for images ending in '.jpg'. Let me know if you make it fancier.