Coding

Python: Resizing Images (Part 1)

Sometimes you have a buttload of images that you need to resize, but are unwilling to spend the time to do so manually. What to do? Write a script to automate it! Suppose you’d like to resize them by a certain proportion, here’s a code snippet that you can follow:

from PIL import Image
import glob

def resizeImages(percentage):
    imageFiles = glob.glob('*.JPG')
    resizedFolder = 'resized/'
    counter = 1
    totalCount = len(imageFiles)

    for imagePath in imageFiles:
        print 'Resizing ' + imagePath + ' (' + str(counter) + ' of ' + str(totalCount) + ')...'
        image = Image.open(imagePath)
        image = image.resize([int(percentage / 100.0 * s) for s in image.size], Image.ANTIALIAS)
        image.save(resizedFolder + imagePath)
        counter += 1

    print 'Resizing complete.'

# Calling resizeImages to half the dimensions of the images
resizeImages(50)

I am currently using Python v2.7 on Mac OS X 10.9.5.



Leave a Reply

Your email address will not be published. Required fields are marked *