Resize the specified image to the required dimension.


    @param image: a Python Image Library (PIL) image instance.

    @param canvas_size: requested size in pixels, as a 2-tuple
        ``(width, height)``.

    @param filter: indicate the filter to use when resizing the image.

    @param does_crop: indicate whether the image needs to be cropped.

    @param crop_alignment: if the image needs to be cropped, select which
       alignment to use when cropping.

    @param match_orientation: indicate whether the given canvas size
        should be inverted to match the orientation of the image.


    @return: a PIL image instance corresponding to the image that has been
        resized, and possibly cropped.

def resize_image(image, canvas_size,
        filter=Filter.NearestNeighbor,
        does_crop=False,
        crop_aligment=CropAlignment.center,
        crop_form=CropForm.rectangle,
        match_orientation=False):
    """
    Resize the specified image to the required dimension.


    @param image: a Python Image Library (PIL) image instance.

    @param canvas_size: requested size in pixels, as a 2-tuple
        ``(width, height)``.

    @param filter: indicate the filter to use when resizing the image.

    @param does_crop: indicate whether the image needs to be cropped.

    @param crop_alignment: if the image needs to be cropped, select which
       alignment to use when cropping.

    @param match_orientation: indicate whether the given canvas size
        should be inverted to match the orientation of the image.


    @return: a PIL image instance corresponding to the image that has been
        resized, and possibly cropped.
    """
    (source_width, source_height) = image.size
    source_aspect = source_width / float(source_height)

    (canvas_width, canvas_height) = canvas_size
    canvas_aspect = canvas_width / float(canvas_height)

    if match_orientation:
        if (source_aspect > 1.0 > canvas_aspect) or (source_aspect < 1.0 < canvas_aspect):
            (canvas_width, canvas_height) = (canvas_height, canvas_width)
            canvas_aspect = canvas_width / float(canvas_height)

    if does_crop:
        if source_aspect > canvas_aspect:
            destination_width = int(source_height * canvas_aspect)
            offset = 0 if crop_aligment == CropAlignment.left_or_top \
                else source_width - destination_width if crop_aligment == CropAlignment.right_or_bottom \
                else (source_width - destination_width) / 2
            box = (offset, 0, offset + destination_width, source_height)
        else:
            destination_height = int(source_width / canvas_aspect)
            offset = 0 if crop_aligment == CropAlignment.left_or_top \
                else source_height - destination_height if crop_aligment == CropAlignment.right_or_bottom \
                else (source_height - destination_height) / 2
            box = (0, offset, source_width, destination_height + offset)

    else:
        if canvas_aspect > source_aspect:
            # The canvas aspect is greater than the image aspect when the canvas's
            # width is greater than the image's width, in which case we need to
            # crop the left and right edges of the image.
            destination_width = int(canvas_aspect * source_height)
            offset = (source_width - destination_width) / 2
            box = (offset, 0, source_width - offset, source_height)
        else:
            # The image aspect is greater than the canvas aspect when the image's
            # width is greater than the canvas's width, in which case we need to
            # crop the top and bottom edges of the image.
            destination_height = int(source_width / canvas_aspect)
            offset = (source_height - destination_height) / 2
            box = (0, offset, source_width, source_height - offset)

    return image.crop(box).resize((canvas_width, canvas_height), PIL_FILTER_MAP[filter])