
     h                         d dl Z d dlZd dlZddlmZmZ ddlmZ 	  G d d          ZddZ		 ej
        j        Zn# e$ r dZY nw xY wddZdd	Zd
 Zd ZdS )    N   )Image
ImageColor)	deprecatec                   N   e Zd ZdZd!dZd Zd!dZd"dZd!dZd#dZ	d#d	Z
d$dZd%dZd#dZd!dZd#dZ	 d&dZd#dZ	 d'dddZd Zd Zd Z	 	 	 	 	 	 	 	 	 	 	 d(dZ	 	 	 	 	 	 	 	 	 	 	 d(dZ	 	 	 	 	 	 d)dZ	 	 	 	 	 	 d)dZ	 	 	 	 	 d*dZ	 	 	 	 	 	 	 	 	 d+dZ	 	 	 	 	 	 	 	 	 d+d ZdS ),	ImageDrawNc                 P   |                                  |j        r|                                 d}||j        }||j        k    r%|dk    r|j        dk    rd}nd}t	          |          |dk    r|j        | _        nd| _        || _        |j        | _        t          j	        
                    | j        |          | _
        || _        |dv r | j
                            d          | _        n| j
                            d	          | _        |d
v rd| _        n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   NRGBARGBr   zmode mismatchP)IF)1r   r   r   r   LF)loadreadonly_copymode
ValueErrorpalette_imageimr   coredrawdraw_inkinkfontmodefill)selfr   r   blendmsgs        I/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/PIL/ImageDraw.py__init__zImageDraw.__init__3   s    				; 	HHJJJ<7D27??v~~"'U"2"2% oo%3;;:DLLDL%JOODGU33		:y))!,,DHHy))"--DH'''DMMDM			    c                 \    | j         sddlm} |                                | _         | j         S )a=  
        Get the current default font.

        To set the default font for this ImageDraw instance::

            from PIL import ImageDraw, ImageFont
            draw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

        To set the default font for all future ImageDraw instances::

            from PIL import ImageDraw, ImageFont
            ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

        If the current default font is ``None``,
        it is initialized with ``ImageFont.load_default()``.

        :returns: An image font.r   )	ImageFont)font r'   load_default)r    r'   s     r#   getfontzImageDraw.getfont]   s;    $ y 	1######!..00DIyr%   c                 t   ||| j         r	| j        }n!| j        }n|t          |t                    rt	          j        || j                  }| j        r:t          |t          j	                  s | j                            || j
                  }| j                            |          }|t          |t                    rt	          j        || j                  }| j        r:t          |t          j	                  s | j                            || j
                  }| j                            |          }||fS N)r   r   
isinstancestrr   getcolorr   r   numbersNumberr   r   r   )r    r   r   s      r#   _getinkzImageDraw._getinkv   s#   ;4<y xhc3'' >$-c49==C< B
3(G(G B,//T[AACi((--dC(( @%.tTY??D< D
4(H(H D<00t{CCDy))$//Dyr%   r   c                 z    |                      |          \  }}| | j                            |||||           dS dS )zDraw an arc.N)r3   r   draw_arc)r    xystartendr   widthr   s          r#   arczImageDraw.arc   sG    LL&&	T?Ir5#sE::::: ?r%   c                     |                                  |                     |          \  }}||}|#| j                            ||j        |           dS dS )zDraw a bitmap.N)r   r3   r   draw_bitmapr   )r    r6   bitmapr   r   s        r#   r=   zImageDraw.bitmap   s\    LL&&	T;C?I!!"fi55555 ?r%   c                     |                      ||          \  }}|| j                            ||||d           |-||k    r)|dk    r%| j                            ||||d|           dS dS dS dS )zDraw a chord.Nr   r   )r3   r   
draw_chordr    r6   r7   r8   r   outliner9   r   s           r#   chordzImageDraw.chord   s    LL$//	TI  UCq999?sd{{uzzI  UCa????? ?{{zzr%   c                     |                      ||          \  }}|| j                            ||d           |+||k    r'|dk    r#| j                            ||d|           dS dS dS dS )zDraw an ellipse.Nr   r   )r3   r   draw_ellipser    r6   r   rA   r9   r   s         r#   ellipsezImageDraw.ellipse   s    LL$//	TI""2tQ///?sd{{uzzI""2sAu55555 ?{{zzr%   r   c                    |                      |          d         }|5| j                            |           |dk    rdk    rt          d         t          t
          f          s*fdt          dt                    d          D             t          dt                    dz
            D ]}|         }d |dz
           |f||dz            ffD             }|d         |d         k    rBfd	}	|d         |d         k    o|d         d
z
  |d         k    p&|d         |d         k     o|d         d
z   |d         k    }
|d         dz  z
  dz   |d         dz  z
  dz   f|d         dz  z   dz
  |d         dz  z   dz
  fg}|
r|d         dz   |d         dz   }}n|d         dz
  |d         dz
  }}|                     ||dz
  |dz
  |           dk    rq|
r, |	||d         dz             | |	||d         dz             g}n+ |	||d         dz
            | |	||d         dz
            g}| 	                    ||d           dS dS dS dS )z6Draw a line, or a connected sequence of line segments.r   Ncurve   c                 D    g | ]}t          ||d z                      S )   )tuple).0ir6   s     r#   
<listcomp>z"ImageDraw.line.<locals>.<listcomp>   s-    MMM1%1q1u9..MMMr%   rK   r   c           
          g | ]M\  }}t          j        t          j        |d          |d          z
  |d         |d         z
                      dz  NS )r   r   h  )mathdegreesatan2)rM   r7   r8   s      r#   rO   z"ImageDraw.line.<locals>.<listcomp>   sg        'E3 TZAq0A58cRSfCT%U%UVV  r%   c                    | \  }}|dz  }dz  dz
  }t          d ||t          j        t          j        |                    z  f||t          j        t          j        |                    z  ffD                       S )NZ   rK   r   c              3   ~   K   | ]8\  }}||d k    rt          j        |          nt          j        |          z   V  9dS r   N)rR   floorceil)rM   pp_ds      r#   	<genexpr>z9ImageDraw.line.<locals>.coord_at_angle.<locals>.<genexpr>   s[       % % &3 C!GGC3P% % % % % %r%   )rL   rR   cosradianssin)coordanglexydistancer9   s        r#   coord_at_anglez&ImageDraw.line.<locals>.coord_at_angle   s    $1#(19q=$ % % "#HtxU8K8K/L/L$L M!"HtxU8K8K/L/L$L M+% % %     r%      rV         )r9   )
r3   r   
draw_linesr.   listrL   rangelenpiesliceline)r    r6   r   r9   jointr   rN   pointanglesrf   flippedcoordsr7   r8   
gap_coordss    ` `           r#   ro   zImageDraw.line   s   ll4  #?I  S%000EAII!"Q%$77 NMMMMaR!8L8LMMMBq#b''A+.. 2= 2=AqEE  -/q1uIu+=r!a%y?Q*R  F
 ayF1I-- 
 
 
 
 
 q	F1I-M&)c/F1I2MQ )fQi/OF1IOfQi4O  qEAI-158eai3G!3KLqEAI-158eai3G!3KLF  F&,Qi"nfQi"ns&,Qi"nfQi"nsMM&%"*cBhEEEqyy"  .ufQi"n E E % .ufQi"n E E*JJ !/ufQi"n E E % .ufQi"n E E*J
 		*d!	<<<o ?II2= 2=r%   c                     |                                  |                     ||          \  }}|| j                            ||d           |$||k    r | j                            ||d           dS dS dS )z(Experimental) Draw a shape.Nr   r   )closer3   r   draw_outline)r    shaper   rA   r   s        r#   ry   zImageDraw.shape   s~    LL$//	TI""5$222?sd{{I""5#q11111 ?{{r%   c                     |                      ||          \  }}|| j                            ||||d           |-||k    r)|dk    r%| j                            ||||d|           dS dS dS dS )zDraw a pieslice.Nr   r   )r3   r   draw_pieslicer@   s           r#   rn   zImageDraw.pieslice   s    LL$//	TI##BsD!<<<?sd{{uzzI##BsCEBBBBB ?{{zzr%   c                 t    |                      |          \  }}|| j                            ||           dS dS )z#Draw one or more individual pixels.N)r3   r   draw_points)r    r6   r   r   s       r#   rq   zImageDraw.point   sA    LL&&	T?I!!"c***** ?r%   c                    |                      ||          \  }}|| j                            ||d           |||k    r|dk    r|dk    r| j                            ||d|           dS t          j        d| j        j                  }|                      d          d         }|                                }t          |          }	|	j                            ||d           |                                }
t          |
          }	|dz  dz
  }|	j                            ||d|           |	                    |
|           t          j        | j
        | j        j                  }t          |          }	|	j                            ||d|           | j        	                    |j        d|j        z   |j                   dS dS dS dS )zDraw a polygon.Nr   r   r   rK   )mask)r   r   )r3   r   draw_polygonr   newr   sizecopyDrawpaster   )r    r6   r   rA   r9   r   r   mask_inkfill_imr   ink_imr   s               r#   polygonzImageDraw.polygon   s   LL$//	TI""2tQ///?sd{{uzzzz	&&r3599999 ydgl33<<??1-))++G}}	&&r8Q777F||	A	&&r8Q>>>

6
000Yty$',77Bxx	&&r35999beVbg%5tw?????/ ?{{zzr%   c                 V    t          |||          }|                     |||           dS )zDraw a regular polygon.N)!_compute_regular_polygon_verticesr   )r    bounding_circlen_sidesrotationr   rA   r6   s          r#   regular_polygonzImageDraw.regular_polygon  s1     /RRRw'''''r%   c                     |                      ||          \  }}|| j                            ||d           |+||k    r'|dk    r#| j                            ||d|           dS dS dS dS )zDraw a rectangle.Nr   r   )r3   r   draw_rectanglerE   s         r#   	rectanglezImageDraw.rectangle"  s    LL$//	TI$$Rq111?sd{{uzzI$$Ra77777 ?{{zzr%   )cornersc                    t          |d         t          t          f          r|\  \  \  n|\  k     rd}t          |          k     rd}t          |          d|dz  d\  t	                    r<z
  k    rz
  z
  k    rz
  rr                     ||          S dk    st                    s                     ||          S dz  }                     |          \   fd}	  |	d	           r- j	        
                    |z   d
