
;c]c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z m Z d d l m Z m Z e j e  Z d	 Z d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d   Z d   Z d   Z d d d d d  Z d e d  Z d d l  m! Z! m" Z" d Z# e$ d e d e d e! d e" d e  Z% d   Z& e&   d S(   s+    Plugin that wraps the the Pillow library.
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formatt   image_as_uinti   (   t   pillow_formatst   pillow_docss  
    Parameters for reading
    ----------------------
    
    pilmode : str
        From the Pillow documentation:
        
        * 'L' (8-bit pixels, grayscale)
        * 'P' (8-bit pixels, mapped to any other mode using a color palette)
        * 'RGB' (3x8-bit pixels, true color)
        * 'RGBA' (4x8-bit pixels, true color with transparency mask)
        * 'CMYK' (4x8-bit pixels, color separation)
        * 'YCbCr' (3x8-bit pixels, color video format)
        * 'I' (32-bit signed integer pixels)
        * 'F' (32-bit floating point pixels)
        
        PIL also provides limited support for a few special modes, including
        'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
        (true color with premultiplied alpha).
        
        When translating a color image to grayscale (mode 'L', 'I' or 'F'),
        the library uses the ITU-R 601-2 luma transform::
        
            L = R * 299/1000 + G * 587/1000 + B * 114/1000
    as_gray : bool
        If True, the image is converted using mode 'F'. When `mode` is
        not None and `as_gray` is True, the image is first converted
        according to `mode`, and the result is then "flattened" using
        mode 'F'.
t   PillowFormatc           B` s   e  Z d  Z e Z d Z d Z d Z d   Z	 e
 d    Z d   Z d   Z d   Z d e j f d	     YZ d
 e j f d     YZ RS(   s/   
    Base format class for Pillow formats.
    t   it    c         O` s,   t  t |   j | |   t j   |  _ d  S(   N(   t   superR   t   __init__t	   threadingt   RLockt   _lock(   t   selft   argst   kwargs(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR   E   s    c         C` s   |  j  S(   s    The PIL plugin id.
        (   t
   _plugin_id(   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt	   plugin_idJ   s    c         C` s   |  j   |  j sb t |  _ d d  l } t | d  sF t d   n  d d l m } | |  _ n |  j d  k r t	 d   n  |  j } Wd  QX|  j
 d k r | j   n
 | j   | S(   Ni    t   __version__s/   Imageio Pillow plugin requires Pillow, not PIL!(   t   Images*   Imageio Pillow plugin requires Pillow lib.t   PNGt   JPEGt   BMPt   GIFt   PPM(   R   R   R   R   R   (   R   t   _pillow_importedt   Truet   PILt   hasattrt   ImportErrorR   t   _Imaget   Nonet   RuntimeErrorR   t   preinitt   init(   R   R   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _init_pillowP   s     
		
c         C` s}   |  j    } | j d |  j d k ry |  j | j k ry | j |  j \ } } | rv | j rs | | j  rs t Sqv qy n  d  S(   Ni   t   ?(   R&   t   modet   modesR   t   OPENt
   firstbytesR   (   R   t   requestR   t   factoryt   accept(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt	   _can_readg   s    c         C` sX   |  j    } | j d |  j d k rT | j |  j k rT |  j | j k rQ t SqT n  d  S(   Ni   R'   (   R&   R(   R)   t	   extensiont
   extensionsR   t   SAVER   (   R   R,   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt
   _can_writep   s
    t   Readerc           B` sM   e  Z d e d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   c         C` sP  |  j  j   } y | j |  j  j \ } } Wn' t k
 rU t d |  j  j   n X|  j   |  _ | |  j d  |  _	 t
 | d  r | j |  j	 j  n  |  j	 j r |  j	 j j r |  j	 j j |  j	 j _ n  t |  j	  t d | d t |  j	   |  _ | d  k	 r| |  j d <n  d |  _ t
 |  j	 d  rL|  j	 j |  _ n  d  S(	   Ns   Format %s cannot read images.R
   t   _decompression_bomb_checkt   as_grayt   is_grayR(   i   t   n_frames(   t   formatR&   R*   R   t   KeyErrorR#   t   namet	   _get_filet   _fpt   _imR   R5   t   sizet   palettet   dirtyt   rawmodet   rawmode_savedt   pil_try_readt   dictt   _palette_is_grayscalet   _kwargsR"   t   _lengthR8   (   R   t   pilmodeR6   R   R-   R.   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _openx   s&    	c         C` s   t  |  _ |  j j   S(   N(   t   Falset
   _we_own_fpR,   t   get_file(   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR<      s    	c         C` s*   t  |  j  |  j r& |  j j   n  d  S(   N(   t   save_pillow_closeR>   RL   R=   t   close(   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _close   s    	c         C` s   |  j  S(   N(   RH   (   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _get_length   s    c         C` s<   y |  j  j |  Wn! t k
 r7 t d |   n Xd  S(   Ns   Could not seek to index %i(   R>   t   seekt   EOFErrort
   IndexError(   R   t   index(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _seek   s    c         C` s   | |  j  k r+ t d | |  j  f   n  |  j j   } | | k rV |  j |  n* x' | | k  r | d 7} |  j |  qY W|  j j r |  j j j r |  j j j |  j j _ n  |  j j	   d t
 |  j |  j  } | |  j j f S(   Ns   Image index %i > %ii   i    (   RH   RT   R>   t   tellRV   R@   RA   RB   RC   t   getdatat   pil_get_frameRG   t   info(   R   RU   R	   t   im(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt	   _get_data   s    
c         C` s.   | d  k p | d k s$ t    n  |  j j S(   Ni    (   R"   RT   R>   RZ   (   R   RU   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _get_meta_data   s    N(   t   __name__t
   __module__R"   RK   RJ   R<   RP   RQ   RV   R\   R]   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR4   w   s   					t   Writerc           B` s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C` s{   |  j  j   } y | j |  j  j |  _ Wn' t k
 rR t d |  j  j   n X|  j j	   |  _
 i  |  _ t |  _ d  S(   Ns   Format %s cannot write images.(   R9   R&   R2   R   t
   _save_funcR:   R#   R;   R,   RM   R=   t   _metaRK   t   _written(   R   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ      s    	c         C` s   d  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRP      s    c         C` s   |  j  r" t d |  j j   n  | j d k rf | j d d k rf | d  d   d  d   d f } n  t |  _  |  j j |  t	 | |  j j
 |  j j d t   } d |  j k r | j   } n  | j |  j d |  j j
 |  j t |  d  S(	   Ns&   Format %s only supports single images.i   ii   i    t   prefer_uint8t   bitsR9   (   Rc   R#   R9   R;   t   ndimt   shapeR   Rb   t   updatet   ndarray_to_pilR   t   popt   quantizet   saveR=   RN   (   R   R[   t   metat   img(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   _append_data   s    	""	$"c         C` s   |  j  j |  d  S(   N(   Rb   Rh   (   R   Rm   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   set_meta_data   s    (   R^   R_   RJ   RP   Ro   Rp   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR`      s   	
		N(   R^   R_   t   __doc__RK   R   R"   R!   t   _modest   _descriptionR   t   propertyR   R&   R/   R3   R   R4   R`   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR   ;   s   					Gt	   PNGFormatc           B` s@   e  Z d  Z d e j f d     YZ d e j f d     YZ RS(   s  A PNG format based on Pillow.
    
    This format supports grayscale, RGB and RGBA images.
    
    Parameters for reading
    ----------------------
    ignoregamma : bool
        Avoid gamma correction. Default True.
    pilmode : str
        From the Pillow documentation:
        
        * 'L' (8-bit pixels, grayscale)
        * 'P' (8-bit pixels, mapped to any other mode using a color palette)
        * 'RGB' (3x8-bit pixels, true color)
        * 'RGBA' (4x8-bit pixels, true color with transparency mask)
        * 'CMYK' (4x8-bit pixels, color separation)
        * 'YCbCr' (3x8-bit pixels, color video format)
        * 'I' (32-bit signed integer pixels)
        * 'F' (32-bit floating point pixels)
        
        PIL also provides limited support for a few special modes, including
        'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
        (true color with premultiplied alpha).
        
        When translating a color image to grayscale (mode 'L', 'I' or 'F'),
        the library uses the ITU-R 601-2 luma transform::
        
            L = R * 299/1000 + G * 587/1000 + B * 114/1000
    as_gray : bool
        If True, the image is converted using mode 'F'. When `mode` is
        not None and `as_gray` is True, the image is first converted
        according to `mode`, and the result is then "flattened" using
        mode 'F'.
    
    Parameters for saving
    ---------------------
    optimize : bool
        If present and true, instructs the PNG writer to make the output file
        as small as possible. This includes extra processing in order to find
        optimal encoder settings.
    transparency: 
        This option controls what color image to mark as transparent.
    dpi: tuple of two scalars
        The desired dpi in each direction.
    pnginfo: PIL.PngImagePlugin.PngInfo
        Object containing text tags.
    compress_level: int
        ZLIB compression level, a number between 0 and 9: 1 gives best speed,
        9 gives best compression, 0 gives no compression at all. Default is 9.
        When ``optimize`` option is True ``compress_level`` has no effect
        (it is set to 9 regardless of a value passed).
    compression: int
        Compatibility with the freeimage PNG format. If given, it overrides
        compress_level.
    icc_profile:
        The ICC Profile to include in the saved file.
    bits (experimental): int
        This option controls how many bits to store. If omitted,
        the PNG writer uses 8 bits (256 colors).
    quantize: 
        Compatibility with the freeimage PNG format. If given, it overrides
        bits. In this case, given as a number between 1-256.
    dictionary (experimental): dict
        Set the ZLIB encoder dictionary.
    prefer_uint8: bool
        Let the PNG writer truncate uint16 image arrays to uint8 if their values fall
        within the range [0, 255]. Defaults to true for legacy compatibility, however
        it is recommended to set this to false to avoid unexpected behavior when
        saving e.g. weakly saturated images.
    R4   c           B` s#   e  Z d e e d   Z d   Z RS(   c         C` s   t  j j |  d | d | S(   NRI   R6   (   R   R4   RJ   (   R   RI   R6   t   ignoregamma(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ   +  s    c         C` s   t  j j |  |  \ } } |  j j j d t  s y t | d  } Wn t t	 f k
 r` q Xt | j
 t j k r| d n d  } d } | | | | | d | (n  | | f S(   NRv   t   gammai   i   g      ?g<Nё\?(   R   R4   R\   R,   R   t   getR   t   floatR:   t
   ValueErrort   dtypet   npt   uint16(   R   RU   R[   RZ   Rw   t   scalet   gain(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR\   .  s    $N(   R^   R_   R"   RK   R   RJ   R\   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR4   *  s   R`   c           B` s#   e  Z d d e d   Z d   Z RS(   c      	   K` s!  | j  d d  | d <| d  k	 rZ | d k  s: | d k rM t d |   n  | | d <n  | d  k	 r x; t d d  D] } d | | k rv Pqv qv Wt d |   | | d <n  | r t j d	  n  d } x- | D]% } | | k r t d |   q q Wt j j	 |   |  j
 j |  d  S(   Nt   compress_leveli	   i    s!   Invalid PNG compression level: %ri   i   s)   PNG quantize must be power of two, not %rRe   s0   PIL PNG writer cannot produce interlaced images.t   optimizet   transparencyt   dpit   pnginfot   icc_profilet
   dictionaryRd   s   Invalid arg for PNG writer: %r(	   R   R   R   R   Re   R   R   R   Rd   (   Rx   R"   Rz   t   ranget   loggert   warningt	   TypeErrorR   R`   RJ   Rb   Rh   (   R   t   compressionRk   t
   interlacedR   Re   t   ok_keyst   key(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ   B  s8            c         C` sx   t  | j  d k rL | j d k s7 | j d d k rL t | d d } n t | d d } t j j |  | |  d  S(   NR}   i   ii   t   bitdepthi   i   (   t   strR{   Rf   Rg   R   R   R`   Ro   (   R   R[   Rm   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRo   i  s    7N(   R^   R_   R"   RK   RJ   Ro   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR`   A  s   '(   R^   R_   Rq   R   R4   R`   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRu      s   Ft
   JPEGFormatc           B` s@   e  Z d  Z d e j f d     YZ d e j f d     YZ RS(   s	  A JPEG format based on Pillow.
    
    This format supports grayscale, RGB and RGBA images.
    
    Parameters for reading
    ----------------------
    exifrotate : bool
        Automatically rotate the image according to exif flag. Default True.
    pilmode : str
        From the Pillow documentation:
        
        * 'L' (8-bit pixels, grayscale)
        * 'P' (8-bit pixels, mapped to any other mode using a color palette)
        * 'RGB' (3x8-bit pixels, true color)
        * 'RGBA' (4x8-bit pixels, true color with transparency mask)
        * 'CMYK' (4x8-bit pixels, color separation)
        * 'YCbCr' (3x8-bit pixels, color video format)
        * 'I' (32-bit signed integer pixels)
        * 'F' (32-bit floating point pixels)
        
        PIL also provides limited support for a few special modes, including
        'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
        (true color with premultiplied alpha).
        
        When translating a color image to grayscale (mode 'L', 'I' or 'F'),
        the library uses the ITU-R 601-2 luma transform::
        
            L = R * 299/1000 + G * 587/1000 + B * 114/1000
    as_gray : bool
        If True, the image is converted using mode 'F'. When `mode` is
        not None and `as_gray` is True, the image is first converted
        according to `mode`, and the result is then "flattened" using
        mode 'F'.
    
    Parameters for saving
    ---------------------
    quality : scalar
        The compression factor of the saved image (1..100), higher
        numbers result in higher quality but larger file size. Default 75.
    progressive : bool
        Save as a progressive JPEG file (e.g. for images on the web).
        Default False.
    optimize : bool
        On saving, compute optimal Huffman coding tables (can reduce a few
        percent of file size). Default False.
    dpi : tuple of int
        The pixel density, ``(x,y)``.
    icc_profile : object
        If present and true, the image is stored with the provided ICC profile.
        If this parameter is not provided, the image will be saved with no
        profile attached.
    exif : dict
        If present, the image will be stored with the provided raw EXIF data.
    subsampling : str
        Sets the subsampling for the encoder. See Pillow docs for details.
    qtables : object
        Set the qtables for the encoder. See Pillow docs for details.
    R4   c           B` s5   e  Z d e e d   Z d   Z d   Z d   Z RS(   c         C` s   t  j j |  d | d | S(   NRI   R6   (   R   R4   RJ   (   R   RI   R6   t
   exifrotate(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ     s    c         C` sl   |  j  j j d  s3 d |  j  j j d d  k rR t |  _ t |  j  j   d  St |  _ |  j  j	   Sd  S(   Ns   http://s   https://s   .zip/s   \t   /t   rb(   s   http://s   https://(
   R,   t   filenamet
   startswitht   replaceR   RL   t   opent   get_local_filenameRK   RM   (   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR<     s    			c         C` s   t  j j |  |  \ } } d | k r d d l m } i  | d <xF |  j j   j   D], \ } } | j | |  } | | d | <qW Wn  |  j	 | |  } | | f S(   Nt   exifi    (   t   TAGSt	   EXIF_MAIN(
   R   R4   R\   t   PIL.ExifTagsR   R>   t   _getexift   itemsRx   t   _rotate(   R   RU   R[   RZ   R   t   tagt   valuet   decoded(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR\     s    
"c         C` s   |  j  j j d t  r y | d d } Wn t k
 r= q X| d k rM n  | d k rn t j | d  } n  | d k r t j | d  } n  | d k r t j |  } n  | d k r t j |  } q n  | S(   s    Use Orientation information from EXIF meta data to 
            orient the image correctly. Similar code as in FreeImage plugin.
            R   R   t   Orientationi   i   i   i   i   i   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   i   i   (   R,   R   Rx   R   R:   R|   t   rot90t   fliplr(   R   R[   Rm   t   ori(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR     s     N(	   R^   R_   R"   RK   R   RJ   R<   R\   R   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR4     s   		R`   c           B` s#   e  Z d  e e d  Z d   Z RS(   iK   c         K` s   t  |  } | d k  s$ | d k r3 t d   n  t d t d |   } | | d <t |  | d <t |  | d <t j j |   |  j j	 |  d  S(   Ni   id   s)   JPEG quality should be between 1 and 100.i_   t   qualityt   progressiveR   (
   t   intRz   t   mint   maxt   boolR   R`   RJ   Rb   Rh   (   R   R   R   R   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ     s    
c         C` s]   | j  d k r1 | j d d k r1 t d   n  t | d d } t j j |  | |  d  S(   Ni   ii   s$   JPEG does not support alpha channel.R   i   (   Rf   Rg   t   IOErrorR   R   R`   Ro   (   R   R[   Rm   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRo     s
    "(   R^   R_   RK   RJ   Ro   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR`     s   (   R^   R_   Rq   R   R4   R`   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR   q  s   :7t   JPEG2000Formatc           B` s@   e  Z d  Z d e j f d     YZ d e j f d     YZ RS(   sO  A JPEG 2000 format based on Pillow.
    
    This format supports grayscale and RGB images.
    
    Parameters for reading
    ----------------------
    pilmode : str
        From the Pillow documentation:
        
        * 'L' (8-bit pixels, grayscale)
        * 'P' (8-bit pixels, mapped to any other mode using a color palette)
        * 'RGB' (3x8-bit pixels, true color)
        * 'RGBA' (4x8-bit pixels, true color with transparency mask)
        * 'CMYK' (4x8-bit pixels, color separation)
        * 'YCbCr' (3x8-bit pixels, color video format)
        * 'I' (32-bit signed integer pixels)
        * 'F' (32-bit floating point pixels)
        
        PIL also provides limited support for a few special modes, including
        'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
        (true color with premultiplied alpha).
        
        When translating a color image to grayscale (mode 'L', 'I' or 'F'),
        the library uses the ITU-R 601-2 luma transform::
        
            L = R * 299/1000 + G * 587/1000 + B * 114/1000
    as_gray : bool
        If True, the image is converted using mode 'F'. When `mode` is
        not None and `as_gray` is True, the image is first converted
        according to `mode`, and the result is then "flattened" using
        mode 'F'.
    
    Parameters for saving
    ---------------------    
    **quality_mode**
        Either `"rates"` or `"dB"` depending on the units you want to use to
        specify image quality.
    
    **quality**
        Approximate size reduction (if quality mode is `rates`) or a signal to noise ratio
        in decibels (if quality mode is `dB`).  
    
    .. note::
    
       To enable JPEG 2000 support, you need to build and install the OpenJPEG
       library, version 2.0.0 or higher, before building the Python Imaging
       Library.
    
       Windows users can install the OpenJPEG binaries available on the
       OpenJPEG website, but must add them to their PATH in order to use PIL (if
       you fail to do this, you will get errors about not being able to load the
       ``_imaging`` DLL).

    R4   c           B` s2   e  Z d e d   Z d   Z d   Z d   Z RS(   c         C` s   t  j j |  d | d | S(   NRI   R6   (   R   R4   RJ   (   R   RI   R6   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ   5  s    c         C` sl   |  j  j j d  s3 d |  j  j j d d  k rR t |  _ t |  j  j   d  St |  _ |  j  j	   Sd  S(   Ns   http://s   https://s   .zip/s   \R   R   (   s   http://s   https://(
   R,   R   R   R   R   RL   R   R   RK   RM   (   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR<   8  s    			c         C` s   t  j j |  |  \ } } d | k r d d l m } i  | d <xF |  j j   j   D], \ } } | j | |  } | | d | <qW Wn  |  j	 | |  } | | f S(   NR   i    (   R   R   (
   R   R4   R\   R   R   R>   R   R   Rx   R   (   R   RU   R[   RZ   R   R   R   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR\   C  s    
"c         C` s   |  j  j j d t  r y | d d } Wn t k
 r= q X| d k rM n  | d k rn t j | d  } n  | d k r t j | d  } n  | d k r t j |  } n  | d k r t j |  } q n  | S(   s    Use Orientation information from EXIF meta data to 
            orient the image correctly. Similar code as in FreeImage plugin.
            R   R   R   i   i   i   i   i   i   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   i   i   (   R,   R   Rx   R   R:   R|   R   R   (   R   R[   Rm   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR   R  s     N(   R^   R_   R"   RK   RJ   R<   R\   R   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR4   4  s   		R`   c           B` s    e  Z d  d d  Z d   Z RS(   t   ratesi   c         K` s   | d d h k r! t  d   n  t |  } | d k ri | d k  sQ | d k ri t  d j |    n< | d k r | d k  s | d k r t  d	 j |    n  | | d
 <| g | d <t j j |   |  j j |  d  S(   NR   t   dBs-   Quality mode should be either 'rates' or 'dB'i   i  s1   The quality value {} seems to be an invalid rate!i   id   s1   The quality value {} seems to be an invalid PSNR!t   quality_modet   quality_layers(   Rz   Ry   R9   R   R`   RJ   Rb   Rh   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRJ   l  s    $$
c         C` s]   | j  d k r1 | j d d k r1 t d   n  t | d d } t j j |  | |  d  S(   Ni   ii   sG   The current implementation of JPEG 2000 does not support alpha channel.R   i   (   Rf   Rg   R   R   R   R`   Ro   (   R   R[   Rm   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRo     s    "(   R^   R_   RJ   Ro   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR`   k  s   (   R^   R_   Rq   R   R4   R`   (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyR     s   67c         C` s>   t  |  d  r: t  t |  d d   d  r: |  j   q: n  d  S(   NRO   t   fp(   R   t   getattrR"   RO   (   R[   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRN     s    c         C` sj   y |  j    d WnQ t k
 re } d } | d 7} t |  } d |  j | | f } t |   n Xd  S(   Ni    s8   http://pillow.readthedocs.io/en/latest/installation.htmls   #external-librariessA   Could not load "%s" 
Reason: "%s"
Please see documentation at: %s(   RX   R   R   R   Rz   (   R[   t   et   sitet   pillow_error_messaget   error_message(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRD     s    
c         C` s   |  j  d k r t S|  j j d d   r, t St j |  j    j d  } |  j	   \ } } | | | d !} t j
 t j |  d  S(   Nt   PR   i   i   i   i    (   i   i   (   R(   RK   RZ   Rx   R"   R|   t   asarrayt
   getpalettet   reshapet
   getextremat   allcloset   diff(   t	   pil_imageR@   t   startt   stopt   valid_palette(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRF     s    c   
      C` s  | d k r t |   } n  |  } | d k	 rQ | |  j k r|  j |  } qn| rZ n|  j d k r | r |  j d  } no|  j d k r|  j j d d  d k	 r |  j d  } q|  j j d  k rt j |  j j	   d t j
  } t |  j d  r|  j j |  j _ n  |  j j r0|  j j n	 |  j j } t |  } d | f | _ | j d d	 k s| j d d
 k r| d d k rt j | d d  d d	  f d t j | j d | j  f  } n  | j d  r;| j d d	 k r| d d  d d d g f n | d d  d d d d	 g f } n  t j |  t j
  } y | | } Wqt k
 r|  j d  } qXqt r|  j d  } q|  j d  } nB d |  j k r|  j d  } n! |  j d k r|  j d  } n  | r| j d  } n4 t | t j  r<| j d k r<| j d  } n  |  j j d  r|  j }	 |  j j d  rod n d } d |  j k r| j d d  } n  t j | j   |  j   } |	 d d d  | _ nH |  j d k r|  j d k r| d k rd } n  t j | d | } | S(!   s    
    is_gray: Whether the image *is* gray (by inspecting its palette).
    as_gray: Whether the resulting image must be converted to gaey.
    mode: The mode to convert to.
    R   t   LR   t   RGBAt   RGBi   RC   ii   i   t   XNi   i    t   BGRi   t   At   CMYKt   Ft   1s   I;16t   Bs   >u2s   <u2t   St   uR	   R   t   IR}   R{   (   R   R   (   R"   RF   R(   t   convertRZ   Rx   R@   R|   t
   frombufferRX   t   uint8R   RC   RB   t   lenRg   t   column_stackt   onesR{   R   t   arrayt	   ExceptionR   t
   isinstancet   ndarrayR?   t   endswithR   t   tobytest   copyR9   (
   R[   R7   R6   R(   R{   t   framet   pt	   nchannelst   frame_palettedRg   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRY     sf    "$6HW"	*	c   	      C` s  d d l  m } |  j d k rS t |  d d }  i d d 6d d 6|  j d	 } n | d k r d } d } |  j j d k r t |   }  q| r |  j   d k  r |  j   d k r |  j	 t
 j  }  d } } qt |  d d }  n t |  d d }  d } d } | d k rt t | d d  j d  d  d k  r|  j   } |  j d	 k r| j | |  j j  } | j | d |  n/ |  j d |  j d f } | j | | |  } | S| j |  |  Sd  S(   Ni    (   R   i   R   i   R   R   i   i   t   pngR   s   I;16R   t   fi   R   i   R   t   0t   .i   t   rawi   (   R   R   (   R   R   Rf   R   Rg   R{   t   kindR   R   t   astypeR|   R   R   R   t   splitR   t   newt   Tt	   frombytest	   fromarray(	   t   arrt
   format_strRd   R   R(   t	   mode_baset   array_bufferR[   t   image_shape(    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyRi     s2    "*7(   t	   GIFFormatt
   TIFFFormatt   MPEGR   R   R   t   TIFFt   JPEG2000c          C` s   x t  D] \ }  } } |  t k r( q n  t j |  t  } | j pF | } | |  d | | | j  } |  | _ | t k s | j r t	 |  t
 | _ n  t j |  q Wd  S(   Ns   -PIL(   R   t   IGNORE_FORMATSt   SPECIAL_FORMATSRx   R   Rs   Rr   R   Rq   R   t   GENERIC_DOCSR   t
   add_format(   t   idt   summaryt   extt	   FormatClsR9   (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   register_pillow_formatsX  s    	('   Rq   t
   __future__R    R   R   t   loggingR   t   numpyR|   R
   R   t   coreR   R   t   pillow_infoR   R   t	   getLoggerR^   R   R   R   Ru   R   R   RN   RD   RF   R"   RY   R   Ri   t   pillowmultiR   R   R   RE   R   R  (    (    (    s5   lib/python2.7/site-packages/imageio/plugins/pillow.pyt   <module>   s8   #			a.		