ó
P'^c           @   sś   d  d l  m Z d  d l m Z d d l m Z m Z m Z m Z d e	 f d     YZ
 e
 j d  Z d   Z d	 e	 f d
     YZ d e	 f d     YZ d e	 f d     YZ d S(   i   (   t   Anchor(   t   histogram_entropyi   (   t   Imaget
   ImageChopst	   ImageDrawt	   ImageStatt   Sidec           B   s2   e  Z d  Z d Z d Z d Z e e e e f Z RS(   t   tt   rt   bt   l(   t   __name__t
   __module__t   TOPt   RIGHTt   BOTTOMt   LEFTt   ALL(    (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR      s
   c         C   s   t  j | k r | d n d t  j | k r5 | d n d t  j | k rQ | d n
 |  j d t  j | k rt | d n
 |  j d f } |  j |  S(   Ni    i   i   i   (   R   R   R   R   t   sizeR   t   crop(   t   imgt   bboxt   sides(    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   _crop   s
    #)c         C   sŁ   t  j d |  j d  } |  j d d |  j d d } } | d k r | d k r t j |  } | j d d | | g d  n  t j |  j d  j	 |   j
 S(   Nt   1i   i    i   t   RGBA(   R   t   newR   R   t   Drawt	   rectangleR   t   Statt   convertt	   histogramt   median(   R   t   maskt   wt   ht   draw(    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   detect_border_color   s    #t   TrimBorderColorc           B   s,   e  Z d  Z d d e j d  Z d   Z RS(   s/   Trims a color from the sides of an image.

    g333333Ó?c         C   s   | |  _  | |  _ | |  _ d S(   s  
        :param color: The color to trim from the image, in a 4-tuple RGBA value,
            where each component is an integer between 0 and 255, inclusive. If
            no color is provided, the processor will attempt to detect the
            border color automatically.
        :param tolerance: A number between 0 and 1 where 0. Zero is the least
            tolerant and one is the most.
        :param sides: A list of sides that should be trimmed. Possible values
            are provided by the :class:`Side` enum class.

        N(   t   colorR   t	   tolerance(   t   selfR'   R(   R   (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   __init__%   s    		c         C   s  | j  d  } |  j p' t t |   } t j d | j |  } t j | |  } |  j	 d k rŐ d |  j	 k o} d k n s t
 d |  j	   n  t j | t |  j	 d   j  d  } t j | |  } n  | j   } | r˙ t | | |  j  } n  | S(   NR   i    i   sN   %s is an invalid tolerance. Acceptable values are between 0 and 1 (inclusive).i˙   (   i    i   (   R   R'   t   tupleR%   R   R   R   R   t
   differenceR(   t
   ValueErrort   constantt   intt   subtractt   getbboxR   R   (   R)   R   t   sourcet   border_colort   bgt   difft   tmpR   (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   process5   s    	N(   R   R   t   __doc__t   NoneR   R   R*   R7   (    (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR&   !   s   t   Cropc           B   s/   e  Z d  Z d d d d d d  Z d   Z RS(   s  
    Crops an image, cropping it to the specified width and height. You may
    optionally provide either an anchor or x and y coordinates. This processor
    functions exactly the same as ``ResizeCanvas`` except that it will never
    enlarge the image.

    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   widtht   heightt   anchort   xt   y(   R)   R;   R<   R=   R>   R?   (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR*   T   s
    				c      	   C   su   d d l  m } | j \ } } t | |  j  t | |  j  } } | | | d |  j d |  j d |  j j	 |  S(   Ni   (   t   ResizeCanvasR=   R>   R?   (
   t   resizeR@   R   t   minR;   R<   R=   R>   R?   R7   (   R)   R   R@   t   original_widtht   original_heightt	   new_widtht
   new_height(    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR7   [   s    N(   R   R   R8   R9   R*   R7   (    (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR:   K   s   t	   SmartCropc           B   s/   e  Z d  Z d d d  Z d   Z d   Z RS(   s  
    Crop an image to the specified dimensions, whittling away the parts of the
    image with the least entropy.

    Based on smart crop implementation from easy-thumbnails:
        https://github.com/SmileyChris/easy-thumbnails/blob/master/easy_thumbnails/processors.py#L193

    c         C   s   | |  _  | |  _ d S(   sr   
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.

        N(   R;   R<   (   R)   R;   R<   (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR*   p   s    	c         C   s   t  |  } t  |  } | rj t | | d  d k  rj | | d k rR | | f S| d } | | | f S| | k r d | f S| d f Sd S(   sŰ   
        Calculate the entropy of two slices (from the start and end of an axis),
        returning a tuple containing the amount that should be added to the start
        and removed from the end of the axis.

        i   g{ŽGáz?i   i    N(   R   t   abs(   R)   t   start_slicet	   end_slicet   sliceR,   t   start_entropyt   end_entropyt
   half_slice(    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   compare_entropyy   s     


c         C   sŃ  | j  \ } } t | t | |  j   } t | t | |  j   } d } } | | } }	 x¤ | rt | t | d d   }
 | j | d | |
 | f  } | j | |
 d | | f  } |  j | | |
 |  \ } } | | 7} | | 8} | | | } qa Wx¤ | rŤt | t | d d   }
 | j d | | | |
 f  } | j d |	 |
 | |	 f  } |  j | | |
 |  \ } } | | 7} |	 | 8}	 | | | } qW| | | |	 f } | j |  } | S(   Ni    i   i
   (   R   R/   RB   R;   R<   t   maxR   RO   (   R)   R   t   source_xt   source_yt   diff_xt   diff_yt   leftt   topt   rightt   bottomRK   t   startt   endt   addt   removet   box(    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyR7      s0    
	

	

N(   R   R   R8   R9   R*   RO   R7   (    (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyRG   f   s   		N(   t   baseR    t   utilsR   t   libR   R   R   R   t   objectR   R   R   R%   R&   R:   RG   (    (    (    s8   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/crop.pyt   <module>   s   "
		*