# Path: aetros/utils/pilutil.py
# def imresize(arr, size, interp='bilinear', mode=None):
#     """
#     Resize an image.
# 
#     Parameters
#     ----------
#     arr : ndarray
#         The array of image to be resized.
# 
#     size : int, float or tuple
#         * int   - Percentage of current size.
#         * float - Fraction of current size.
#         * tuple - Size of the output image.
# 
#     interp : str, optional
#         Interpolation to use for re-sizing ('nearest', 'lanczos', 'bilinear', 'bicubic'
#         or 'cubic').
# 
#     mode : str, optional
#         The PIL image mode ('P', 'L', etc.) to convert `arr` before resizing.
# 
#     Returns
#     -------
#     imresize : ndarray
#         The resized array of image.
# 
#     See Also
#     --------
#     toimage : Implicitly used to convert `arr` according to `mode`.
#     scipy.ndimage.zoom : More generic implementation that does not use PIL.
# 
#     """
#     im = toimage(arr, mode=mode)
#     ts = type(size)
#     if issubdtype(ts, int):
#         percent = size / 100.0
#         size = tuple((array(im.size)*percent).astype(int))
#     elif issubdtype(type(size), float):
#         size = tuple((array(im.size)*size).astype(int))
#     else:
#         size = (size[1], size[0])
#     func = {'nearest': 0, 'lanczos': 1, 'bilinear': 2, 'bicubic': 3, 'cubic': 3}
#     imnew = im.resize(size, resample=func[interp])
#     return fromimage(imnew)


import math
import numpy as np
import PIL.Image
from six.moves import range
from aetros.utils.pilutil import imresize
    from cStringIO import StringIO
    from io import StringIO
    from PIL import Image, ImageDraw
        # chop off ends of dimension that is still too long
        if width_ratio > height_ratio:
            start = int(round((resize_width - width) / 2.0))
            return image[:, start:start + width]
        else:
            start = int(round((resize_height - height) / 2.0))
            return image[start:start + height, :]
    else:
        if resize_mode == 'fill':
            # resize to biggest of ratios (relatively smaller image), keeping aspect ratio
            if width_ratio > height_ratio:
                resize_width = width
                resize_height = int(round(image.shape[0] / width_ratio))
                if (height - resize_height) % 2 == 1:
                    resize_height += 1
            else:
                resize_height = height
                resize_width = int(round(image.shape[1] / height_ratio))
                if (width - resize_width) % 2 == 1:
                    resize_width += 1
            image = imresize(image, (resize_height, resize_width), interp=interp)
        elif resize_mode == 'half_crop':
            # resize to average ratio keeping aspect ratio
            new_ratio = (width_ratio + height_ratio) / 2.0
            resize_width = int(round(image.shape[1] / new_ratio))
            resize_height = int(round(image.shape[0] / new_ratio))
            if width_ratio > height_ratio and (height - resize_height) % 2 == 1:
                resize_height += 1
            elif width_ratio < height_ratio and (width - resize_width) % 2 == 1:
                resize_width += 1

# Path: aetros/utils/pilutil.py
# def imresize(arr, size, interp='bilinear', mode=None):
#     """
#     Resize an image.
# 
#     Parameters
#     ----------
#     arr : ndarray
#         The array of image to be resized.
# 
#     size : int, float or tuple
#         * int   - Percentage of current size.
#         * float - Fraction of current size.
#         * tuple - Size of the output image.
# 
#     interp : str, optional
#         Interpolation to use for re-sizing ('nearest', 'lanczos', 'bilinear', 'bicubic'
#         or 'cubic').
# 
#     mode : str, optional
#         The PIL image mode ('P', 'L', etc.) to convert `arr` before resizing.
# 
#     Returns
#     -------
#     imresize : ndarray
#         The resized array of image.
# 
#     See Also
#     --------
#     toimage : Implicitly used to convert `arr` according to `mode`.
#     scipy.ndimage.zoom : More generic implementation that does not use PIL.
# 
#     """
#     im = toimage(arr, mode=mode)
#     ts = type(size)
#     if issubdtype(ts, int):
#         percent = size / 100.0
#         size = tuple((array(im.size)*percent).astype(int))
#     elif issubdtype(type(size), float):
#         size = tuple((array(im.size)*size).astype(int))
#     else:
#         size = (size[1], size[0])
#     func = {'nearest': 0, 'lanczos': 1, 'bilinear': 2, 'bicubic': 3, 'cubic': 3}
#     imnew = im.resize(size, resample=func[interp])
#     return fromimage(imnew)


# Path: aetros/utils/image.py
import math
import numpy as np
import PIL.Image
from six.moves import range
from aetros.utils.pilutil import imresize
    from cStringIO import StringIO
    from io import StringIO
    from PIL import Image, ImageDraw

        # chop off ends of dimension that is still too long
        if width_ratio > height_ratio:
            start = int(round((resize_width - width) / 2.0))
            return image[:, start:start + width]
        else:
            start = int(round((resize_height - height) / 2.0))
            return image[start:start + height, :]
    else:
        if resize_mode == 'fill':
            # resize to biggest of ratios (relatively smaller image), keeping aspect ratio
            if width_ratio > height_ratio:
                resize_width = width
                resize_height = int(round(image.shape[0] / width_ratio))
                if (height - resize_height) % 2 == 1:
                    resize_height += 1
            else:
                resize_height = height
                resize_width = int(round(image.shape[1] / height_ratio))
                if (width - resize_width) % 2 == 1:
                    resize_width += 1
            image = imresize(image, (resize_height, resize_width), interp=interp)
        elif resize_mode == 'half_crop':
            # resize to average ratio keeping aspect ratio
            new_ratio = (width_ratio + height_ratio) / 2.0
            resize_width = int(round(image.shape[1] / new_ratio))
            resize_height = int(round(image.shape[0] / new_ratio))
            if width_ratio > height_ratio and (height - resize_height) % 2 == 1:
                resize_height += 1
            elif width_ratio < height_ratio and (width - resize_width) % 2 == 1:
                resize_width += 1

            image = imresize(image, (resize_height, resize_width), interp=interp)