z   |z
  d
z
  fd
           n, j	        
                    |z   d
z   |z
  d
z
  fd
           ss|z   g}
d         r|
d
xx         |d
z   z  cc<   d         r|
dxx         |d
z   z  cc<    j	        
                    |
d
           |z
  g}d
         r|d
xx         |d
z   z  cc<   d         r|dxx         |d
z   z  cc<    j	        
                    |d
           k    rdk    r |	d           sz   d
z
  g}d         r|dxx         |d
z   z  cc<   d
         r|dxx         |d
z   z  cc<    j	        
                    |d
           z
  d
z   g}d         r|dxx         |d
z   z  cc<   d         r|dxx         |d
z   z  cc<    j	        
                    |d
           sĉz   d
z
  g}
d         r|
d
xx         |d
z   z  cc<   d         r|
dxx         |d
z   z  cc<    j	        
                    |
d
           z
  d
z   g}d
         r|d
xx         |d
z   z  cc<   d         r|dxx         |d
z   z  cc<    j	        
                    |d
           dS dS dS dS dS )zDraw a rounded rectangle.r   z&x1 must be greater than or equal to x0z&y1 must be greater than or equal to y0N)TTTTrK   )FFc           	         rz   z   fddfz
  z   fddff}nrz   z   fddfz
  z   fddff}nlg }t          z   z   fddfz
  z   fddfz
  z
  fddfz
  z   fddff          D ]"\  }}|         r|                    |           #|D ]-}| r 
