Ñò
ÔoXJc           @   s^  d  d k  Z  d  d k Z d  d k  l Z l Z l Z l Z l Z l Z l Z l	 Z	 l
 Z
 l Z l Z l Z l Z l Z l Z l Z d  d k Z d  d k Z d d d d d d d	 d
 d d g
 Z d d d d d „ Z d d „ Z d „  Z d d „ Z d Z d d d d d d d d „ Z d d „ Z d d d „ Z d „  Z d „  Z d „  Z d d „ Z  d S(   iÿÿÿÿN(   t   amint   amaxt   ravelt   asarrayt   castt   aranget   onest   newaxist	   transposet   mgridt   iscomplexobjt   sumt   zerost   uint8t
   issubdtypet   arrayt	   fromimaget   toimaget   imsavet   imreadt	   bytescalet   imrotatet   imresizet   imshowt   imfiltert   radoniÿ   i    c         C   s£   |  i  t j o |  S| | } | d  j o |  i ƒ  } n | d  j o |  i ƒ  } n | d | | p d } |  d | | d i t ƒ } | t t | ƒ S(   Ng      ð?i   g<NÑ‘\þß?(   t   dtypeR   t   Nonet   mint   maxt   astypeR   (   t   datat   cmint   cmaxt   hight   lowt   scalet   bytedata(    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR      s    
  c         C   s   t  i |  ƒ } t | d | ƒS(   sö   Read an image file from a filename.

    Optional arguments:

     - flatten (0): if true, the image is flattened by calling convert('F') on
     the resulting image object.  This flattens the color layers into a single
     grayscale layer.
    t   flatten(   t   Imaget   openR   (   t   nameR&   t   im(    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR      s    
c         C   s   t  | ƒ } | i |  ƒ d S(   s$   Save an array to an image file.
    N(   R   t   save(   R)   t   arrR*   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   (   s    c         C   sD   t  i |  ƒ p t d ƒ ‚ n | o |  i d ƒ }  n t |  ƒ S(   s¥  Return a copy of a PIL image as a numpy array.

    :Parameters:
        im : PIL image
            Input image.
        flatten : bool
            If true, convert the output to grey-scale.

    :Returns:
        img_array : ndarray
            The different colour bands/channels are stored in the
            third dimension, such that a grey-image is MxN, an
            RGB-image MxNx3 and an RGBA-image MxNx4.

    s   Input is not a PIL image.t   F(   R'   t   isImageTypet	   TypeErrort   convertR   (   R*   R&   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   /   s
    s7   Mode is unknown or incompatible with input array shape.c      
   C   s¶  t  |  ƒ } t | ƒ o t d ‚ n t | i ƒ }	 t |	 ƒ d j p* t |	 ƒ d j o d |	 j p
 d |	 j }
 t |	 ƒ d j o9|	 d |	 d f }	 | d j o2 | i t i ƒ } t	 i
 | |	 | i ƒ  ƒ } | S| d j oì t | d
 | d | d | d | ƒ} t	 i
 d |	 | i ƒ  ƒ } | d j	 o# | i t  | d t ƒi ƒ  ƒ ny | d	 j ok t d d d d t ƒd d … t f t d d t ƒt d d … f } | i t  | d t ƒi ƒ  ƒ n | S| d j o, | | j } t	 i
 d |	 | i ƒ  ƒ } | S| d j o t t | ƒ ƒ } n | d j o t t | ƒ ƒ } n | d | | | | | | } | d j o1 | i t i ƒ } t	 i
 | |	 | i ƒ  ƒ } n
 t t ‚ | S| d j os d |	 j o# t i t  |	 ƒ d j ƒ d } qFt i t  |	 ƒ d j ƒ } t | ƒ o | d } qFt d ‚ n | } |	 | } | d j o t d ‚ n t | d
 | d | d | d | ƒ} | d j o$ | i ƒ  } |	 d |	 d f }	 nu | d j o- t | d ƒ i ƒ  } |	 d |	 d f }	 n; | d j o- t | d ƒ i ƒ  } |	 d |	 d f }	 n | d j o! | d j o
 d } qad } n | d j o t t ‚ n | d  j o n | d! j o n t	 i
 | |	 | ƒ } | S("   s¢  Takes a numpy array and returns a PIL image.  The mode of the
    PIL image depends on the array shape, the pal keyword, and the mode
    keyword.

    For 2-D arrays, if pal is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then mode='P', otherwise mode='L', unless mode is given
    as 'F' or 'I' in which case a float and/or integer array is made

    For 3-D arrays, the channel_axis argument tells which dimension of the
      array holds the channel data.
    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
      by default or 'YCbCr' if selected.
    if the

    The numpy array must be either 2 dimensional or 3 dimensional.
    s&   Cannot convert a complex-valued array.i   i   i   i   i    R-   t   Lt   PR"   R#   R    R!   R   i   Nt   1g      ð?t   Is!   Could not find channel dimension.s$   Channel axis dimension is not valid.t   RGBt   RGBAt   YCbCrt   CMYK(   NR1   R2   (   i   (   i   i   (   i    i   i   (   i   i   i    (   R5   R6   R7   R8   (   R5   R7   (   R6   R8   (   R   R
   t
   ValueErrort   listt   shapet   lenR   t   numpyt   float32R'   t
   fromstringt   tostringR   R   t
   putpaletteR   R   R   R   R    R   R   t   uint32t   _errstrt   flatnonzeroR   (   R,   R"   R#   R    R!   t   palt   modet   channel_axisR   R;   t   validt   data32t   imageR%   t   cat   numcht   strdata(    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   F   sŠ    &$#%##"	#
$ 

t   bilinearc         C   s]   t  |  ƒ }  h d d 6d d 6d d 6d d 6} t |  ƒ } | i | d | | ƒ} t | ƒ S(	   sÒ   Rotate an image counter-clockwise by angle degrees.

    Interpolation methods can be:
        'nearest' :  for nearest neighbor
        'bilinear' : for bilinear
        'cubic' or 'bicubic' : for bicubic
    i    t   nearesti   RN   i   t   bicubict   cubict   resample(   R   R   t   rotateR   (   R,   t   anglet   interpt   funcR*   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   ­   s
    "c         C   s…   t  | ƒ } | i ƒ  t | ƒ } t |  ƒ }  h d d 6d d 6d d 6d d 6} t |  d | ƒ} | i | d	 | | ƒ} t | ƒ S(
   Ni    RO   i   RN   i   RP   RQ   RF   RR   (   R:   t   reverset   tupleR   R   t   resizeR   (   R,   t   newsizeRU   RF   RV   R*   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   »   s    
"c         C   s½   t  |  ƒ } t i d ƒ \ } } y | i | ƒ Wn t d ƒ ‚ n Xd d k } | i | ƒ | i i d d ƒ } | i	 d | | f ƒ } | i
 | ƒ | d j o t d	 ƒ ‚ n d S(
   s;   Simple showing of an image through an external viewer.
    s   .pngs"   Error saving temporary image data.iÿÿÿÿNt   SCIPY_PIL_IMAGE_VIEWERt   sees   %s %si    s   Could not execute image viewer.(   R   t   tempfilet   mkstempR+   t   RuntimeErrort   ost   closet   environt   gett   systemt   unlink(   R,   R*   t   fnumt   fnameR`   t   cmdt   status(    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   Å   s    c         C   s™   t  |  ƒ } t | ƒ } t | t ƒ o | d } nK t t | ƒ t ƒ o  t | i ƒ | i t ƒ } n | d | d f } | i | ƒ } t	 | ƒ S(   sÈ   Resize an image.

    If size is an integer it is a percentage of current size.
    If size is a float it is a fraction of current size.
    If size is a tuple it is the size of the output image.
    g      Y@i   i    (
   R   t   typeR   t   intt   floatR   t   sizeR   RY   R   (   R,   Rm   R*   t   tst   imnew(    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   Ù   s     c         C   s­   h
 t  i d 6t  i d 6t  i d 6t  i d 6t  i d 6t  i d 6t  i d 6t  i d 6t  i	 d	 6t  i
 d
 6} t |  ƒ } | | i ƒ  j o t d ‚ n t | i | | ƒ ƒ S(   sÉ   Simple filtering of an image.

    type can be:
            'blur', 'contour', 'detail', 'edge_enhance', 'edge_enhance_more',
            'emboss', 'find_edges', 'smooth', 'smooth_more', 'sharpen'
    t   blurt   contourt   detailt   edge_enhancet   edge_enhance_moret   embosst
   find_edgest   smootht   smooth_moret   sharpens   Unknown filter type.(   t   ImageFiltert   BLURt   CONTOURt   DETAILt   EDGE_ENHANCEt   EDGE_ENHANCE_MOREt   EMBOSSt
   FIND_EDGESt   SMOOTHt   SMOOTH_MOREt   SHARPENR   t   keysR9   R   t   filter(   R,   t   ftypet   _tdictR*   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR   ì   s    







c         C   s—   | d  j o t d d !} n t |  i d t | ƒ f t ƒ } d } xJ | D]B } t |  | ƒ } t | d d ƒ| d  d  … | f <| d 7} qM W| S(   Ni    i´   i   t   axis(   R   R	   R   R;   R<   Rl   R   R   (   R,   t   thetat   st   kt   thR*   (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyR     s    " "(!   R=   R]   R    R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R'   Rz   t   __all__R   R   R   R   R   RC   R   R   R   R   R   R   (    (    (    sB   C:\graphics\Tools\Python26\Lib\site-packages\scipy\misc\pilutil.pyt   <module>   s&   j
	f
			