o �J�h��@s&UddlmZddlZddlZddlmZddlmZddlm Z m Z m Z m Z m Z mZddlmZmZddlmZdd lmZd ed <zejjZWn eyTdZYnwe r_dd lmZmZe eeed fe fZ! Gdd�d�Z"d5d6dd�Z# d7d8dd�Z$  d9d:d'd(�Z%d;d/d0�Z&d<d3d4�Z'dS)=�)� annotationsN)�Sequence)� ModuleType)� TYPE_CHECKING�Any�AnyStr�Callable�Union�cast�)�Image� ImageColor)� deprecate)�Coordsz(Callable[[], Image.core._Outline] | None�Outline)� ImageDraw2� ImageFont.c@s�eZdZUdZded<dsdtd d �Zdud d�Zdvdd�Z dsdwdd�Z  dxdyd!d"�Z dsdzd%d&�Z   d{d|d(d)�Z   d{d}d*d+�Z   d{d~d.d/�Z  0 dd�d2d3�Z  d�d�d6d7�Z   d{d|d8d9�Zdsd�d:d;�Z   d{d}d<d=�Z 0   d�d�dBdC�Z   d{d}dDdE�Z 0   d�ddF�d�dIdJ�Zd�dNdO�Zd�dQdR�Zd�dUdV�Z    W X    0  Yd�d�dgdh�Z    W X    0  Yd�ddi�d�djdk�Z     Yd�ddi�d�dldm�Z   W X    0 Yd�ddi�d�dodp�Z   W X    0 Yd�ddi�d�dqdr�ZdS)�� ImageDrawN�NImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont | None�font�im� Image.Image�mode� str | None�return�NonecCs�|��|jr |��d}|dur|j}||jkr+|dkr%|jdkr%d}nd}t|��|dkr4|j|_nd|_||_|j|_tj � |j|�|_ ||_|dvrV|j � d�|_ n|j � d �|_ |d vred |_ nd |_ d |_dS)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. rN�RGBA�RGBr z mode mismatch�P)�I�F�����)�1rrr r"�LF)�load�readonly�_copyr� ValueError�palette�_imagerr �core�draw�draw_ink�ink�fontmode�fill)�selfrr�blend�msg�r3�KC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\PIL\ImageDraw.py�__init__Es2    zImageDraw.__init__�GImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFontcCs"|jsddlm}|��|_|jS)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 �r)r�r� load_default)r0rr3r3r4�getfontos  zImageDraw.getfont� font_size� float | NonecCs&|durddlm}|�|�S|��S)Nr r7)r8rr9r:)r0r;rr3r3r4�_getfont�s  zImageDraw._getfontr-� _Ink | Noner/�tuple[int | None, int | None]cCs�d}d}|dur|dur|jr|j}||fS|j}||fS|durCt|t�r-t�||j�}|jr=t|t�r=|j�||j �}|j � |�}|durit|t�rSt�||j�}|jrct|t�rc|j�||j �}|j � |�}||fS�N) r/r-� isinstance�strr �getcolorrr(�tupler)r+r,)r0r-r/Z result_inkZ result_fillr3r3r4�_getink�s*��    zImageDraw._getinkr �xyr�start�float�end�width�intcCs2|�|�\}}|dur|j�|||||�dSdS)z Draw an arc.N)rEr+�draw_arc)r0rFrGrIr/rJr-r3r3r4�arc�s �z ImageDraw.arc� Sequence[int]�bitmapcCsD|��|�|�\}}|dur|}|dur |j�||j|�dSdS)zDraw a bitmap.N)r$rEr+� draw_bitmapr)r0rFrOr/r-r3r3r4rO�s�zImageDraw.bitmap�outlinec C�j|�||�\}}|dur|j�||||d�|dur/||kr1|dkr3|j�||||d|�dSdSdSdS)z Draw a chord.Nr r)rEr+� draw_chord� r0rFrGrIr/rQrJr-�fill_inkr3r3r4�chord��   �zImageDraw.chordcC�b|�||�\}}|dur|j�||d�|dur+||kr-|dkr/|j�||d|�dSdSdSdS)zDraw an ellipse.Nr r)rEr+� draw_ellipse�r0rFr/rQrJr-rUr3r3r4�ellipse��  �zImageDraw.ellipse�Sequence[float]�radiuscCs@|d||d||d||d|f}|�||||�dS)z4Draw a circle given center coordinates and a radius.rr N)r[)r0rFr^r/rQrJZ ellipse_xyr3r3r4�circle�s, zImageDraw.circler�jointcsR|�|�d}|du�r!|j��|��|dk�r#�dk�r%t�dttf�r1tttt��}n�fdd�t dt ��d�D�}t dt |�d�D]�}||}d d�||d|f|||dffD�} | d| dkrmqId�fdd� } | d| dko�| dd| dkp�| d| dko�| dd| dk} |d�dd|d�ddf|d�dd|d�ddfg} | r�| dd| dd} }n | dd| dd} }|� | | d|d|��dk�r | �r| || dd�|| || dd�g}n| || dd�|| || dd�g}|j ||dd�qIdSdSdSdS)z6Draw a line, or a connected sequence of line segments.rNZcurve�c s*g|]}tttt�||d����qS)�)r rrHrD��.0�i)rFr3r4� <listcomp>�s��z"ImageDraw.line.<locals>.<listcomp>rbr c Ss>g|]\}}t�t�|d|d|d|d��d�qS)rr �h)�math�degrees�atan2)rdrGrIr3r3r4rfs (����coordr]�anglerHr�tuple[float, ...]c sZ|\}}|d8}�dd}tdd�||t�t�|��f||t�t�|��ffD��S)N�Zrbr css4�|]\}}||dkrt�|�nt�|�VqdS)rN)rh�floor�ceil)rd�pZp_dr3r3r4� <genexpr>s �� �z9ImageDraw.line.<locals>.coord_at_angle.<locals>.<genexpr>)rDrh�cos�radians�sin)rkrl�x�y�distance�rJr3r4�coord_at_angles ��z&ImageDraw.line.<locals>.coord_at_angle�rn��ry)rkr]rlrHrrm) rEr+� draw_linesrA�listrDr rrH�range�len�pieslice�line)r0rFr/rJr`r-�pointsre�point�anglesrzZflipped�coordsrGrIZ gap_coordsr3)rJrFr4r��sZ  ���"�"�&&� ���� zImageDraw.line�shape�Image.core._OutlinecCs\|��|�||�\}}|dur|j�||d�|dur*||kr,|j�||d�dSdSdS)z(Experimental) Draw a shape.Nr r)�closerEr+� draw_outline)r0r�r/rQr-rUr3r3r4r�<s�zImageDraw.shapec CrR)zDraw a pieslice.Nr r)rEr+� draw_pieslicerTr3r3r4r�JrWzImageDraw.pieslicecCs,|�|�\}}|dur|j�||�dSdS)z#Draw one or more individual pixels.N)rEr+� draw_points)r0rFr/r-r3r3r4r�Zs�zImageDraw.pointc Cs<|�||�\}}|dur|j�||d�|dur�||kr�|dkr�|dkr/|j�||d|�dS|jdur�t�d|jj�}|�d�d}|��} t| �} | j�||d�|��} t| �} |dd}| j�||d|�|j | | d�t�|j |jj�} t| �} | j�||d|�|j� | jd| j|j�dSdSdSdSdS)zDraw a polygon.Nr rr"rb)�mask�rr) rEr+� draw_polygonrr �new�size�copy�Draw�paster) r0rFr/rQrJr-rUr�Zmask_inkZfill_imr+Zink_imrr3r3r4�polygon`s.   �zImageDraw.polygon�bounding_circle�!Sequence[Sequence[float] | float]�n_sides�rotationcCs t|||�}|�||||�dS)zDraw a regular polygon.N)�!_compute_regular_polygon_verticesr�)r0r�r�r�r/rQrJrFr3r3r4�regular_polygon�s zImageDraw.regular_polygoncCrX)zDraw a rectangle.Nr r)rEr+�draw_rectanglerZr3r3r4� rectangle�r\zImageDraw.rectangle��cornersr��$tuple[bool, bool, bool, bool] | Nonec snt|dttf�rtttt|�\\�� \� � n ttt|�\�� � � � �kr.d}t|��� � kr8d}t|���dur>d�|d�t���t� �� t� �� t� �� d\��t��r��� �dk��rh� ���� � dk��rv� � ��r��r��� |||��S�dks�t ��s��� |||��St �d�}�� ||�\��d���������� � � f d d� } �du�rT| d��r҈j��� |d� � |df�d�n � |d�|dkr�j��|d� � |d� f�d���sT��sT�� �|� g} �d�r| d|d7<�d�r| d|d8<�j�| �d�� |� � � g} �d�r=| d|d7<�d�rL| d|d8<�j�| �d��du�r/��k�r1�dk�r3| d���sʈ� � � �dg} �d�r�| d|d7<�d�r�| d|d8<�j�| �d��� �d� � g} �d�r�| d|d7<�d�r�| d|d8<�j�| �d���s5�� ��d� g} �d�r�| d|d7<�d�r�| d|d8<�j�| �d�� �d� � � g} �d�r| d|d7<�d�r%| d|d8<�j�| �d�dSdSdSdSdS)zDraw a rounded rectangle.rz&x1 must be greater than or equal to x0z&y1 must be greater than or equal to y0N)TTTTrb)FFr r��boolrrc s*�r�� ��� �fddf�� ���� fddff}nZ�r:�� ��� �fddf� �� � � �fddff}n=t�fdd�t�� ��� �fddf� �� � � �fddf� �� �� � fddf�� ���� fddff�D��}|D]}|r��jj|�df�qy�jj|��f�qydS) Nr{rgrrn�c3s �|] \}}�|r|VqdSr@r3)rdre�partr�r3r4rr�s�� �zDImageDraw.rounded_rectangle.<locals>.draw_corners.<locals>.<genexpr>r )rD� enumerater+r�rL)r��partsr�� r��drUZfull_xZfull_yr-r0rJ�x0�x1�y0�y1r3r4� draw_corners�s,�� ��� �z1ImageDraw.rounded_rectangle.<locals>.draw_cornersTr}F)r�r�rr)rArrDr rrHr'�round�allr[�anyr�rKrEr+r�)r0rFr^r/rQrJr�r2�rr��left�right�top�bottomr3r�r4�rounded_rectangle�s�  $ "*(              �zImageDraw.rounded_rectangle�textrr�cCst|t�rdnd}||vS�N� � )rArB)r0r�Zsplit_characterr3r3r4�_multiline_check+szImageDraw._multiline_check� list[AnyStr]cCs|�t|t�r d�Sd�Sr�)�splitrArB)r0r�r3r3r4�_multiline_split0szImageDraw._multiline_split�spacing� stroke_widthcCs|jdd||d�d||S)Nr��A)r�r})�textbbox)r0rr�r�r3r3r4�_multiline_spacing3s ���zImageDraw._multiline_spacingrar�F�tuple[float, float]�anchor�alignrB� direction�features�list[str] | None�language� stroke_fill�embedded_color�argsr�kwargscs��r �jdvr d}t|���dur����d������ �r/��� � |��||���| | �� Sd�fd d � }dd���������� � f dd� }||�}|durtd}| r^| dur\|| �n|}|durn||| �||d �dS||�dSdS)z Draw text.�rr�3Embedded color supported only in RGB and RGBA modesNr;r/r>rrKcs*��|�\}}|dur|dusJ�|S|Sr@)rE)r/r-rU)r0r3r4�getinkos  zImageDraw.text.<locals>.getinkrr-r�rHrc s��j}|dkr �r d}g}td�D] }|�t� |��qt�� d�dt�� d�df}z)�j� |g��R���|�||d����\}}|d|d|d|dg}Wn/ty�z�j� |���|�|g��Rd|i���}Wnt y���� �}YnwYnw|dkr�||� d�}}t � d|�d} |� d| �|\} } �jdurÈj�|| | | |jd| |jdf|�dSdS�j�|||�dS) Nrrrbr )r�r�r�r�r�r-rGrGr}re)r.r��appendrKrh�modfZgetmask2�AttributeError�getmask� TypeError�getband�struct�pack�fillbandrr�r�r+rP) r-r�rrkrerGr��offset�colorZ ink_alpharvrw� r�r�r�r�r�rr�r�r0r�rFr3r4� draw_textvsv  $� �� �$ � � � � ���  "��z!ImageDraw.text.<locals>.draw_text)r/r>rrK)r)r-rKr�rHrr)rr'r=�getr��multiline_text)r0rFr�r/rr�r�r�r�r�r�r�r�r�r�r�r2r�r�r-Z stroke_inkr3r�r4r�?sB �$6  �zImageDraw.text)r;cCs�|dkr d}t|��|durd}nt|�dkrd}t|��|ddvr)d}t|��|dur2|�|�}g}d }|�|�}|�||| �}|D]}|j|||| | d �}|�|�t||�}qD|d}|dd krr|t|�d|d 8}n|dd kr�|t|�d|8}t|�D]Z\}}|d }|||}|d d kr�||d 8}n |d dkr�||8}|dkr�n|dkr�||d 7}n|dkr�||7}nd}t|��|j ||f|||||| | | | | d� ||7}q�dS)N�ttb�/ttb direction is unsupported for multiline text�larb�#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'r�r=r�r�� textlengthr��maxr�r�)r0rFr�r/rr�r�r�r�r�r�r�r�r�r;r2�widths� max_width�lines� line_spacingr�� line_widthr��idxr��width_differencer3r3r4r��st     �        � �zImageDraw.multiline_textc Csb|�|�r d}t|��|r|jdvrd}t|��|dur!|�|�}|r%dn|j} |�|| |||�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)r�r'rr=r.Z getlength) r0r�rr�r�r�r�r;r2rr3r3r4r�s  zImageDraw.textlength�!tuple[float, float, float, float]c  Cs�| r |jdvr d} t| ��|dur|�| �}|�|�r*|�||||||||| | | � S| r.dn|j}|�||||| | |�}|d|d|d|d|d|d|d|dfS) z2Get the bounding box of a given string, in pixels.r�r�Nrrr rbr})rr'r=r��multiline_textbboxr.�getbbox)r0rFr�rr�r�r�r�r�r�r�r�r;r2r�bboxr3r3r4r�5s0  ��<zImageDraw.textbboxc  Cs@|dkr d} t| ��|durd}nt|�dkrd} t| ��|ddvr)d} t| ��|dur2|�| �}g}d }|�|�}|�||| �}|D]}|j||||| | d �}|�|�t||�}qD|d}|dd krs|t|�d|d 8}n|dd kr�|t|�d|8}d}t|�D]�\}}|d }|||}|d d kr�||d 8}n |d dkr�||8}|dkr�n|dkr�||d 7}n|dkr�||7}nd} t| ��|j ||f|||||| | | d� }|dur�|}n"t |d |d �t |d|d�t|d|d�t|d|d�f}||7}q�|du�r|d |d|d |dfS|S)Nr�r�r�rbr�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)r0rFr�rr�r�r�r�r�r�r�r�r;r2r�r�r�r�r�r�r�r�r�r�r�Z bbox_liner3r3r4r�gs�    �        � �  zImageDraw.multiline_textbboxr@)rrrrrr)rr6)r;r<rr6)r-r>r/r>rr?)Nr ) rFrrGrHrIrHr/r>rJrKrr)rFrNrOrr/r>rr)NNr )rFrrGrHrIrHr/r>rQr>rJrKrr) rFrr/r>rQr>rJrKrr) rFr]r^rHr/r>rQr>rJrKrr)NrN) rFrr/r>rJrKr`rrr�NN)r�r�r/r>rQr>rr)rFrr/r>rr)rNNr )r�r�r�rKr�rHr/r>rQr>rJrKrr)rFrr^rHr/r>rQr>rJrKr�r�rr)r�rrr�)r�rrr�)rr6r�rHr�rHrrH) NNNrar�NNNrNF) rFr�r�rr/r>rrr�rr�rHr�rBr�rr�r�r�rr�rHr�r>r�r�r�rr�rrr)rFr�r�rr/r>rrr�rr�rHr�rBr�rr�r�r�rr�rHr�r>r�r�r;r<rr)NNNNF)r�rrrr�rr�r�r�rr�r�r;r<rrH) NNrar�NNNrF)rFr�r�rrrr�rr�rHr�rBr�rr�r�r�rr�rHr�r�r;r<rr�)�__name__� __module__� __qualname__r�__annotations__r5r:r=rErMrOrVr[r_r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r3r3r3r4r@s�  �  *  � � � � � � �P � �   �( � ���   ���d��&��;��rrrrrrcCs.zt|d�|�WStyt||�YSw)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. �getdraw)�getattrr�r)rrr3r3r4r��s   �r��Image.Image | None�hintsr��)tuple[ImageDraw2.Draw | None, ModuleType]cCs<|dur tdd�ddlm}|dur|�|�nd}||fS)z� :param im: The image to draw in. :param hints: An optional list of hints. Deprecated. :returns: A (drawing context, drawing resource factory) tuple. Nz'hints' parameter� r )r)rr8rr�)rrrr+r3r3r4r��s   r��imagerF�tuple[int, int]�value�float | tuple[int, ...]�border�float | tuple[int, ...] | None�threshrHrc Csn|��}|dus J�|\}}z|||f}t||�|krWdS||||f<Wn ttfy2YdSw||fh} t�} | r�t�} | D]j\}}|d|f|d|f||df||dffD]O\} } | | f| vsn| dksn| dkroq\z|| | f}Wn ttfy�Yq\w| �| | f�|dur�t||�|k}n|||fv}|r�||| | f<| �| | f�q\qB| } | } | s=dSdS)a� .. warning:: This method is 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)rrFrrr �pixelrvrw� background�edgeZ full_edgeZnew_edge�s�trqr/r3r3r4� floodfill�sH  �  4�  �� �rr�r�r�rKr��list[tuple[float, float]]cs�t|t�s d}t|��|dkrd}t|��t|ttf�s"d}t|��t|�dkrDtdd�|D��s7d}t|��ttt t|���^��nPt|�dkr�t|d ttf�r�td d�|d D��rgt|d tt f�smd}t|��t|d �dkr{d }t|��ttt t|d ���tt |d ��nd }t|���d kr�d}t|��t|tt f�s�d}t|��d#�fdd� �d$��fdd� �d%dd�}|||�}�fd d!�|D�S)&u� Generate a list of vertices for a 2D regular polygon. :param bounding_circle: The bounding circle is a sequence 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 zn_sides should be an intr}zn_sides should be an int > 2z$bounding_circle should be a sequencecs��|] }t|ttf�VqdSr@�rArKrHrcr3r3r4rros�z4_compute_regular_polygon_vertices.<locals>.<genexpr>z0bounding_circle should only contain numeric datarbrcsrr@rrcr3r3r4rrus� �r zBbounding_circle centre should contain 2D coordinates (e.g. (x, y))z[bounding_circle should contain 2D coordinates and a radius (e.g. (x, y, r) or ((x, y), r) )z$bounding_circle radius should be > 0z"rotation should be an int or floatr�� list[float]rirHrr�c s�t|dt�t�d|��|dt�t�d|���dd�t|dt�t�d|��|dt�t�d|���dd�fS)Nrrgr rb)r�rhrsrtru)r�ri)�centroidr3r4�_apply_rotation�s"�������z:_compute_regular_polygon_vertices.<locals>._apply_rotationrlcs�dg}�||�S�Nrr3)rlZ start_point)r�polygon_radiusr3r4�_compute_polygon_vertex�s zB_compute_regular_polygon_vertices.<locals>._compute_polygon_vertexr�rKr�cSsRg}d|}dd||}td|�D]}|�|�||7}|dkr&|d8}q|S)Nrgr�g�?r)r�r�)r�r�r�riZ current_angle�_r3r3r4� _get_angles�s �z6_compute_regular_polygon_vertices.<locals>._get_anglescsg|]}�|��qSr3r3)rdrl)rr3r4rf�sz5_compute_regular_polygon_vertices.<locals>.<listcomp>N)r�rrirHrr�)rlrHrr�)r�rKr�rHrr) rArKr�r'rrDr�r�r rH)r�r�r�r2rr�r3)rrrrr4r�7sR + ���  r��color1�color2csJt|t�r|n|f�t|t�r|n|f�t��fdd�tdt���D��S)zJ Uses 1-norm distance to calculate difference between two values. c3s$�|] }t�|�|�VqdSr@)�absrc��first�secondr3r4rr�s�"z_color_diff.<locals>.<genexpr>r)rArD�sumr�r�)r r!r3r#r4r �s"r r@)rrrrrrr�)rrrr�rrr) rrrFrrrrr r rHrr)r�r�r�rKr�rHrr)r rr!rrrH)(� __future__rrhr��collections.abcr�typesr�typingrrrrr r r8r r � _deprecater�_typingrr�r*rQrr�rrrHrDrKrBZ_Inkrr�r�rr�r r3r3r3r4�<module>sH       �  � � A
Memory