ó
î Xc           @   s!  d  d l  m Z d  d l m Z d  d l Z d  d l m Z d   Z d   Z d   Z	 d d d	  Z d
   Z d d  Z e j d  Z d d  Z d d d  Z e j d d d  Z d   Z d   Z d   Z d   Z d   Z d d  Z d d  Z e Z d d d d  Z e Z d S(   i˙˙˙˙(   t   Image(   t   isStringTypeN(   t   reducec         C   s   t  |  t  rd t |   d k r: |  \ } } \ } } qv t |   d k rv |  \ } } } } qv n |  } } } } | | | | f S(   Ni   i   (   t
   isinstancet   tuplet   len(   t   bordert   leftt   topt   rightt   bottom(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   _border   s    c         C   s5   t  |   r1 d d l m } | j |  |  }  n  |  S(   Ni˙˙˙˙(   t
   ImageColor(   R   t   PILR   t   getcolor(   t   colort   modeR   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   _color(   s    c         C   s|   |  j  d k r t d   nZ |  j  d k rl |  j  d k r_ t |  d k r_ | | | } n  |  j |  St d   d  S(   Nt   Ps   mode P support coming soont   Lt   RGBi   s!   not supported for this image mode(   R   R   (   R   t   NotImplementedErrorR   t   pointt   IOError(   t   imaget   lut(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   _lut/   s    !i    c         C   s¸  |  j    } g  } xt d t |  d  D]|} | | | d !} | d k	 r y d | | <Wq t k
 r x | D] } d | | <qs Wq Xn  | r´d } x" t d  D] } | | | } q§ W| | d }	 xe t d  D]W }
 |	 | |
 k r|	 | |
 }	 d | |
 <n | |
 c |	 8<d }	 |	 d k rÚ PqÚ qÚ W| | d }	 xn t d d d  D]W } |	 | | k r|	 | | }	 d | | <n | | c |	 8<d }	 |	 d k rVPqVqVWn  x" t d  D] }
 | |
 rÁPqÁqÁWx( t d d d  D] } | | rěPqěqěW| |
 k r,| j t t d    q+ d | |
 } |
 | } x_ t d  D]Q } t | | |  } | d k  rd } n | d k rd } n  | j |  qRWq+ Wt	 |  |  S(   s   
    Maximize (normalize) image contrast. This function calculates a
    histogram of the input image, removes **cutoff** percent of the
    lightest and darkest pixels from the histogram, and remaps the image
    so that the darkest pixel becomes black (0), and the lightest
    becomes white (255).

    :param image: The image to process.
    :param cutoff: How many percent to cut off from the histogram.
    :param ignore: The background pixel value (use None for no background).
    :return: An image.
    i    i   id   i˙   i˙˙˙˙g     ŕo@N(
   t	   histogramt   rangeR   t   Nonet	   TypeErrort   extendt   listt   intt   appendR   (   R   t   cutofft   ignoreR   R   t   layert   ht   ixt   nt   cutt   lot   hit   scalet   offset(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   autocontrast>   sd    

		c         C   sű   |  j  d k s t  t | d  } t | d  } g  } g  } g  } x t d  D] } | j | d | | d | d d  | j | d | | d | d d  | j | d | | d | d d  qR W|  j d  }  t |  | | |  S(   sľ  
    Colorize grayscale image.  The **black** and **white**
    arguments should be RGB tuples; this function calculates a color
    wedge mapping all black pixels in the source image to the first
    color, and all white pixels to the second color.

    :param image: The image to colorize.
    :param black: The color to use for black input pixels.
    :param white: The color to use for white input pixels.
    :return: An image.
    R   R   i   i    i˙   i   i   (   R   t   AssertionErrorR   R   R"   t   convertR   (   R   t   blackt   whitet   redt   greent   bluet   i(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   colorize   s    ))-c         C   sG   t  |  \ } } } } |  j | | |  j d | |  j d | f  S(   s/  
    Remove border from image.  The same amount of pixels are removed
    from all four sides.  This function works on all image modes.

    .. seealso:: :py:meth:`~PIL.Image.Image.crop`

    :param image: The image to crop.
    :param border: The number of pixels to remove.
    :return: An image.
    i    i   (   R   t   cropt   size(   R   R   R   R   R	   R
   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyR8   ¤   s    c         C   s%   |  j  |  j t j | j |   |  S(   s  
    Deform the image.

    :param image: The image to deform.
    :param deformer: A deformer object.  Any object that implements a
                    **getmesh** method can be used.
    :param resample: What resampling filter to use.
    :return: An image.
    (   t	   transformR9   R    t   MESHt   getmesh(   R   t   deformert   resample(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   deformľ   s    
c   
      C   sB  |  j  d k r! |  j d  }  n  |  j |  } g  } xü t d t |  d  D]â } g  | | | d !D] } | rg | ^ qg } t |  d k r­ | j t t d    qO t t j	 |  | d d } | sí | j t t d    qO | d } x7 t d  D]) }	 | j
 | |  | | |	 | } qWqO Wt |  |  S(	   s  
    Equalize the image histogram. This function applies a non-linear
    mapping to the input image, in order to create a uniform
    distribution of grayscale values in the output image.

    :param image: The image to equalize.
    :param mask: An optional mask.  If given, only the pixels selected by
                 the mask are included in the analysis.
    :return: An image.
    R   R   i    i   i   i˙˙˙˙i˙   i   (   R   R0   R   R   R   R   R    R   t   operatort   addR"   R   (
   R   t   maskR&   R   t   bt   _ft   histot   stepR(   R6   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   equalizeÄ   s     *
c   
      C   s   t  |  \ } } } } | |  j d | } | |  j d | } t j |  j | | f t | |  j   }	 |	 j |  | | f  |	 S(   sÖ   
    Add border to the image

    :param image: The image to expand.
    :param border: Border width, in pixels.
    :param fill: Pixel fill value (a color value).  Default is 0 (black).
    :return: An image.
    i    i   (   R   R9   R    t   newR   R   t   paste(
   R   R   t   fillR   R   R	   R
   t   widtht   heightt   out(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   expandă   s    
*g        g      ŕ?c         C   sÎ  t  | t  s& | d | d g } n  | d d k sF | d d k  rS d | d <n  | d d k ss | d d k  r d | d <n  | d k s | d k  rĄ d } n  t t |  t |  j d  d  t t |  t |  j d  d  f } d d |  j d |  j d f } | d k r^| d | d |  j d | d d |  j d | d d f } n  | d | d | d | d f } t | d  t | d  } t | d  t | d  }	 | |	 k rőt |	 t | d  d  }
 | d } n( | d }
 t t | d  |	 d  } t | d t | d |
  | d  } | d k  r\d } n  t | d t | d |  | d  } | d k  rd } n  |  j | | | |
 | | f  } | j | |  S(	   sy  
    Returns a sized and cropped version of the image, cropped to the
    requested aspect ratio and size.

    This function was contributed by Kevin Cazabon.

    :param size: The requested output size in pixels, given as a
                 (width, height) tuple.
    :param method: What resampling method to use. Default is
                   :py:attr:`PIL.Image.NEAREST`.
    :param bleed: Remove a border around the outside of the image (from all
                  four edges. The value is a decimal percentage (use 0.01 for
                  one percent). The default value is 0 (no border).
    :param centering: Control the cropping position.  Use (0.5, 0.5) for
                      center cropping (e.g. if cropping the width, take 50% off
                      of the left side, and therefore 50% off the right side).
                      (0.0, 0.0) will crop from the top left corner (i.e. if
                      cropping the width, take all of the crop off of the right
                      side, and if cropping the height, take all of it off the
                      bottom).  (1.0, 0.0) will crop from the bottom left
                      corner, etc. (i.e. if cropping the width, take all of the
                      crop off the left side, and if cropping the height take
                      none from the top, and therefore all off the bottom).
    :return: An image.
    i    i   g      đ?g        g      ŕ?g9îÖ˙ß?i   i   (   R   R    R!   t   floatR9   R8   t   resize(   R   R9   t   methodt   bleedt	   centeringt   bleedPixelst   liveAreat   liveSizet   liveAreaAspectRatiot   aspectRatiot	   cropWidtht
   cropHeightt   leftSidet   topSideRM   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   fitő   s>       	$* $$
*	*	c         C   s   |  j  t j  S(   sq   
    Flip the image vertically (top to bottom).

    :param image: The image to flip.
    :return: An image.
    (   t	   transposeR    t   FLIP_TOP_BOTTOM(   R   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   flipR  s    c         C   s   |  j  d  S(   si   
    Convert the image to grayscale.

    :param image: The image to convert.
    :return: An image.
    R   (   R0   (   R   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt	   grayscale\  s    c         C   s;   g  } x% t  d  D] } | j d |  q Wt |  |  S(   sc   
    Invert (negate) the image.

    :param image: The image to invert.
    :return: An image.
    i   i˙   (   R   R"   R   (   R   R   R6   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   invertf  s    c         C   s   |  j  t j  S(   sq   
    Flip image horizontally (left to right).

    :param image: The image to mirror.
    :return: An image.
    (   R^   R    t   FLIP_LEFT_RIGHT(   R   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   mirrors  s    c         C   sN   g  } d d | d } x% t  d  D] } | j | | @ q& Wt |  |  S(   sÁ   
    Reduce the number of bits for each color channel.

    :param image: The image to posterize.
    :param bits: The number of bits to keep for each channel (1-8).
    :return: An image.
    i   i   i   i   (   R   R"   R   (   R   t   bitsR   RB   R6   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt	   posterize}  s
    i   c         C   sW   g  } xA t  d  D]3 } | | k  r5 | j |  q | j d |  q Wt |  |  S(   sż   
    Invert all pixel values above a threshold.

    :param image: The image to solarize.
    :param threshold: All pixels above this greyscale level are inverted.
    :return: An image.
    i   i˙   (   R   R"   R   (   R   t	   thresholdR   R6   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   solarize  s    c         C   s/   | d k r d } n  |  j   |  j j |  S(   s    PIL_usm.gblur(im, [radius])g      @N(   R   t   loadt   imt   gaussian_blur(   Rj   t   radius(    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyRk      s    	
c         C   s_   | d k r d } n  | d k r* d } n  | d k r? d } n  |  j   |  j j | | |  S(   s.    PIL_usm.usm(im, [radius, percent, threshold])g      @i   i   N(   R   Ri   Rj   t   unsharp_mask(   Rj   Rl   t   percentRg   (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyRm   ­  s    			
(   g      ŕ?g      ŕ?(   R   R    t	   PIL._utilR   R@   t	   functoolsR   R   R   R   R   R.   R7   R8   t   BILINEARR?   RG   RN   t   NEARESTR]   R`   Ra   Rb   Rd   Rf   Rh   Rk   t   gblurRm   t   usm(    (    (    s0   /usr/lib/python2.7/dist-packages/PIL/ImageOps.pyt   <module>   s.   			L	]	
	
		
	
