'Resizes Image if it\'s over the maximum dimension'

def _image_resize(self, filepath):

    filepath = filepath.replace(settings.MEDIA_URL, '')
    filepath = (settings.MEDIA_ROOT + filepath)
    try:
        img = Image.open(filepath)
        expected_size = getattr(settings, 'HARDTREE_IMAGE_MAX_SIZE', [400, 300])
        if ((img.size[0] > expected_size[0]) or (img.size[1] > expected_size[1])):
            filter_name = getattr(settings, 'HARDTREE_IMAGE_RESIZE_FILTER', 'ANTIALIAS')
            filter = getattr(Image, filter_name, Image.ANTIALIAS)
            aspect = (img.size[0] / float(img.size[1]))
            newsize = list(expected_size)
            if (img.size[0] > expected_size[0]):
                newsize[0] = expected_size[0]
                newsize[1] = round((newsize[0] / aspect))
            if (newsize[1] > expected_size[1]):
                newsize[1] = expected_size[1]
                newsize[0] = round((newsize[1] * aspect))
            img = img.resize(newsize, filter)
            img.save(filepath)
    except Exception:
        pass
