menpo/image/base.py
def as_PILImage(self):
        r"""
        Return a PIL copy of the image. Depending on the image data type,
        different operations are performed:

        ========= ===========================================
        dtype     Processing
        ========= ===========================================
        uint8     No processing, directly converted to PIL
        bool      Scale by 255, convert to uint8
        float32   Scale by 255, convert to uint8
        float64   Scale by 255, convert to uint8
        OTHER     Raise ValueError
        ========= ===========================================

        Image must only have 1 or 3 channels and be 2 dimensional.
        Non `uint8` images must be in the rage ``[0, 1]`` to be converted.

        Returns
        -------
        pil_image : `PILImage`
            PIL copy of image

        Raises
        ------
        ValueError
            If image is not 2D and 1 channel or 3 channels.
        ValueError
            If pixels data type is not `float32`, `float64`, `bool` or `uint8`
        ValueError
            If pixels data type is `float32` or `float64` and the pixel
            range is outside of ``[0, 1]``
        """
        if self.n_dims != 2 or self.n_channels not in [1, 3]:
            raise ValueError(
                'Can only convert greyscale or RGB 2D images. '
                'Received a {} channel {}D image.'.format(self.n_channels,
                                                          self.n_dims))

        # Slice off the channel for greyscale images
        pixels = self.pixels[..., 0] if self.n_channels == 1 else self.pixels
        if pixels.dtype in [np.float64, np.float32, np.bool]:  # Type check
            if np.any((self.pixels < 0) | (self.pixels > 1)):  # Range check
                raise ValueError('Pixel values are outside the range '
                                 '[0, 1] - ({}, {}).'.format(self.pixels.min(),
                                                             self.pixels.max()))
            else:
                pixels = (pixels * 255).astype(np.uint8)
        if pixels.dtype != np.uint8:
            raise ValueError('Unexpected data type - {}.'.format(pixels.dtype))
        return PILImage.fromarray(pixels)
r"""
        Return a PIL copy of the image. Depending on the image data type,
        different operations are performed:

        ========= ===========================================
        dtype     Processing
        ========= ===========================================
        uint8     No processing, directly converted to PIL
        bool      Scale by 255, convert to uint8
        float32   Scale by 255, convert to uint8
        float64   Scale by 255, convert to uint8
        OTHER     Raise ValueError
        ========= ===========================================

        Image must only have 1 or 3 channels and be 2 dimensional.
        Non `uint8` images must be in the rage ``[0, 1]`` to be converted.

        Returns
        -------
        pil_image : `PILImage`
            PIL copy of image

        Raises
        ------
        ValueError
            If image is not 2D and 1 channel or 3 channels.
        ValueError
            If pixels data type is not `float32`, `float64`, `bool` or `uint8`
        ValueError
            If pixels data type is `float32` or `float64` and the pixel
            range is outside of ``[0, 1]``
        
r"""
Return a PIL copy of the image. Depending on the image data type,
different operations are performed.

dtype     Processing

uint8     No processing, directly converted to PIL
bool      Scale by 255, convert to uint8
float32   Scale by 255, convert to uint8
float64   Scale by 255, convert to uint8
OTHER     Raise ValueError

Image must only have 1 or 3 channels and be 2 dimensional.

Returns

pil_image : `PILImage`
PIL copy of image

Raises

ValueError
If image is not 2D and 1 channel or 3 channels.
r " " " Return a PIL copy of the image . Depending on the image data type different operations are performed . dtype Processing uint8 No processing directly converted to PIL bool Scale by 255 convert to uint8 float32 Scale by 255 convert to uint8 float64 Scale by 255 convert to uint8 OTHER Raise ValueError Image must only have 1 or 3 channels and be 2 dimensional . Returns pil_image : ` PILImage ` PIL copy of image Raises ValueError If image is not 2D and 1 channel or 3 channels .
def as_PILImage(self):
        if self.n_dims != 2 or self.n_channels not in [1, 3]:
            raise ValueError(
                'Can only convert greyscale or RGB 2D images. '
                'Received a {} channel {}D image.'.format(self.n_channels,
                                                          self.n_dims))
        pixels = self.pixels[..., 0] if self.n_channels == 1 else self.pixels
        if pixels.dtype in [np.float64, np.float32, np.bool]:  
            if np.any((self.pixels < 0) | (self.pixels > 1)):  
                raise ValueError('Pixel values are outside the range '
                                 '[0, 1] - ({}, {}).'.format(self.pixels.min(),
                                                             self.pixels.max()))
            else:
                pixels = (pixels * 255).astype(np.uint8)
        if pixels.dtype != np.uint8:
            raise ValueError('Unexpected data type - {}.'.format(pixels.dtype))
        return PILImage.fromarray(pixels)
def as_PILImage ( self ) : if self . n_dims != 2 or self . n_channels not in [ 1 , 3 ] : raise ValueError ( 'Can only convert greyscale or RGB 2D images. ' 'Received a {} channel {}D image.' . format ( self . n_channels , self . n_dims ) ) pixels = self . pixels [ ... , 0 ] if self . n_channels == 1 else self . pixels if pixels . dtype in [ np . float64 , np . float32 , np . bool ] : if np . any ( ( self . pixels < 0 ) | ( self . pixels > 1 ) ) : raise ValueError ( 'Pixel values are outside the range ' '[0, 1] - ({}, {}).' . format ( self . pixels . min ( ) , self . pixels . max ( ) ) ) else : pixels = ( pixels * 255 ) . astype ( np . uint8 ) if pixels . dtype != np . uint8 : raise ValueError ( 'Unexpected data type - {}.' . format ( pixels . dtype ) ) return PILImage . fromarray ( pixels )
r"""
Return a PIL copy of the image.
r " " " Return a PIL copy of the image .
r"""
        Return a PIL copy of the image. Depending on the image data type,
        different operations are performed:

        ========= ===========================================
        dtype     Processing
        ========= ===========================================
        uint8     No processing, directly converted to PIL
        bool      Scale by 255, convert to uint8
        float32   Scale by 255, convert to uint8
        float64   Scale by 255, convert to uint8
        OTHER     Raise ValueError
        ========= ===========================================

        Image must only have 1 or 3 channels and be 2 dimensional.
        Non `uint8` images must be in the rage ``[0, 1]`` to be converted.

        Returns
        -------
        pil_image : `PILImage`
            PIL copy of image

        Raises
        ------
        ValueError
            If image is not 2D and 1 channel or 3 channels.
        ValueError
            If pixels data type is not `float32`, `float64`, `bool` or `uint8`
        ValueError
            If pixels data type is `float32` or `float64` and the pixel
            range is outside of ``[0, 1]``
        """ # Slice off the channel for greyscale images # Type check # Range check
self