ó
 ‰\c           @   s‘   d  d g Z  d d l Z d d l m Z d d l m Z d d l m Z m	 Z	 d d d „ Z d d d	 „ Z d
 „  Z d d „ Z d d „ Z d S(   t   imreadt   imsaveiÿÿÿÿN(   t   string_types(   t   Imagei   (   t   img_as_ubytet   img_as_uintc      
   K   sx   t  |  t ƒ rO t |  d ƒ , } t j | ƒ } t | d | d | ƒSWd QXn% t j |  ƒ } t | d | d | ƒSd S(   sž  Load an image from file.

    Parameters
    ----------
    fname : str or file
       File name or file-like-object.
    dtype : numpy dtype object or string specifier
       Specifies data type of array elements.
    img_num : int, optional
       Specifies which image to read in a file with multiple images
       (zero-indexed).
    kwargs : keyword pairs, optional
        Addition keyword arguments to pass through.

    Notes
    -----
    Files are read using the Python Imaging Libary.
    See PIL docs [1]_ for a list of supported formats.

    References
    ----------
    .. [1] http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html
    t   rbt   dtypet   img_numN(   t
   isinstanceR   t   openR   t   pil_to_ndarray(   t   fnameR   R   t   kwargst   ft   im(    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyR    
   s    c         C   s9  y |  j  ƒ  d WnG t k
 r[ } d } t | ƒ } d |  j | | f } t | ƒ ‚ n Xg  } d } d }	 xJy |  j |	 ƒ Wn t k
 r– Pn X|  }
 | d k	 rÓ | |	 k rÓ |  j  ƒ  d |	 d 7}	 qq n  |  j d k r|  j	 d k r| d k rd } n  |  j	 d k rŠ| d k r0t
 |  ƒ } n  | rH|  j d	 ƒ }
 qí|  j d k rxd
 |  j k rx|  j d ƒ }
 qí|  j d ƒ }
 nc |  j	 d k r«|  j d	 ƒ }
 nB d |  j	 k rÌ|  j d ƒ }
 n! |  j	 d k rí|  j d ƒ }
 n  |  j	 j d ƒ r{|  j } |  j	 j d ƒ r d n d } d |  j	 k rJ| j d d ƒ } n  t j |
 j ƒ  | ƒ }
 | d d d … |
 _ n t j |
 d | ƒ}
 | j |
 ƒ |	 d 7}	 | d k	 rq Pqq qq Wt |  d ƒ rã|  j rã|  j j ƒ  n  | d k rt | ƒ d k rt j | ƒ S| r| d S| r5t d | ƒ ‚ n  d S(   sq   Import a PIL Image object to an ndarray, in memory.

    Parameters
    ----------
    Refer to ``imread``.

    i    sL   http://pillow.readthedocs.org/en/latest/installation.html#external-librariessA   Could not load "%s" 
Reason: "%s"
Please see documentation at: %si   t   PNGt   It   uint16t   Pt   Lt   transparencyt   RGBAt   RGBt   1t   At   CMYKs   I;16t   Bs   >u2s   <u2t   St   ut   iNiÿÿÿÿR   t   fps   Could not find image  #%s(   t   getdatat   IOErrort   strt   filenamet
   ValueErrort   Nonet   seekt   EOFErrort   formatt   modet   _palette_is_grayscalet   convertt   infot
   startswitht   sizet   endswitht   replacet   npt
   fromstringt   tobytest   shapet   arrayt   appendt   hasattrR   t   closet   lent
   IndexError(   t   imageR   R   t   et   sitet   pillow_error_messaget   error_messaget   framest	   grayscaleR   t   frameR4   (    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyR   +   sr    
*		
c         C   so   |  j  d k s t ‚ t j |  j ƒ  ƒ j d ƒ } |  j ƒ  \ } } | | | d !} t j t j | ƒ d ƒ S(   s  Return True if PIL image in palette mode is grayscale.

    Parameters
    ----------
    pil_image : PIL image
        PIL Image that is in Palette mode.

    Returns
    -------
    is_grayscale : bool
        True if all colors in image palette are gray.
    R   i   i   i   i    (   i   i   (	   R)   t   AssertionErrorR1   t   asarrayt
   getpalettet   reshapet
   getextremat   allcloset   diff(   t	   pil_imaget   palettet   startt   stopt   valid_palette(    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyR*   ‚   s
    c         C   sØ  |  j  d k r= t |  ƒ }  i d d 6d d 6|  j d } n£ | d k rÈ d } d	 } |  j j d
 k rv t |  ƒ }  qà |  j ƒ  d k  r¹ |  j ƒ  d k r¹ |  j t	 j
 ƒ }  d } } qà t |  ƒ }  n t |  ƒ }  d } d } y |  j ƒ  } Wn t k
 r|  j ƒ  } n X|  j  d k rxt j | |  j j ƒ } y | j | d | ƒ WqÔt k
 rt| j | d | ƒ qÔXn\ |  j d |  j d f } y t j | | | ƒ } Wn& t k
 rÓt j | | | ƒ } n X| S(   s`   Export an ndarray to a PIL object.

    Parameters
    ----------
    Refer to ``imsave``.

    i   R   R   i   i   t   pngR   s   I;16R   R   i   i    R   t   rawi   (   RO   R   (   t   ndimR   R4   R   t   kindR   t   maxt   mint   astypeR1   t   uint8R3   t   AttributeErrort   tostringR   t   newt   Tt	   frombytesR2   (   t   arrt
   format_strR)   t	   mode_baset   array_bufferR   t   image_shape(    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyt   ndarray_to_pilš   s>    "$c         K   s  t  |  t ƒ r% | d k r% d } n  t  |  t ƒ rR |  j ƒ  j d ƒ rR d } n  t j | ƒ } | j j d k rˆ | j	 t j
 ƒ } n  | j d k r° t d | j f ƒ ‚ n  | j d k rä | j d d k rä t d ƒ ‚ qä n  t | d	 | ƒ} | j |  d
 | | d S(   s:  Save an image to disk.

    Parameters
    ----------
    fname : str or file-like object
        Name of destination file.
    arr : ndarray of uint8 or float
        Array (image) to save.  Arrays of data-type uint8 should have
        values in [0, 255], whereas floating-point arrays must be
        in [0, 1].
    format_str: str
        Format to save as, this is defaulted to PNG if using a file-like
        object; this will be derived from the extension if fname is a string
    kwargs: dict
        Keyword arguments to the Pillow save function (or tifffile save
        function, for Tiff files). These are format dependent. For example,
        Pillow's JPEG save function supports an integer ``quality`` argument
        with values in [1, 95], while TIFFFile supports a ``compress``
        integer argument with values in [0, 9].

    Notes
    -----
    Use the Python Imaging Libary.
    See PIL docs [1]_ for a list of other supported formats.
    All images besides single channel PNGs are converted using `img_as_uint8`.
    Single Channel PNGs have the following behavior:
    - Integer values in [0, 255] and Boolean types -> img_as_uint8
    - Floating point and other integers -> img_as_uint16

    References
    ----------
    .. [1] http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html
    R   s   .pngt   bi   i   s!   Invalid shape for image array: %si   s*   Invalid number of channels in image array.R]   R(   N(   i   i   (   i   i   (   R	   R   R%   t   lowerR/   R1   t
   asanyarrayR   RR   RU   RV   RQ   R$   R4   Ra   t   save(   R   R\   R]   R   t   img(    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyR   Í   s    #		(   t   __all__t   numpyR1   t   sixR   t   PILR   t   utilR   R   R%   R    R   R*   Ra   R   (    (    (    s=   lib/python2.7/site-packages/skimage/io/_plugins/pil_plugin.pyt   <module>   s   !W	3