# 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
            elif image.ndim == 3 and image.shape[2] == 3:
                channels = 3
            else:
                raise ValueError('invalid image shape: %s' % (image.shape,))
        elif channels == 1:
            if image.ndim != 2:
                if image.ndim == 3 and image.shape[2] == 3:
                    # color to grayscale
                    image = np.dot(image, [0.299, 0.587, 0.114]).astype(np.uint8)
                else:
                    raise ValueError('invalid image shape: %s' % (image.shape,))
        elif channels == 3:
            if image.ndim == 2:
                # grayscale to color
                image = np.repeat(image, 3).reshape(image.shape + (3,))
            elif image.shape[2] != 3:
                raise ValueError('invalid image shape: %s' % (image.shape,))
    else:
        raise ValueError('resize_image() expected a PIL.Image.Image or a numpy.ndarray')

    # No need to resize
    if image.shape[0] == height and image.shape[1] == width:
        return image

    # Resize
    interp = 'bilinear'

    width_ratio = float(image.shape[1]) / width
    height_ratio = float(image.shape[0]) / height
    if resize_mode == 'squash' or width_ratio == height_ratio:

# 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

            elif image.ndim == 3 and image.shape[2] == 3:
                channels = 3
            else:
                raise ValueError('invalid image shape: %s' % (image.shape,))
        elif channels == 1:
            if image.ndim != 2:
                if image.ndim == 3 and image.shape[2] == 3:
                    # color to grayscale
                    image = np.dot(image, [0.299, 0.587, 0.114]).astype(np.uint8)
                else:
                    raise ValueError('invalid image shape: %s' % (image.shape,))
        elif channels == 3:
            if image.ndim == 2:
                # grayscale to color
                image = np.repeat(image, 3).reshape(image.shape + (3,))
            elif image.shape[2] != 3:
                raise ValueError('invalid image shape: %s' % (image.shape,))
    else:
        raise ValueError('resize_image() expected a PIL.Image.Image or a numpy.ndarray')

    # No need to resize
    if image.shape[0] == height and image.shape[1] == width:
        return image

    # Resize
    interp = 'bilinear'

    width_ratio = float(image.shape[1]) / width
    height_ratio = float(image.shape[0]) / height
    if resize_mode == 'squash' or width_ratio == height_ratio:

        return imresize(image, (height, width), interp=interp)