ó
È4ec           @   sµ   d  d l  Z  d  d l Z d d l m Z m Z d d l m Z d e f d „  ƒ  YZ e	 d „ Z
 y e j j Z Wn e k
 r‰ e	 Z n Xe	 e	 d „ Z e	 d	 d
 „ Z d „  Z d S(   iÿÿÿÿNi   (   t   Imaget
   ImageColor(   t   isStringTypet	   ImageDrawc           B   s^  e  Z d d  „ Z d „  Z d d „ Z d d d „ Z d d „ Z d d d d „ Z d d d d „ Z	 d d d d „ Z
 d d d	 „ Z d d d d
 „ Z d d „ Z d d d „ Z d d d d „ Z d „  Z d „  Z d d d d d d d d d d d „
 Z d d d d d d d d d d d „
 Z d d d d d d d „ Z d d d d d d d „ Z RS(   c         C   sD  | j  ƒ  | j r  | j ƒ  n  d } | d k r> | j } n  | | j k r€ | d k rq | j d k rq d } q€ t d ƒ ‚ n  | d k r› | j |  _ n	 d |  _ | j |  _ t j	 j
 |  j | ƒ |  _
 | |  _ | d k rø |  j
 j d ƒ |  _ n |  j
 j d	 ƒ |  _ | d k r%d
 |  _ n	 d |  _ d |  _ d |  _ d S(   s˜  
        Create a drawing instance.

        :param im: The image to draw in.
        :param mode: Optional mode to use for color values.  For RGB
           images, this argument can be RGB or RGBA (to blend the
           drawing into the image).  For all other modes, this argument
           must be the same as the image mode.  If omitted, the mode
           defaults to the mode of the image.
        i    t   RGBAt   RGBi   s   mode mismatcht   Pt   It   Fiÿÿÿÿt   1t   LN(   R   R   (   R	   R   R   R   (   t   loadt   readonlyt   _copyt   Nonet   modet
   ValueErrort   palettet   imR    t   coret   drawt   draw_inkt   inkt   fontmodet   fillt   font(   t   selfR   R   t   blend(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   __init__1   s0    
						c         C   s2   |  j  s+ d d l m } | j ƒ  |  _  n  |  j  S(   sH   
        Get the current default font.

        :returns: An image font.i   (   t	   ImageFont(   R   t    R   t   load_default(   R   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   getfontZ   s    	c         C   s/  | d  k r9 | d  k r9 |  j r- |  j } q%|  j } nì | d  k	 r¯ t | ƒ ri t j | |  j ƒ } n  |  j rš t | t	 j
 ƒ rš |  j j | ƒ } n  |  j j | ƒ } n  | d  k	 r%t | ƒ rß t j | |  j ƒ } n  |  j rt | t	 j
 ƒ r|  j j | ƒ } n  |  j j | ƒ } n  | | f S(   N(   R   R   R   R   R   t   getcolorR   R   t
   isinstancet   numberst   NumberR   R   (   R   R   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   _getinkf   s"    	i    c         C   sD   |  j  | ƒ \ } } | d k	 r@ |  j j | | | | | ƒ n  d S(   s   Draw an arc.N(   R%   R   R   t   draw_arc(   R   t   xyt   startt   endR   t   widthR   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   arc{   s    c         C   s`   | j  ƒ  |  j | ƒ \ } } | d k r4 | } n  | d k	 r\ |  j j | | j | ƒ n  d S(   s   Draw a bitmap.N(   R   R%   R   R   t   draw_bitmapR   (   R   R'   t   bitmapR   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR-      s    
	c         C   s   |  j  | | ƒ \ } } | d k	 rC |  j j | | | | d ƒ n  | d k	 r} | | k r} |  j j | | | | d | ƒ n  d S(   s   Draw a chord.i   i    N(   R%   R   R   t
   draw_chord(   R   R'   R(   R)   R   t   outlineR*   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   chordŠ   s
    c         C   su   |  j  | | ƒ \ } } | d k	 r= |  j j | | d ƒ n  | d k	 rq | | k rq |  j j | | d | ƒ n  d S(   s   Draw an ellipse.i   i    N(   R%   R   R   t   draw_ellipse(   R   R'   R   R/   R*   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   ellipse’   s
    c      	      sÁ  |  j  | ƒ d } | d k	 r½|  j j | | ˆ  ƒ | d k r½ˆ  d k r½xjt d t | ƒ d ƒ D]L} | | } g  | | d | f | | | d f f D]C \ } }	 t j t j |	 d | d | d |	 d ƒ ƒ d ^ q  }
 |
 d |
 d k rqg n  ‡  f d †  } |
 d |
 d k r>|
 d d |
 d k pg|
 d |
 d k  og|
 d d |
 d k } | d ˆ  d d | d ˆ  d d f | d ˆ  d d | d ˆ  d d f g } | rè|
 d d	 |
 d d	 } }	 n |
 d d	 |
 d d	 } }	 |  j	 | | d	 |	 d	 | ƒ ˆ  d
 k rg | ri| | |
 d d	 ƒ | | | |
 d d	 ƒ g } n1 | | |
 d d	 ƒ | | | |
 d d	 ƒ g } |  j
 | | d d ƒqg qg Wq½n  d S(   s6   Draw a line, or a connected sequence of line segments.i    t   curvei   i   ih  c            s­   |  \ } } | d 8} ˆ  d d } t  g  | | t j t j | ƒ ƒ f | | t j t j | ƒ ƒ f f D]: \ } } | | d k r– t j | ƒ n t j | ƒ ^ ql ƒ S(   NiZ   i   i   i    (   t   tuplet   matht   cost   radianst   sint   floort   ceil(   t   coordt   anglet   xt   yt   distancet   pt   p_d(   R*   (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   coord_at_angle«   s    
i´   i   iZ   i   R*   i   N(   R%   R   R   t
   draw_linest   ranget   lenR5   t   degreest   atan2t   pieslicet   line(   R   R'   R   R*   t   jointR   t   it   pointR(   R)   t   anglesRB   t   flippedt   coordst	   gapCoords(    (   R*   s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRI   š   s8     
o,,)/ c         C   s|   | j  ƒ  |  j | | ƒ \ } } | d k	 rG |  j j | | d ƒ n  | d k	 rx | | k rx |  j j | | d ƒ n  d S(   s   (Experimental) Draw a shape.i   i    N(   t   closeR%   R   R   t   draw_outline(   R   t   shapeR   R/   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRS   Ö   s    
c         C   s   |  j  | | ƒ \ } } | d k	 rC |  j j | | | | d ƒ n  | d k	 r} | | k r} |  j j | | | | d | ƒ n  d S(   s   Draw a pieslice.i   i    N(   R%   R   R   t   draw_pieslice(   R   R'   R(   R)   R   R/   R*   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRH   ß   s
    c         C   s;   |  j  | ƒ \ } } | d k	 r7 |  j j | | ƒ n  d S(   s#   Draw one or more individual pixels.N(   R%   R   R   t   draw_points(   R   R'   R   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRL   ç   s    c         C   sr   |  j  | | ƒ \ } } | d k	 r= |  j j | | d ƒ n  | d k	 rn | | k rn |  j j | | d ƒ n  d S(   s   Draw a polygon.i   i    N(   R%   R   R   t   draw_polygon(   R   R'   R   R/   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   polygoní   s
    c         C   su   |  j  | | ƒ \ } } | d k	 r= |  j j | | d ƒ n  | d k	 rq | | k rq |  j j | | d | ƒ n  d S(   s   Draw a rectangle.i   i    N(   R%   R   R   t   draw_rectangle(   R   R'   R   R/   R*   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt	   rectangleõ   s
    c         C   s%   t  | t ƒ r d n d } | | k S(   s
   Draw text.s   
(   R"   t   str(   R   t   textt   split_character(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   _multiline_checký   s    c         C   s(   t  | t ƒ r d n d } | j | ƒ S(   Ns   
(   R"   RZ   t   split(   R   R[   R\   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   _multiline_split  s    i   t   leftc            s  ˆ j  ˆ ƒ r= ˆ j ˆ ˆ | ˆ | | | ˆ ˆ ˆ | | ƒ Sˆ d  k rX ˆ j ƒ  ‰ n  ‡ f d †  } d d  ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f	 d † } | | ƒ } | d  k	 rd  } | rÙ | d  k	 rÐ | | ƒ n | } n  | d  k	 r| | | ƒ | | d | | f ƒ q| | ƒ n  d  S(   Nc            s)   ˆ  j  |  ƒ \ } }  | d  k r% |  S| S(   N(   R%   R   (   R   R   (   R   (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   getink,  s    i    c            s  ˆ } ya ˆ j  ˆ ˆ j d ˆ d ˆ d ˆ d | ˆ  ˆ Ž\ } } | d | d | d | d f } Wn_ t k
 rÈ y+ ˆ j ˆ ˆ j ˆ ˆ ˆ | ˆ  ˆ Ž } WqÉ t k
 rÄ ˆ j ˆ ƒ } qÉ Xn X| rö | d | d | d | d f } n  ˆ j j | | |  ƒ d  S(   Nt	   directiont   featurest   languaget   stroke_widthi    i   (   t   getmask2R   t   AttributeErrort   getmaskt	   TypeErrorR   R,   (   R   Re   t   stroke_offsetR;   t   maskt   offset(	   t   argsRb   Rc   R   t   kwargsRd   R   R[   R'   (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt	   draw_text2  s8    	('(   R]   t   multiline_textR   R    (   R   R'   R[   R   R   t   anchort   spacingt   alignRb   Rc   Rd   Re   t   stroke_fillRm   Rn   Ra   Ro   R   t
   stroke_ink(    (	   Rm   Rb   Rc   R   Rn   Rd   R   R[   R'   s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR[     s6    - !c         C   sz  g  } d } |  j  | ƒ } |  j d d | d | ƒd | } xZ | D]R } |  j | | d | d |	 d |
 d | ƒ\ } } | j | ƒ t | | ƒ } qE W| \ } } xÌ t | ƒ D]¾ \ } } | d	 k rÏ nR | d
 k rô | | | | d 7} n- | d k r| | | | 7} n t d ƒ ‚ |  j | | f | | | | d | d |	 d |
 d | d | ƒ| | 7} | d } q´ Wd  S(   Ni    t   AR   Re   i   Rb   Rc   Rd   R`   t   centerg       @t   rights)   align must be "left", "center" or "right"Rt   (   R_   t   textsizet   appendt   maxt	   enumerateR   R[   (   R   R'   R[   R   R   Rq   Rr   Rs   Rb   Rc   Rd   Re   Rt   t   widthst	   max_widtht   linest   line_spacingRI   t
   line_widtht   line_heightR`   t   topt   idx(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRp   b  sH    #	
c         C   sb   |  j  | ƒ r. |  j | | | | | | | ƒ S| d k rI |  j ƒ  } n  | j | | | | | ƒ S(   s*   Get the size of a given string, in pixels.N(   R]   t   multiline_textsizeR   R    t   getsize(   R   R[   R   Rr   Rb   Rc   Rd   Re   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyRy   ›  s    c      	   C   s—   d } |  j  | ƒ }	 |  j d d | d | ƒd | }
 xD |	 D]< } |  j | | | | | | | ƒ \ } } t | | ƒ } q? W| t |	 ƒ |
 | f S(   Ni    Rv   R   Re   i   (   R_   Ry   R{   RE   (   R   R[   R   Rr   Rb   Rc   Rd   Re   R~   R   R€   RI   R   R‚   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR…   ¯  s    
#!N(   t   __name__t
   __module__R   R   R    R%   R+   R-   R0   R2   RI   RS   RH   RL   RW   RY   R]   R_   R[   Rp   Ry   R…   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR   0   s^   )		<				Q/c         C   s3   y |  j  | ƒ SWn t k
 r. t |  | ƒ SXd S(   s‹  
    A simple 2D drawing interface for PIL images.

    :param im: The image to draw in.
    :param mode: Optional mode to use for color values.  For RGB
       images, this argument can be RGB or RGBA (to blend the
       drawing into the image).  For all other modes, this argument
       must be the same as the image mode.  If omitted, the mode
       defaults to the mode of the image.
    N(   t   getdrawRg   R   (   R   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   DrawÆ  s    c         C   s…   d } | s d | k rD y d d l m } WqD t k
 r@ qD Xn  | d k rc d d l m } n  |  r{ | j |  ƒ }  n  |  | f S(   s  
    (Experimental) A more advanced 2D drawing interface for PIL images,
    based on the WCK interface.

    :param im: The image to draw in.
    :param hints: An optional list of hints.
    :returns: A (drawing context, drawing resource factory) tuple.
    t   nicesti   (   t   _imagingagg(   t
   ImageDraw2N(   R   R   RŒ   t   ImportErrorR   RŠ   (   R   t   hintst   handler(    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR‰   Þ  s    i    c         C   sé  |  j  ƒ  } | \ } } y= | | | f } t | | ƒ | k rD d S| | | | f <Wn t t f k
 ro d SX| | f h }	 t ƒ  }
 xZ|	 rät ƒ  } x8|	 D]0\ } } x!| d | f | d | f | | d f | | d f f D]å \ } } | | f |
 k sè | d k  sè | d k  r$qè n  y | | | f } Wn t t f k
 rQqè X|
 j | | f ƒ | d k r‰t | | ƒ | k } n | | k ož| | k } | rè | | | | f <| j | | f ƒ qè qè Wq¡ W|	 }
 | }	 q‹ Wd S(   sÏ  
    (experimental) Fills a bounded region with a given color.

    :param image: Target image.
    :param xy: Seed position (a 2-item coordinate tuple). See
        :ref:`coordinate-system`.
    :param value: Fill color.
    :param border: Optional border value.  If given, the region consists of
        pixels with a color different from the border color.  If not given,
        the region consists of pixels having the same color as the seed
        pixel.
    :param thresh: Optional threshold value which specifies a maximum
        tolerable difference of a pixel value from the 'background' in
        order for it to be replaced. Useful for filling regions of
        non-homogeneous, but similar, colors.
    Ni   i    (   R   t   _color_diffR   t
   IndexErrort   sett   addR   (   t   imageR'   t   valuet   bordert   thresht   pixelR=   R>   t
   backgroundt   edget	   full_edget   new_edget   st   tR@   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt	   floodfillö  s<    			G*c         C   s_   t  | t ƒ rM t g  t d t | ƒ ƒ D] } t |  | | | ƒ ^ q( ƒ St |  | ƒ Sd S(   sJ   
    Uses 1-norm distance to calculate difference between two values.
    i    N(   R"   R4   t   sumRD   RE   t   abs(   t   color1t   color2RK   (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyR‘   .  s    >(   R5   R#   R   R    R   t   _utilR   t   objectR   R   RŠ   R   R/   t   OutlineRg   R‰   R    R‘   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.pyt   <module>!   s   ÿ —
8