a
    Sic                      @   s   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 dZdddZ	zej
jZW n eyn   dZY n0 dd	d
ZdddZdd Zdd ZdS )    N   )Image
ImageColor)	deprecatec                   @   s   e Zd Zd9ddZdd Zd:ddZd;d	d
Zd<ddZd=ddZd>ddZ	d?ddZ
d@ddZdAddZdBddZdCddZdDddZdEddZdFd d!Zd"d# Zd$d% Zd&d' ZdGd+d,ZdHd-d.ZdId/d0ZdJd1d2ZdKd3d4ZdLd5d6ZdMd7d8ZdS )N	ImageDrawNc                 C   s   |   |jr|  d}|du 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|| _
|| _|dv r| j
d| _n| j
d	| _|d
v 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   NRGBARGBr   zmode mismatchP)IF)1r	   r
   r   r   L)loadreadonly_copymode
ValueErrorpalette_imageimr   coredrawdraw_inkinkfontmodefillfont)selfr   r   blend r    I/var/www/html/django/DPS/env/lib/python3.9/site-packages/PIL/ImageDraw.py__init__1   s2    

zImageDraw.__init__c                 C   s"   | j sddlm} | | _ | j S )zH
        Get the current default font.

        :returns: An image font.r   )	ImageFont)r    r#   load_default)r   r#   r    r    r!   getfont[   s    
zImageDraw.getfontc                 C   s   |d u r&|d u r&| j r| j}q| j}n|d urtt|trFt|| j}| jrht|tj	sh| j|| j
}| j|}|d urt|trt|| j}| jrt|tj	s| j|| j
}| j|}||fS N)r   r   
isinstancestrr   getcolorr   r   numbersNumberr   r   r   )r   r   r   r    r    r!   _getinkg   s"    

zImageDraw._getinkr   c                 C   s.   |  |\}}|dur*| j||||| dS )zDraw an arc.N)r-   r   draw_arc)r   xystartendr   widthr   r    r    r!   arc|   s    zImageDraw.arcc                 C   s@   |   | |\}}|du r"|}|dur<| j||j| dS )zDraw a bitmap.N)r   r-   r   draw_bitmapr   )r   r/   bitmapr   r   r    r    r!   r5      s    zImageDraw.bitmapc                 C   s^   |  ||\}}|dur,| j||||d |durZ||krZ|dkrZ| j||||d| dS )zDraw a chord.Nr   r   )r-   r   
draw_chordr   r/   r0   r1   r   outliner2   r   r    r    r!   chord   s
    zImageDraw.chordc                 C   sV   |  ||\}}|dur(| j||d |durR||krR|dkrR| j||d| dS )zDraw an ellipse.Nr   r   )r-   r   draw_ellipser   r/   r   r8   r2   r   r    r    r!   ellipse   s
    zImageDraw.ellipser   c                    s6  |  |d }|dur2| j|  |dkr2 dkr2td ttfslfddtdtdD tdtd D ]}| }d	d |d  |f||d  ffD }|d |d krq~ fd
d}	|d |d kr|d d |d kp|d |d k o|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  }}| ||d |d |  dkr~|
r|	||d d ||	||d d g}n&|	||d d ||	||d d g}| j	||dd q~dS )z6Draw a line, or a connected sequence of line segments.r   Ncurve   c                    s    g | ]}t  ||d   qS )   )tuple.0i)r/   r    r!   
<listcomp>       z"ImageDraw.line.<locals>.<listcomp>r?   r   c              
   S   s>   g | ]6\}}t t |d  |d   |d |d  d qS )r   r   h  )mathdegreesatan2)rB   r0   r1   r    r    r!   rD      s   (c              
      sZ   | \}}|d8 } d d }t dd ||tt| f||tt| ffD S )NZ   r?   r   c                 s   s2   | ]*\}}||d krt |nt | V  qdS )r   N)rG   floorceil)rB   pp_dr    r    r!   	<genexpr>   s   z9ImageDraw.line.<locals>.coord_at_angle.<locals>.<genexpr>)r@   rG   cosradianssin)coordanglexydistancer2   r    r!   coord_at_angle   s    z&ImageDraw.line.<locals>.coord_at_angle   rJ         rX   )
r-   r   
draw_linesr(   listr@   rangelenpiesliceline)r   r/   r   r2   Zjointr   rC   pointanglesrY   flippedcoordsr0   r1   Z
gap_coordsr    )r2   r/   r!   rb      sH    
"$&&zImageDraw.linec                 C   sT   |   | ||\}}|dur0| j||d |durP||krP| j||d dS )z(Experimental) Draw a shape.Nr   r   )closer-   r   draw_outline)r   shaper   r8   r   r    r    r!   ri      s    zImageDraw.shapec                 C   s^   |  ||\}}|dur,| j||||d |durZ||krZ|dkrZ| j||||d| dS )zDraw a pieslice.Nr   r   )r-   r   draw_pieslicer7   r    r    r!   ra      s
    zImageDraw.pieslicec                 C   s(   |  |\}}|dur$| j|| dS )z#Draw one or more individual pixels.N)r-   r   draw_points)r   r/   r   r   r    r    r!   rc      s    zImageDraw.pointc                 C   s&  |  ||\}}|dur(| j||d |dur"||kr"|dkr"|dkrb| j||d| ntd| jj}|  dd }| }t|}	|	j||d | }
t|
}	|d d }|	j||d| |j	|
|d t| j
| jj}t|}	|	j||d| | j	|jd|j |j dS )zDraw a polygon.Nr   r   r   r?   )mask)r   r   )r-   r   draw_polygonr   newr   sizecopyDrawpaster   )r   r/   r   r8   r2   r   rl   Zmask_inkZfill_imr   Zink_imr   r    r    r!   polygon   s(    zImageDraw.polygonc                 C   s   t |||}| ||| dS )zDraw a regular polygon.N)!_compute_regular_polygon_verticesrs   )r   bounding_circlen_sidesrotationr   r8   r/   r    r    r!   regular_polygon  s    zImageDraw.regular_polygonc                 C   sV   |  ||\}}|dur(| j||d |durR||krR|dkrR| j||d| dS )zDraw a rectangle.Nr   r   )r-   r   draw_rectangler;   r    r    r!   	rectangle  s
    zImageDraw.rectanglec                    s  t |d ttfr$|\\	\
n|\	
|d    krP   
	 krh
	  rr||S  dkr||S  d }|\ 	
fdd}dur|d rj	| d 
| d fd n(j| d 	| d 
fd ssj	| d | 
| d fd j| 	| d 
| d fd durkrdkr|d s*j| d 	| d 	 d fd j| d 
 d | d 
fd sj	| d  d 
| d fd j d 	| d 
| d fd dS )	zDraw a rounded rectangle.r   r?   c                    s  r:	  	  fddf
    
fddff}nrt	  	  fddf  		  fddff}nd  		  fddf  
  
fddf
    
fddf	  	  fddff}|D ]2}| rj j|df   q܈j j|f   qd S )NrZ   rF   r   rJ     r   )r   rj   r.   )ra   partspartdr   Zfull_xZfull_yr   r   r2   x0x1y0y1r    r!   draw_corners7  s"    z1ImageDraw.rounded_rectangle.<locals>.draw_cornersNTr   F)r(   r^   r@   r<   rz   r-   r   ry   )r   r/   radiusr   r8   r2   rr   r    r~   r!   rounded_rectangle  sR     
*(,,&&&&zImageDraw.rounded_rectanglec                 C   s   t |trdnd}||v S )z
Draw text.
   
)r(   r)   r   textZsplit_characterr    r    r!   _multiline_checkn  s    zImageDraw._multiline_checkc                 C   s   t |trdnd}||S )Nr   r   )r(   r)   splitr   r    r    r!   _multiline_splitt  s    zImageDraw._multiline_splitc                 C   sR   t  6 t jdtd | jd||dd | W  d    S 1 sD0    Y  d S )NignorecategoryA)r   stroke_widthr   )warningscatch_warningsfilterwarningsDeprecationWarningtextsize)r   r   spacingr   r    r    r!   _multiline_spacingy  s    
zImageDraw._multiline_spacingr>   leftFc                    s    	r,
	| ||||S rBjdvrBtdd u rR fdd}d 	
fdd	}||}|d urd }|r|d ur||n|}|d ur||| ||d n|| d S )	Nr   r   3Embedded color supported only in RGB and RGBA modesc                    s     | \}} |d u r| S |S r'   )r-   )r   r   )r   r    r!   getink  s    zImageDraw.text.<locals>.getinkr   c           	         sf  j }|dkrrd}
}zPj	|gR | | d\}}|d |d  |d |d  f}W nZ ty   z*j	|| | gR i }W n ty   	}Y n0 Y n0 |r|d |d  |d |d  f}|dkrR||d }}|d| d? d@  |d |jd  |d |jd  f}j	||| | nj
|||  d S )Nr   r   )	directionfeatureslanguager   anchorr   r   r\         )r   Zgetmask2AttributeErrorgetmask	TypeErrorgetbandfillbandro   r   rr   r   r4   )	r   r   Zstroke_offsetr   rS   rl   offsetcolorZcoord2r   argsr   embedded_colorr   r   kwargsr   r   r   r/   r    r!   	draw_text  s^    	

$	

 
$z!ImageDraw.text.<locals>.draw_text)r   N)r   multiline_textr   r   r&   )r   r/   r   r   r   r   r   alignr   r   r   r   stroke_fillr   r   r   r   r   r   Z
stroke_inkr    r   r!   r     s>    
"/
zImageDraw.textc                 C   s  |dkrt d|d u rd}n*t|dkr4t dn|d dv rHt dg }d	}| |}| |||}|D ],}| j||||	|
d
}|| t||}ql|d }|d dkr|t|d | d 8 }n |d dkr|t|d | 8 }t|D ]\}}|d	 }|||  }|d	 dkr(||d 8 }n|d	 dkr>||8 }|dkrJn4|dkrb||d 7 }n|dkrv||7 }nt d| j||f||||||	|
|||d ||7 }qd S )Nttb/ttb direction is unsupported for multiline textlar?   #anchor must be a 2 character stringr   tb'anchor not supported for multiline textr   r   r   r   m       @r   r   r   centerright)align must be "left", "center" or "right")r   r   r   r   r   r   )	r   r`   r   r   
textlengthappendmax	enumerater   )r   r/   r   r   r   r   r   r   r   r   r   r   r   r   widths	max_widthlinesline_spacingrb   
line_widthtopidxr   width_differencer    r    r!   r     sf    







zImageDraw.multiline_textc              
   C   s   t ddd | |rbt 4 tjdtd | |||||||W  d   S 1 sX0    Y  |du rr|  }t 0 tjdtd ||||||W  d   S 1 s0    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   r    r    r!   r   B  s0    

"

zImageDraw.textsizec                 C   s   t ddd d}| |}	| |||}
t L tjdtd |	D ](}| |||||||\}}t||}qDW d    n1 s0    Y  |t	|	|
 | fS )Nr   r   multiline_textbboxr   r   r   )
r   r   r   r   r   r   r   r   r   r`   )r   r   r   r   r   r   r   r   r   r   r   rb   r   line_heightr    r    r!   r   g  s$    


	*zImageDraw.multiline_textsizec           	      C   s   |  |rtd|r(| jdvr(td|du r8|  }|r@dn| j}z||||||W S  ty   tdd t	 2 tj
dtd	 | j|||||d
}W d   n1 s0    Y  |dkr|d  Y S |d  Y S 0 dS )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   Z	getlengthr   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   ro   r    r    r!   r     s.    



$zImageDraw.textlengthc                 C   s   |r| j dvrtd| |r>| |||||||||	|
|S |du rN|  }|rVdn| j}||||||	|
|}|d |d  |d |d  |d |d  |d |d  fS )	z2Get the bounding box of a given string, in pixels.r   r   Nr   r   r   r?   r\   )r   r   r   r   r&   r   getbbox)r   r/   r   r   r   r   r   r   r   r   r   r   r   bboxr    r    r!   textbbox  s.    
zImageDraw.textbboxc                 C   s.  |dkrt d|d u rd}n*t|dkr4t dn|d dv rHt dg }d	}| |}| |||
}|D ].}| j|||||	|d
}|| t||}ql|d }|d dkr|t|d | d 8 }n |d dkr|t|d | 8 }d }t|D ]
\}}|d	 }|||  }|d	 dkr0||d 8 }n|d	 dkrF||8 }|dkrRn4|dkrj||d 7 }n|dkr~||7 }nt d| j||f||||||	|
|d	}|d u r|}nDt	|d	 |d	 t	|d |d t|d |d t|d |d f}||7 }q|d u r*|d	 |d |d	 |d fS |S )Nr   r   r   r?   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r\   )
r   r`   r   r   r   r   r   r   r   min)r   r/   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rb   r   r   r   r   r   r   Z	bbox_liner    r    r!   r     s    