j        j        |dfz      
j        j        |	fz     .d S )Nrg   rQ   r   rV     r   )	enumerateappendr   r{   r5   )rn   partsrN   partr   dr   full_xfull_yr   r    r9   x0x1y0y1s       r#   draw_cornersz1ImageDraw.rounded_rectangle.<locals>.draw_cornersT  s    + "b1fb1f-sC8"q&"q&"-q#6  + "b1fb1f-r371fb"b1f-sB7 (b"q&"q&13<q&"b"q&13<q&"q&"b11b9b1fb1fb12s;	    	+ 	+GAt qz +T*** ? ? ?+DI+ddAY.>@@@&DI&e)<>>>	? ?r%   Tr   ri   F)r.   rk   rL   r   allrF   anyr   r3   r   r   )r    r6   radiusr   rA   r9   r   r"   rr   leftrighttopbottomr   r   r   r   r   r   r   r   s   `  ` ``       @@@@@@@@r#   rounded_rectanglezImageDraw.rounded_rectangle*  sw    bedE]++ 	 !#HRhr22NBB77:CS//!77:CS//!?.GQJ%w<< 	>"r'\F G"r'\F G >& >||Bgu===66W6 >>"dGU;;;FLL$//	T	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?@ L T	(("b1fqj"b1fqj)I4QRSSSS	(("q&1*b"q&1*b)I4QRSSS 9& 9BQ+1: %GGGq1u$GGG1: %GGGq1u$GGG	((tQ777aR,1: &!HHHA%HHH1: &!HHHA%HHH	((a888?sd{{uzzL 92r2:>21: $FFFa!eOFFF1: $FFFa!eOFFF	((c1555b5j1nb"51: '1IIIQ&III1: '1IIIQ&III	((a888 8BU
Q31: %GGGq1u$GGG1: %GGGq1u$GGG	((sA666eaR41: &!HHHA%HHH1: &!HHHA%HHH	((Q77777= ?{{zz"8 8r%   c                 <    t          |t                    rdnd}||v S N
   
)r.   r/   r    textsplit_characters      r#   _multiline_checkzImageDraw._multiline_check  s%    ",T3"7"7B$$U$&&r%   c                 ^    t          |t                    rdnd}|                    |          S r   )r.   r/   splitr   s      r#   _multiline_splitzImageDraw._multiline_split  s,    ",T3"7"7B$$Uzz/***r%   c                     t          j                    5  t          j        dt                     |                     d||          d         |z   	 cd d d            S # 1 swxY w Y   d S )NignorecategoryA)r(   stroke_widthr   )warningscatch_warningsfilterwarningsDeprecationWarningtextsize)r    r(   spacingr   s       r#   _multiline_spacingzImageDraw._multiline_spacing  s    $&& 		 		#H7IJJJJ!-    	
 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   =AA"%A"rI   r   Fc                    	
                                 r!                     |||	
||          S r j        dvrd}t          |                                            fd}d	
 fd	} ||          }|<d}|r| ||          n|}| |||            ||d           dS  ||           dS dS )z
