Python Image Processing and PIL

I wrote the following code (based on code provided in the PIL documentation) to halve the size of all jpeg images in a folder and save as a new file. Not the fastest code, but it worked pretty well.

from PIL import Image

import glob, os

newPath = raw_input("folder to process:")
os.chdir(newPath)
for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
print file
x = int(im.size[0]*0.5)
y = int(im.size[1]*0.5)
size = x,y
im.resize(size, Image.ANTIALIAS)
im.save(file + "_small_" + ".jpg")
Advertisements
Tagged ,

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

%d bloggers like this: