ó
P'—^c           @   sÔ   d  d l  m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i   (   t   Anchori   (   t   Imaget   Resizec           B   s#   e  Z d  Z e d „ Z d „  Z RS(   s>   
    Resizes an image to the specified width and height.

    c         C   s   | |  _  | |  _ | |  _ d S(   sÇ   
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param upscale: Should the image be enlarged if smaller than the dimensions?

        N(   t   widtht   heightt   upscale(   t   selfR   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyt   __init__
   s    		c         C   sl   |  j  s5 |  j | j d k  rh |  j | j d k  rh | j d ƒ } | j |  j |  j f t j ƒ } n  | S(   Ni    i   t   RGBA(   R   R   t   sizeR   t   convertt   resizeR   t	   ANTIALIAS(   R   t   img(    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyt   process   s    5$(   t   __name__t
   __module__t   __doc__t   TrueR   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR      s   t   ResizeToCoverc           B   s#   e  Z d  Z e d „ Z d „  Z RS(   së   
    Resizes the image to the smallest possible size that will entirely cover the
    provided dimensions. You probably won't be using this processor directly,
    but it's used internally by ``ResizeToFill`` and ``SmartResize``.

    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   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   #   s    c         C   sŠ   | j  \ } } t t |  j ƒ | t |  j ƒ | ƒ } t t | | ƒ ƒ t t | | ƒ ƒ } } t | | d |  j ƒj	 | ƒ } | S(   NR   (
   R	   t   maxt   floatR   R   t   intt   roundR   R   R   (   R   R   t   original_widtht   original_heightt   ratiot	   new_widtht
   new_height(    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   ,   s    !(   R   R   R   R   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR      s   	t   ResizeToFillc           B   s,   e  Z d  Z d d d e d „ Z d „  Z RS(   sQ   
    Resizes an image, cropping it to the exact specified width and height.

    c         C   s(   | |  _  | |  _ | |  _ | |  _ d S(   s.  
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param anchor: Specifies which part of the image should be retained
            when cropping.
        :param upscale: Should the image be enlarged if smaller than the dimensions?

        N(   R   R   t   anchorR   (   R   R   R   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   <   s    				c         C   s\   d d l  m } t |  j |  j d |  j ƒj | ƒ } | |  j |  j d |  j ƒj | ƒ S(   Ni   (   t   CropR   R   (   t   cropR   R   R   R   R   R   R   (   R   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   J   s
    N(   R   R   R   t   NoneR   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   6   s   t   SmartResizec           B   s#   e  Z d  Z e d „ Z d „  Z RS(   s  
    The ``SmartResize`` processor is identical to ``ResizeToFill``, except that
    it uses entropy to crop the image instead of a user-specified anchor point.
    Internally, it simply runs the ``ResizeToCover`` and ``SmartCrop``
    processors in series.
    c         C   s    | | |  _  |  _ | |  _ d S(   sÇ   
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param upscale: Should the image be enlarged if smaller than the dimensions?

        N(   R   R   R   (   R   R   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   Y   s    c         C   sS   d d l  m } t |  j |  j d |  j ƒj | ƒ } | |  j |  j ƒ j | ƒ S(   Ni   (   t	   SmartCropR   (   R    R#   R   R   R   R   R   (   R   R   R#   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   c   s    (   R   R   R   R   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR"   R   s   
t   ResizeCanvasc           B   s,   e  Z d  Z d d d d d „ Z d „  Z RS(   sx   
    Resizes the canvas, using the provided background color if the new size is
    larger than the current image.

    c         C   sŸ   | d k	 s | d k	 rX | r- t d ƒ ‚ qz | p6 d | p? d |  _ |  _ d |  _ n" | pd t j |  _ d |  _ |  _ | |  _ | |  _ | p• d |  _	 d S(   sa  
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param color: The background color to use for padding.
        :param anchor: Specifies the position of the original image on the new
            canvas. Valid values are:

            - Anchor.TOP_LEFT
            - Anchor.TOP
            - Anchor.TOP_RIGHT
            - Anchor.LEFT
            - Anchor.CENTER
            - Anchor.RIGHT
            - Anchor.BOTTOM_LEFT
            - Anchor.BOTTOM
            - Anchor.BOTTOM_RIGHT

            You may also pass a tuple that indicates the position in
            percentages. For example, ``(0, 0)`` corresponds to "top left",
            ``(0.5, 0.5)`` to "center" and ``(1, 1)`` to "bottom right". This is
            basically the same as using percentages in CSS background positions.

        sE   You may provide either an anchor or x and y coordinate, but not both.i    iÿ   N(   iÿ   iÿ   iÿ   i    (
   R!   t	   Exceptiont   xt   yR   R    t   CENTERR   R   t   color(   R   R   R   R)   R   R&   R'   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   p   s    		c   
      C   sÙ   | j  \ } } |  j rˆ t j |  j ƒ } |  j | |  j | } } t t | ƒ t | d ƒ ƒ } t t | ƒ t | d ƒ ƒ } n |  j |  j	 } } t
 j d |  j |  j f |  j ƒ }	 |	 j | | | f ƒ |	 S(   Ni    i   R   (   R	   R   R    t	   get_tupleR   R   R   R   R&   R'   R   t   newR)   t   paste(
   R   R   R   R   R   t   trim_xt   trim_yR&   R'   t   new_img(    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   —   s    	
 #$N(   R   R   R   R!   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR$   j   s   't	   AddBorderc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s?   
    Add a border of specific color and size to an image.

    c         C   s[   | |  _  t | t ƒ r9 | |  _ |  _ |  _ |  _ n | \ |  _ |  _ |  _ |  _ d S(   sÌ   
        :param color: Color to use for the border
        :param thickness: Thickness of the border. Can be either an int or
            a 4-tuple of ints of the form (top, right, bottom, left).
        N(   R)   t
   isinstanceR   t   topt   rightt   bottomt   left(   R   t	   thicknessR)   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   ­   s    	!c      	   C   sg   | j  d |  j |  j } | j  d |  j |  j } t | | d |  j d |  j d |  j ƒj | ƒ S(   Ni    i   R)   R&   R'   (   R	   R5   R3   R2   R4   R$   R)   R   (   R   R   R   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   ¹   s    N(   R   R   R   R!   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR0   ¨   s   t   ResizeToFitc           B   s2   e  Z d  Z d d e d e j d „ Z d „  Z RS(   sC   
    Resizes an image to fit within the specified dimensions.

    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d S(   sÔ  
        :param width: The maximum width of the desired image.
        :param height: The maximum height of the desired image.
        :param upscale: A boolean value specifying whether the image should
            be enlarged if its dimensions are smaller than the target
            dimensions.
        :param mat_color: If set, the target image size will be enforced and the
            specified color will be used as a background color to pad the image.

        N(   R   R   R   t	   mat_colorR   (   R   R   R   R   R8   R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   Æ   s
    				c         C   s)  | j  \ } } |  j d  k	 rY |  j d  k	 rY t t |  j ƒ | t |  j ƒ | ƒ } n8 |  j d  k r~ t |  j ƒ | } n t |  j ƒ | } t t | | ƒ ƒ t t | | ƒ ƒ f } t | d | d d |  j	 ƒj
 | ƒ } |  j d  k	 r%t |  j |  j |  j d |  j ƒj
 | ƒ } n  | S(   Ni    i   R   R   (   R	   R   R!   R   t   minR   R   R   R   R   R   R8   R$   R   (   R   R   t	   cur_widtht
   cur_heightR   t   new_dimensions(    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   ×   s    )0N(	   R   R   R   R!   R   R    R(   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR7   À   s   t	   Thumbnailc           B   s/   e  Z d  Z d d d d d d „ Z d „  Z RS(   sğ   
    Resize the image for use as a thumbnail. Wraps ``ResizeToFill``,
    ``ResizeToFit``, and ``SmartResize``.

    Note: while it doesn't currently, in the future this processor may also
    sharpen based on the amount of reduction.

    c         C   s   | |  _  | |  _ | |  _ | rE | t k r< t d ƒ ‚ q‡ t } nB | d  k r‡ | d  k	 of | d  k	 } | r‡ | d  k r‡ d } q‡ n  | |  _ | |  _ d  S(   Ns3   You can't specify an anchor point if crop is False.t   auto(	   R   R   R   t   FalseR%   R   R!   R    R   (   R   R   R   R   R    R   (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR   ó   s    					c      	   C   sÃ   |  j  r’ |  j s |  j r, t d ƒ ‚ n  |  j d k rb t d |  j d |  j d |  j ƒ } q¶ t d |  j d |  j d |  j d |  j ƒ } n$ t d |  j d |  j d |  j ƒ } | j	 | ƒ S(   Ns7   You must provide both a width and height when cropping.R>   R   R   R   R   (
   R    R   R   R%   R   R"   R   R   R7   R   (   R   R   t	   processor(    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR     s    	'0$N(   R   R   R   R!   R   R   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyR=   é   s   N(   t   baseR    t   libR   t   objectR   R   R   R"   R$   R0   R7   R=   (    (    (    s:   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/processors/resize.pyt   <module>   s   >)