Draw text.r   r
   3Embedded color supported only in RGB and RGBA modesNc                 @                         |           \  }} || S |S r-   )r3   )r   r   r    s     r#   getinkzImageDraw.text.<locals>.getink  s'    T**IC{Jr%   r   c                    j         }|dk    rrd}g }g }t          d          D ]]}|                    t          |                              |                    t	          j        |                   d                    ^	  j        |gR || |d\  }}|d         |d         z   |d         |d         z   f}nQ# t          $ rD 	  j        ||| gR d|i}n%# t          $ r                               }Y nw xY wY nw xY w|r"|d         |d         z   |d         |d         z   f}|dk    rv||
                    d          }}	|	                    d| dz	  d	z             |\  }
}j                            |	|
||
|j        d         z   ||j        d         z   f|           d S j                            |||            d S )
Nr   r
   rK   )	directionfeatureslanguager   anchorr   r7   r   r7   ri         )r   rl   r   intrR   modfgetmask2AttributeErrorgetmask	TypeErrorgetbandfillbandr   r   r   r   r<   )r   r   stroke_offsetr   ra   r7   rN   r   offsetcolorrc   rd   r   argsr   embedded_colorr   r(   kwargsr   r    r   r6   s               r#   	draw_textz!ImageDraw.text.<locals>.draw_text  s   =Dq  ^ EE1XX 2 2SAZZ(((TYr!u--a01111.,t}      (%%!-!       f a6!9,eAh.BB! . . ..'4<!  $    $ ! DD ! . . .<<--DDD.."  Qa=#33U1Xa@P5PPv~~ #DLLOOtq3"9"45551eaA	!,<a$)A,>N%OQUVVVVV	%%eT377777s6   ?C 
DC+*D+D
DDDDrX   )r   multiline_textr   r   r+   )r    r6   r   r   r(   r   r   alignr   r   r   r   stroke_fillr   r   r   r"   r   r   r   
stroke_inks   ``` ``  ```  ```     r#   r   zImageDraw.text  s   &   && 	&&     	"di>>GCS//!<<<>>D	 	 	 	 	3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8 3	8j fTll?J U4?4KVVK000QT
%	*l333 	#q!!!!! 	# ?r%   c                    |dk    rd}t          |          |d}n?t          |          dk    rd}t          |          |d         dv rd}t          |          g }d	}|                     |          }|                     |||          }|D ]A}|                     ||||	|

          }|                    |           t          ||          }B|d         }|d         dk    r|t          |          dz
  |z  dz  z  }n$|d         dk    r|t          |          dz
  |z  z  }t          |          D ]\  }}|d	         }|||         z
  }|d	         dk    r	||dz  z  }n|d	         dk    r||z  }|dk    rn,|dk    r	||dz  z  }n|dk    r||z  }nd}t          |          |                     ||f||||||	|
|||           ||z  }d S )Nttb/ttb direction is unsupported for multiline textlarK   #anchor must be a 2 character stringr   tb'anchor not supported for multiline textr   r   r   r   m       @r   r   r   centerr   )align must be "left", "center" or "right")r   r   r   r   r   r   )	r   rm   r   r   
textlengthr   maxr   r   )r    r6   r   r   r(   r   r   r   r   r   r   r   r   r   r"   widths	max_widthlinesline_spacingro   
line_widthr   idxr   width_differences                            r#   r   zImageDraw.multiline_text6  sv     CCS//!>FF[[A7CS//!AY$;CS//!	%%d++..tWlKK 	3 	3Ddi(X )  J MM*%%%Iz22IIe!9CJJNl2S88CCAY#CJJNl22C"5)) "	  "	 ICa5D(6#;6 ayC(3..c!!(( (""(3..'!!((A oo%IIs#!!)'-     <CCE"	  "	 r%   c           
         t          ddd           |                     |          rat          j                    5  t          j        dt
                     |                     |||||||          cddd           S # 1 swxY w Y   ||                                 }t          j                    5  t          j        dt
                     |                    |||||          cddd           S # 1 swxY w Y   dS )z*Get the size of a given string, in pixels.r   
   ztextbbox or textlengthr   r   N)	r   r   r   r   r   r   multiline_textsizer+   getsize)r    r   r(   r   r   r   r   r   s           r#   r   zImageDraw.textsize  s    	*b":;;;  && 	(** 
 
';MNNNN..  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 <<<>>D$&& 	 	#H7IJJJJ<< 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s#   6A==BB14C22C69C6c                    t          ddd           d}|                     |          }	|                     |||          }
t          j                    5  t          j        dt                     |	D ]0}|                     |||||||          \  }}t          ||          }1	 d d d            n# 1 swxY w Y   |t          |	          |
z  |z
  fS )Nr   r   multiline_textbboxr   r   r   )
r   r   r   r   r   r   r   r   r   rm   )r    r   r(   r   r   r   r   r   r   r   r   ro   r   line_heights                 r#   r   zImageDraw.multiline_textsize  s3    	&,@AAA	%%d++..tWlKK$&& 	7 	7#H7IJJJJ 
7 
7*.-- + +'
K  	:66		
7	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 #e**|3g===s   AB//B36B3c           	      ,   |                      |          rd}t          |          |r| j        dvrd}t          |          ||                                 }|rdn| j        }	 |                    |||||          S # t          $ r t          dd           t          j	                    5  t          j
        dt          	           |                     |||||
          }	ddd           n# 1 swxY w Y   |dk    r
|	d         cY S |	d         cY S w xY w)z@Get the length of a given string, in pixels with 1/64 precision.z&can't measure length of multiline textr   r   Nr
   z.textlength support for fonts without getlengthr   r   r   r   r   r   r   )r   r   r   r+   r   	getlengthr   r   r   r   r   r   r   )
r    r   r(   r   r   r   r   r"   r   r   s
             r#   r   zImageDraw.textlength  s      && 	":CS//! 	"di>>GCS//!<<<>>D':vvT]	>>$i8LLL 	 	 	FKKK(**  ';MNNNN}}'%% %                 E!!Aw7NNN	s<   %A> >-D+6C-!D-C1	1D4C1	5DDDc                    |r| j         dvrd}t          |          |                     |          r|                     |||||||||	|
|          S ||                                 }|rdn| j        }|                    |||||	|
|          }|d         |d         z   |d         |d         z   |d         |d         z   |d         |d         z   fS )	z2Get the bounding box of a given string, in pixels.r   r   Nr
   r   r   rK   ri   )r   r   r   r   r+   r   getbbox)r    r6   r   r(   r   r   r   r   r   r   r   r   r"   r   bboxs                  r#   textbboxzImageDraw.textbbox  s      	"di>>GCS//!  && 	**   <<<>>D':vvT]||$	8X|V
 
 AwAQ"Q%a2a5$q'BqE/QQr%   c                    |dk    rd}t          |          |d}n?t          |          dk    rd}t          |          |d         dv rd}t          |          g }d	}|                     |          }|                     |||
          }|D ]B}|                     |||||	|
          }|                    |           t          ||          }C|d         }|d         dk    r|t          |          dz
  |z  dz  z  }n$|d         dk    r|t          |          dz
  |z  z  }d }t          |          D ]
\  }}|d	         }|||         z
  }|d	         dk    r	||dz  z  }n|d	         dk    r||z  }|dk    rn,|dk    r	||dz  z  }n|dk    r||z  }nd}t          |          |                     ||f||||||	|
|	  	        }||}nnt          |d	         |d	                   t          |d         |d                   t          |d         |d                   t          |d         |d                   f}||z  }||d	         |d         |d	         |d         fS |S )Nr   r   r   rK   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   ri   )
r   rm   r   r   r   r   r   r   r  min)r    r6   r   r(   r   r   r   r   r   r   r   r   r"   r   r   r   r   ro   r   r   r  r   r   r   	bbox_lines                            r#   r   zImageDraw.multiline_textbbox  s    CCS//!>FF[[A7CS//!AY$;CS//!	%%d++..tWlKK 
	3 
	3D#!!- )  J MM*%%%Iz22IIe!9CJJNl2S88CCAY#CJJNl22C"5)) *	  *	 ICa5D(6#;6 ayC(3..c!!(( (""(3..'!!((A oo%s#!!)- & 
 
I |  Q1..Q1..Q1..Q1..	 <CC<a5"Q%A1--r%   r-   )Nr   )NNr   )Nr   NNN)r   NN)r   NNr   )NNNrI   r   NNNr   NF)NrI   NNNr   )NNNNF)	NNrI   r   NNNr   F)__name__
__module____qualname__r(   r$   r+   r3   r:   r=   rB   rF   ro   ry   rn   rq   r   r   r   r   r   r   r   r   r   r   r   r   r  r    r%   r#   r   r   0   s       D( ( ( (T  2   *; ; ; ;6 6 6 6@ @ @ @6 6 6 6:= := := :=x2 2 2 2C C C C+ + + +@ @ @ @> HL( ( ( (8 8 8 8 <=}8IM}8 }8 }8 }8 }8~' ' '
+ + +
  " t t t tt P  P  P  P j # # # #P > > > >@ # # # #R (R (R (R (R\ a a a a a ar%   r   c                 n    	 |                      |          S # t          $ r t          | |          cY S w xY w)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.
    )getdrawr   r   )r   r   s     r#   r   r   }  sJ    #zz$ # # #T"""""#s    44c                     d}|rd|v r	 ddl m} n# t          $ r Y nw xY w|ddl m} | r|                    |           } | |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.
    Nnicestr   )_imagingagg)
ImageDraw2)r)   r  ImportErrorr  r   )r   hintshandlers      r#   r  r    s     G H%%	0000000 	 	 	D	++++++	 \\"w;s    
c                 ~   |                                  }|\  }}	 |||f         }t          ||          |k    rdS ||||f<   n# t          t          f$ r Y dS w xY w||fh}	t	                      }
|	rt	                      }|	D ]\  }}|dz   |f|dz
  |f||dz   f||dz
  ffD ]\  }}||f|
v s|dk     s|dk     r	 |||f         }|
                    ||f           |t          ||          |k    }n||k    o||k    }|r||||f<   |                    ||f           ~# t          t          f$ r Y w xY w|	}
|}	|	dS 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). 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.
    Nr   r   )r   _color_diffr   
IndexErrorsetadd)imager6   valueborderthreshpixelrc   rd   
backgroundedge	full_edgenew_edgestr[   r   s                   r#   	floodfillr&    s   & JJLLEDAq1a4[
uj))V33Fad
#   F8D I
 55 	- 	-DAqa%a!eQZ!QUaQZH - -1q6Y&&!a%%1q55-adA MM1a&)))~*1j99VC Ez9a6k -&+ad aV,,, #J/   D-" 	+      s(   A A AA8
DD10D1c                   	 t          |t                    sd}t          |          |dk     rd}t          |          t          | t          t
          f          sd}t          |          t          |           dk    r| ^ 	n*t          |           dk    r| \  	nd}t          |          t          d g 	R D                       sd}t          |          t                    dk    sd	}t          |          	d
k    rd}t          |          t          |t          t          f          sd}t          |          d fdd } |||          }	fd|D             S )u  
    Generate a list of vertices for a 2D regular polygon.

    :param bounding_circle: The bounding circle is a tuple defined
        by a point and radius. The polygon is inscribed in this circle.
        (e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)
    :param n_sides: Number of sides
        (e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)
    :param rotation: Apply an arbitrary rotation to the polygon
        (e.g. ``rotation=90``, applies a 90 degree rotation)
    :return: List of regular polygon vertices
        (e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)

    How are the vertices computed?
    1. Compute the following variables
        - theta: Angle between the apothem & the nearest polygon vertex
        - side_length: Length of each polygon edge
        - centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)
        - polygon_radius: Polygon radius (last element of bounding_circle)
        - angles: Location of each polygon vertex in polar grid
            (e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])

    2. For each angle in angles, get the polygon vertex at that angle
        The vertex is computed using the equation below.
            X= xcos(φ) + ysin(φ)
            Y= −xsin(φ) + ycos(φ)

        Note:
            φ = angle in degrees
            x = 0
            y = polygon_radius

        The formula above assumes rotation around the origin.
        In our case, we are rotating around the centroid.
        To account for this, we use the formula below
            X = xcos(φ) + ysin(φ) + centroid_x
            Y = −xsin(φ) + ycos(φ) + centroid_y
    zn_sides should be an intri   zn_sides should be an int > 2z!bounding_circle should be a tuplerK   z[bounding_circle should contain 2D coordinates and a radius (e.g. (x, y, r) or ((x, y), r) )c              3   N   K   | ] }t          |t          t          f          V  !d S r-   )r.   r   float)rM   rN   s     r#   r]   z4_compute_regular_polygon_vertices.<locals>.<genexpr>%  s0      PPqz!c5\**PPPPPPr%   z0bounding_circle should only contain numeric datazBbounding_circle centre should contain 2D coordinates (e.g. (x, y))r   z$bounding_circle radius should be > 0z"rotation should be an int or floatc                    t          | d         t          j        t          j        d|z
                      z  | d         t          j        t          j        d|z
                      z  z
  |d         z   d          t          | d         t          j        t          j        d|z
                      z  | d         t          j        t          j        d|z
                      z  z   |d         z   d          fS )Nr   rQ   r   rK   )roundrR   r^   r_   r`   )rq   rS   centroids      r#   _apply_rotationz:_compute_regular_polygon_vertices.<locals>._apply_rotation7  s    a48DLw$?$?@@@(TXdl3=&A&ABBBC1+ 	  a48DLw$?$?@@@(TXdl3=&A&ABBBC1+ 	 
 	
