# Original Author: giuamato 
# Original URL: http://python-examples.pythonblogs.com/31_python_examples/archive/65_python-resize_images_with_pil.html
#
# Modified by: matt@blackcodeseo.com
# Date: March 22, 2010
import Image
import os

def resize(file, width=None, height=None, quality=100):
    path,ext=os.path.splitext(file)
    name=os.path.basename(path)
    try:
        image=Image.open(file)
    except Exception, e:
        print e.msg
        return False
    w,h=image.size
    if width==None and height:
        w=height*w/h
        h=height
    if height==None and width:
        h=width*h/w
        w=width
    if width and height:
        w=width
        h=height
    try:
        image=image.resize((w,h),Image.ANTIALIAS).save(file,image.format,quality=quality)
    except Exception, e:
        print e.msg
        return False
    return True