zImageDraw.multiline_textbbox)N)N)Nr   )N)NNr   )NNr   )Nr   N)NN)NNr   )N)NNr   )r   NN)NNr   )r   NNr   )NNNr>   r   NNNr   NF)NNNr>   r   NNNr   NF)Nr>   NNNr   )Nr>   NNNr   )NNNNF)	NNr>   r   NNNr   F)	NNr>   r   NNNr   F)__name__
__module____qualname__r"   r&   r-   r3   r5   r9   r<   rb   ri   ra   rc   rs   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r    r    r    r!   r   0   s   
*


	


<
	


 


S           
r           
Q      
(      
      
'         
-         r   c                 C   s.   z|  |W S  ty(   t| | Y S 0 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!   rq   0  s    rq   c                 C   s^   d}|rd|v r4zddl m} W n ty2   Y n0 |du rHddl m} | rV|| } | |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$   r   ImportErrorr   rq   )r   hintshandlerr    r    r!   r   H  s    
r   c                 C   sf  |   }|\}}z0|||f }t|||kr2W dS ||||f< W n ttfyX   Y dS 0 ||fh}	t }
|	rbt }|	D ]\}}|d |f|d |f||d f||d ffD ]\}}||f|
v s|dk s|dk rqz|||f }W n ttfy   Y q0 |
||f |du r$t|||k}n||ko4||k}|r||||f< |||f qqz|	}
|}	qjd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)imager/   valueborderthreshpixelrU   rV   
backgroundedgeZ	full_edgeZnew_edgestrM   r   r    r    r!   	floodfill`  s<    
4
r   c                    s  t |tstd|dk r"tdt | ttfs8tdt| dkrP| ^ nt| dkrf| \ntdtdd g R D std	tdkstd
dkrtdt |ttfstddd   fdddd }|||}fd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 intr\   zn_sides should be an int > 2z!bounding_circle should be a tupler?   z[bounding_circle should contain 2D coordinates and a radius (e.g. (x, y, r) or ((x, y), r) )c                 s   s   | ]}t |ttfV  qd S r'   )r(   intfloatrA   r    r    r!   rO     rE   z4_compute_regular_polygon_vertices.<locals>.<genexpr>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              
   S   s   t | d ttd|  | d ttd|   |d  dt | d ttd|  | d ttd|   |d  dfS )Nr   rF   r   r?   )roundrG   rP   rQ   rR   )rc   rH   centroidr    r    r!   _apply_rotation  s"    z:_compute_regular_polygon_vertices.<locals>._apply_rotationc                    s   |dg} ||| S )Nr   r    )r   polygon_radiusrT   Zstart_point)r   r    r!   _compute_polygon_vertex  s    zB_compute_regular_polygon_vertices.<locals>._compute_polygon_vertexc                 S   sR   g }d|  }dd|  | }t d| D ]&}|| ||7 }|dkr&|d8 }q&|S )NrF   r{   g      ?r   )r_   r   )rv   rw   rd   rH   Zcurrent_angle_r    r    r!   _get_angles  s    

z6_compute_regular_polygon_vertices.<locals>._get_anglesc                    s   g | ]} |qS r    r    )rB   rT   )r   r   r   r    r!   rD     s   z5_compute_regular_polygon_vertices.<locals>.<listcomp>)	r(   r   r   r   r^   r@   r`   allr   )ru   rv   rw   r   rd   r    )r   r   r   r   r!   rt     s<    )


rt   c                    s<   t tr,t fddtdtD S t  S dS )zJ
    Uses 1-norm distance to calculate difference between two values.
    c                 3   s"   | ]}t  | |  V  qd S r'   )absrA   Zcolor1Zcolor2r    r!   rO     rE   z_color_diff.<locals>.<genexpr>r   N)r(   r@   sumr_   r`   r   r   r    r   r!   r     s    
"r   )N)NN)Nr   )rG   r+   r   r$   r   r   
_deprecater   r   rq   r   r8   ZOutliner   r   r   rt   r   r    r    r    r!   <module>!   s(   
      



8u