r%   c                 &    |dg} |||           S Nr   r  )r,  polygon_radiusrb   start_pointr-  s       r#   _compute_polygon_vertexzB_compute_regular_polygon_vertices.<locals>._compute_polygon_vertexG  s!    %q){E8<<<r%   c                     g }d| z  }dd|z  z
  |z   }t          d|           D ]'}|                    |           ||z  }|dk    r|dz  }(|S )NrQ   r   g      ?r   )rl   r   )r   r   rr   rS   current_angle_s         r#   _get_anglesz6_compute_regular_polygon_vertices.<locals>._get_anglesK  ss    -sW},8q'"" 	% 	%AMM-(((W$Ms""$r%   c                 *    g | ]} |          S r  r  )rM   rb   r2  r,  r0  s     r#   rO   z5_compute_regular_polygon_vertices.<locals>.<listcomp>[  s6       EJ.%@@  r%   )	r.   r   r   r   rk   rL   rm   r   r)  )
r   r   r   r"   r6  rr   r-  r2  r,  r0  s
         @@@@r#   r   r     s   R gs## (nn{{,oo oe}55 1nn
?q  $3!>>	_			"	"#2 ..< 	 ooPP4Oh4O4O4OPPPPP @oox==ARoo4oo he-- 2oo
 
 
 = = = = =
 
 
 [(++F     NT   r%   c           	           t          t                    r7t           fdt          dt	                              D                       S t           z
            S )zJ
    Uses 1-norm distance to calculate difference between two values.
    c              3   T   K   | ]"}t          |         |         z
            V  #d S r-   )abs)rM   rN   color1color2s     r#   r]   z_color_diff.<locals>.<genexpr>e  s8      MM!3vay6!9,--MMMMMMr%   r   )r.   rL   sumrl   rm   r:  )r;  r<  s   ``r#   r  r  `  s`     &%   $MMMMMuQF7L7LMMMMMM6F?###r%   r-   r  r/  )rR   r1   r   r)   r   r   
_deprecater   r   r   r   rA   Outliner   r  r&  r   r  r  r%   r#   <module>r@     s!  B            ! ! ! ! ! !J J J J J J J JZ# # # #$j GG   GGG   05 5 5 5px x xv$ $ $ $ $s   ; AA