3
`JZI5                 @   s   d dl Z ddlmZmZ ddlmZ G dd deZdddZyej	j
ZW n ek
rb   dZY nX dd	d
ZdddZdd ZdS )    N   )Image
ImageColor)isStringTypec               @   s   e Zd Zd+ddZdd Zd,ddZd-dd	Zd.d
dZd/ddZd0ddZ	d1ddZ
d2ddZd3ddZd4ddZd5ddZd6ddZdd Zdd  Zd7d!d"Zd8d%d&Zd9d'd(Zd:d)d*ZdS );	ImageDrawNc             C   s   |j   |jr|j  d}|dkr(|j}||jkrR|dkrJ|jdkrJd}ntd|dkrd|j| _nd| _|j| _tjj	| j|| _	|| _|dkr| j	j
d|| _n| j	j
d|| _|dkrd
| _nd| _d| _d| _dS )a  
        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.
        r   NZRGBAZRGBr   zmode mismatchPIF1L)r   r	   )r
   r   r   r	   )loadreadonlyZ_copymode
ValueErrorpaletteimr   coredrawdraw_inkinkfontmodefillfont)selfr   r   Zblend r   //usr/lib/python3/dist-packages/PIL/ImageDraw.py__init__0   s0    

zImageDraw.__init__c             C   s"   | j sddlm} |j | _ | j S )zH
        Get the current default font.

        :returns: An image font.r   )	ImageFont)r    r   Zload_default)r   r   r   r   r   getfontY   s    
zImageDraw.getfontc             C   s   |d kr&|d kr&| j r| j}q| j}n|d k	rtt|rDtj|| j}| jrdt|tj	 rd| jj|}| j
j|| j}|d k	rt|rtj|| j}| jrt|tj	 r| jj|}| j
j|| j}||fS )N)r   r   r   r   Zgetcolorr   r   
isinstancenumbersNumberr   r   )r   r   r   r   r   r   _getinkd   s"    zImageDraw._getinkc             C   s,   | j |\}}|dk	r(| jj|||| dS )zDraw an arc.N)r$   r   Zdraw_arc)r   xystartendr   r   r   r   r   arcy   s    zImageDraw.arcc             C   s@   |j   | j|\}}|dkr"|}|dk	r<| jj||j| dS )zDraw a bitmap.N)r   r$   r   draw_bitmapr   )r   r%   bitmapr   r   r   r   r   r*      s    zImageDraw.bitmapc             C   sL   | j ||\}}|dk	r,| jj||||d |dk	rH| jj||||d dS )zDraw a chord.Nr   r   )r$   r   Z
draw_chord)r   r%   r&   r'   r   outliner   r   r   r   chord   s
    zImageDraw.chordc             C   sD   | j ||\}}|dk	r(| jj||d |dk	r@| jj||d dS )zDraw an ellipse.Nr   r   )r$   r   Zdraw_ellipse)r   r%   r   r+   r   r   r   r   ellipse   s
    zImageDraw.ellipser   c             C   s*   | j |\}}|dk	r&| jj||| dS )z6Draw a line, or a connected sequence of line segments.N)r$   r   Z
draw_lines)r   r%   r   widthr   r   r   r   line   s    zImageDraw.linec             C   sL   |j   | j||\}}|dk	r0| jj||d |dk	rH| jj||d dS )z(Experimental) Draw a shape.Nr   r   )closer$   r   Zdraw_outline)r   shaper   r+   r   r   r   r   r1      s    zImageDraw.shapec             C   sL   | j ||\}}|dk	r,| jj||||d |dk	rH| jj||||d dS )zDraw a pieslice.Nr   r   )r$   r   Zdraw_pieslice)r   r%   r&   r'   r   r+   r   r   r   r   pieslice   s
    zImageDraw.pieslicec             C   s(   | j |\}}|dk	r$| jj|| dS )z#Draw one or more individual pixels.N)r$   r   Zdraw_points)r   r%   r   r   r   r   r   point   s    zImageDraw.pointc             C   sD   | j ||\}}|dk	r(| jj||d |dk	r@| jj||d dS )zDraw a polygon.Nr   r   )r$   r   Zdraw_polygon)r   r%   r   r+   r   r   r   r   polygon   s
    zImageDraw.polygonc             C   sD   | j ||\}}|dk	r(| jj||d |dk	r@| jj||d dS )zDraw a rectangle.Nr   r   )r$   r   Zdraw_rectangle)r   r%   r   r+   r   r   r   r   	rectangle   s
    zImageDraw.rectanglec             C   s   t |trdnd}||kS )z
Draw text.
   
)r!   str)r   textsplit_characterr   r   r   _multiline_check   s    zImageDraw._multiline_checkc             C   s   t |trdnd}|j|S )Nr6   r7   )r!   r8   split)r   r9   r:   r   r   r   _multiline_split   s    zImageDraw._multiline_splitc             O   s   | j |r$| j|||||f||S | j|\}}|d krB| j }|d krN|}|d k	ry>|j|| jf||\}	}
|d |
d  |d |
d  f}W nN tk
r   y|j|| jf||}	W n tk
r   |j|}	Y nX Y nX | j	j
||	| d S )Nr   r   )r;   multiline_textr$   r    Zgetmask2r   AttributeErrorZgetmask	TypeErrorr   r)   )r   r%   r9   r   r   anchorargskwargsr   maskoffsetr   r   r   r9      s$    

$zImageDraw.text   leftc
          
   C   s   g }
d}| j |}| jd|dd | }x0|D ](}| j||\}}|
j| t||}q.W |\}}xt|D ]\}}|dkr~nD|dkr|||
|  d 7 }n&|dkr|||
|  7 }nd	std
| j||f||||||	d ||7 }|d }qlW d S )Nr   A)r   r   rG   centerg       @rightFz)align must be "left", "center" or "right")	directionfeatures)r=   textsizeappendmax	enumerateAssertionErrorr9   )r   r%   r9   r   r   rA   spacingZalignrK   rL   Zwidths	max_widthlinesline_spacingr/   
line_widthline_heightrG   topidxr   r   r   r>      s*    



zImageDraw.multiline_textc             C   s:   | j |r| j|||||S |dkr,| j }|j|||S )z*Get the size of a given string, in pixels.N)r;   multiline_textsizer    getsize)r   r9   r   rR   rK   rL   r   r   r   rM      s    

zImageDraw.textsizec             C   sb   d}| j |}| jd|dd | }x,|D ]$}	| j|	||||\}
}t||
}q*W |t|| fS )Nr   rH   )r   r   )r=   rM   rO   len)r   r9   r   rR   rK   rL   rS   rT   rU   r/   rV   rW   r   r   r   rZ   	  s    


zImageDraw.multiline_textsize)N)N)N)N)NN)NN)Nr   )NN)NN)N)NN)NN)NNN)NNNrF   rG   NN)NrF   NN)NrF   NN)__name__
__module____qualname__r   r    r$   r(   r*   r,   r-   r/   r1   r2   r3   r4   r5   r;   r=   r9   r>   rM   rZ   r   r   r   r   r   .   s,   
)


	



	




 
 

 r   c             C   s*   y
| j |S  tk
r$   t| |S X dS )a  
    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)getdrawr?   r   )r   r   r   r   r   Draw  s    
ra   c             C   sb   d}| sd|kr8yddl m} W n tk
r6   Y nX |dkrLddl m} | rZ|j| } | |fS )a  
    (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.
    NZnicestr   )_imagingagg)
ImageDraw2)r   rb   ImportErrorrc   ra   )r   ZhintsZhandlerr   r   r   r`   -  s    
r`   c             C   s  | j  }|\}}y.|||f }t|||kr0dS ||||f< W n ttfk
rV   dS X ||fg}	|dkrx|	rg }
x|	D ]\}}x|d |f|d |f||d f||d ffD ]V\}}y|||f }W n tk
r   Y qX t|||kr||||f< |
j||f qW q~W |
}	qnW nx|	rg }
x|	D ]\}}x|d |f|d |f||d f||d ffD ]`\}}y|||f }W n tk
r   Y n0X ||krd||krd||||f< |
j||f qdW q.W |
}	qW dS )a  
    (experimental) Fills a bounded region with a given color.

    :param image: Target image.
    :param xy: Seed position (a 2-item coordinate tuple).
    :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.
    Nr   )r   _color_diffr   
IndexErrorrN   )Zimager%   valueZborderZthreshZpixelxyZ
backgroundZedgeZnewedgestpr   r   r   	floodfillE  sF    

6
6rm   c             C   s<   t | d |d  t | d |d   t | d |d   S )zN
    Uses 1-norm distance to calculate difference between two rgb values.
    r   r      )abs)Zrgb1Zrgb2r   r   r   re   ~  s    re   )N)NN)Nr   )r"   r   r   r   Z_utilr   objectr   ra   r   r+   ZOutliner?   r`   rm   re   r   r   r   r   <module>!   s   
 h



9