CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ImageDraw.cpython-310.pyc254 linesDownload Raw Back to __pycache__
1o

2��Yiˑ�@s�UddlmZddlZddlZddlmZddlmZddlm	Z	m3Z4mZdZerMddlm
Z
dd	lmZdd5lmZmZddlmZmZddlmZmZe	jjZd
ed<	Gdd�d�Zd4d5dd�Zd4d6dd�Z		d7d8d&d'�Zd9d.d/�Zd:d2d3�Z dS);�)�annotationsN)�Sequence)�cast�)�Image�6ImageColor�	ImageTextF)�Callable)�7ModuleType)�Any�AnyStr)�8ImageDraw2�	ImageFont)�Coords�_Inkz!Callable[[], Image.core._Outline]�Outlinec@s�eZdZUdZded<dmdnd9d�Zdod
d�Zdpdd�Z	dmdqdd�Z		drdsd!d"�Z		dmdtd%d&�Z10			dudvd(d)�Z			dudwd*d+�Z			dudxd.d/�Z
		0	dydzd2d3�Z		d{d|d6d7�Z			dudvd8d9�Zdmd}d:d;�Z			dudwd<d=�Z	0			d~ddBdC�Z			dudwdDdE�Z	0			d~ddF�d�dIdJ�Z				K	L				0		Md�d�d`da�Z				K	L				0		Md�ddb�d�ddde�Z					Md�ddb�d�dfdg�Z			K	L				0	Md�ddb�d�didj�Z			K	L				0	Md�ddb�d�dkdl�ZdS)��	ImageDrawN�NImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont | None�font�im�Image.Image�mode�11str | None�return�NonecCs�|��d}|dur
|j}||jkr$|dkr|jdkrd}nd}t|��|dkr-|j|_nd|_||_|j|_tj�|j|�|_||_|dvrO|j�	d�|_12n|j�	d	�|_13|d14vr^d|_nd|_d
|_dS)a�15        Create a drawing instance.16 17        :param im: The image to draw in.18        :param mode: Optional mode to use for color values.  For RGB19           images, this argument can be RGB or RGBA (to blend the20           drawing into the image).  For all other modes, this argument21           must be the same as the image mode.  If omitted, the mode22           defaults to the mode of the image.23        rN�RGBA�RGBrz
mode mismatch�P)�I�F�����)�1rrrr!�LF)
Z_ensure_mutabler�24ValueError�palette�_imagerr�core�draw�draw_ink�ink�fontmode�fill)�selfrr�blend�msg�r/�wE:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\PIL/ImageDraw.py�__init__Bs.252627zImageDraw.__init__�GImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFontcCs"|jsddlm}|��|_|jS)a=28        Get the current default font.29 30        To set the default font for this ImageDraw instance::31 32            from PIL import ImageDraw, ImageFont33            draw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")34 35        To set the default font for all future ImageDraw instances::36 37            from PIL import ImageDraw, ImageFont38            ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")39 40        If the current default font is ``None``,41        it is initialized with ``ImageFont.load_default()``.42 43        :returns: An image font.r�r)r�r�load_default)r,rr/r/r0�getfontjs44zImageDraw.getfont�	font_size�float | NonecCs&|durddlm}|�|�S|��S)Nrr3)r4rr5r6)r,r7rr/r/r0�_getfont�s45zImageDraw._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	�}|j46�|�}|durit|t�rSt�||j�}|jrct|t�rc|j�||j	�}|j47�|�}||fS�N)r+r)�48isinstance�strrZgetcolorrr$�tupler%r'r()r,r)r+Z49result_inkZresult_fillr/r/r0�_getink�s*��5051zImageDraw._getinkr�xyr�start�float�end�width�intcCs2|�|�\}}|dur|j�|||||�dSdS)zDraw an arc.N)r@r'�draw_arc)r,rArBrDr+rEr)r/r/r0�arc�s	�z
ImageDraw.arc�
Sequence[int]�bitmapcCsD|��|�|�\}}|dur|}|dur |j�||j|�dSdS)zDraw a bitmap.N)�loadr@r'�draw_bitmapr)r,rArJr+r)r/r/r0rJ�s�zImageDraw.bitmap�outlinec	C�j|�||�\}}|dur|j�||||d�|dur/||kr1|dkr3|j�||||d|�dSdSdSdS)z
Draw a chord.Nrr)r@r'Z52draw_chord�	r,rArBrDr+rMrEr)�fill_inkr/r/r0�chord��53�zImageDraw.chordcC�b|�||�\}}|dur|j�||d�|dur+||kr-|dkr/|j�||d|�dSdSdSdS)zDraw an ellipse.Nrr)r@r'Zdraw_ellipse�r,rAr+rMrEr)rPr/r/r0�ellipse���zImageDraw.ellipse�Sequence[float]�radiuscCs@|d||d||d||d|f}|�||||�dS)z4Draw a circle given center coordinates and a radius.rrN)rU)r,rArXr+rMrEZ54ellipse_xyr/r/r0�circle�s,	zImageDraw.circler�jointcsR|�|�d}|du�r!|j��|��|dk�r#�dk�r%t�dttf�r1tttt��}n�fdd�t	dt55��d�D�}t	dt56|�d�D]�}||}d	d�||d|f|||dffD�}	|	d|	dkrmqId�fdd�}57|	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|58||	dd�||59||	dd�g}n|60||	dd�||61||	dd�g}|j||dd�qIdSdSdSdS)z6Draw a line, or a connected sequence of line segments.rNZcurve�c	s*g|]}tttt�||d����qS)�)rrrCr?��.0�i)rAr/r0�62<listcomp>�s��z"ImageDraw.line.<locals>.<listcomp>r\rc63Ss>g|]\}}t�t�|d|d|d|d��d�qS)rr�h)�math�degrees�atan2)r^rBrDr/r/r0r`s(����coordrW�anglerCr�tuple[float, ...]c64sZ|\}}|d8}�dd}tdd�||t�t�|��f||t�t�|��ffD��S)N�Zr\rcss4�|]\}}||dkrt�|�nt�|�VqdS)rN)rb�floor�ceil)r^�pZp_dr/r/r0�	<genexpr>s65��66�z9ImageDraw.line.<locals>.coord_at_angle.<locals>.<genexpr>)r?rb�cos�radians�sin)rerf�x�yZdistance�rEr/r0�coord_at_angles��z&ImageDraw.line.<locals>.coord_at_angle�rh��rr)rerWrfrCrrg)
r@r'Z67draw_linesr=�listr?rrrC�range�len�pieslice�line)r,rAr+rErZr)Zpointsr_�point�anglesrsZflipped�coordsrBrDZ68gap_coordsr/)rErAr0r{�sZ6970���"�"�&&�71����	zImageDraw.line�shape�Image.core._OutlinecCs\|��|�||�\}}|dur|j�||d�|dur*||kr,|j�||d�dSdSdS)z(Experimental) Draw a shape.Nrr)�closer@r'Zdraw_outline)r,rr+rMr)rPr/r/r0r7s�zImageDraw.shapec	CrN)zDraw a pieslice.Nrr)r@r'�
draw_pieslicerOr/r/r0rzErRzImageDraw.pieslicecCs,|�|�\}}|dur|j�||�dSdS)z#Draw one or more individual pixels.N)r@r'Zdraw_points)r,rAr+r)r/r/r0r|Us�zImageDraw.pointc72Cs�|�||�\}}|dur|j�||d�|dur`||krb|dkrd|dkr/|j�||d|�dS|jdurft�d|jj�}|�d�d}t|�}	|	j�||d�|j�||d|dd|j�dSdSdSdSdS)zDraw a polygon.Nrrr!r\)r@r'Zdraw_polygonrr�new�size�Draw)73r,rAr+rMrEr)rP�maskZmask_inkr'r/r/r0�polygon[s74"�zImageDraw.polygon�bounding_circle�!Sequence[Sequence[float] | float]�n_sides�rotationcCs t|||�}|�||||�dS)zDraw a regular polygon.N)�!_compute_regular_polygon_verticesr�)r,r�r�r�r+rMrErAr/r/r0�regular_polygonss75zImageDraw.regular_polygoncCrS)zDraw a rectangle.Nrr)r@r'�draw_rectanglerTr/r/r0�	rectangle�rVzImageDraw.rectangle��cornersr��$tuple[bool, bool, bool, bool] | Nonec
snt|dttf�rtttt|�\\��76\�	�nttt|�\��77�	��	�kr.d}t|����78kr8d}t|���dur>d�|d�t���t�79��80t�	��	t���d\��t��r���	�dk��rh�	�����81dk��rv��82��r��r���	|||��S�dks�t83��s���|||��St�d�}��
||�\��d����������	�84�fd
d�}	�du�rT|	d��r҈j���85|d�	�|df�d�n �	|d�|dkr�j��|d�86�	|d�f�d���sT��sT��87�|�g}88�d�r|89d|d7<�d�r|90d|d8<�j�|91�d��	|�92�	�g}�d�r=|d|d7<�d�rL|d|d8<�j�|�d��du�r/��k�r1�dk�r3|	d���sʈ�93�	�94�dg}�d�r�|d|d7<�d�r�|d|d8<�j�|�d����d�	�g}
�d�r�|
d|d7<�d�r�|
d|d8<�j�|
�d���s5��95��d�g}96�d�r�|97d|d7<�d�r�|98d|d8<�j�|99�d��	�d�100�	�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)TTTTr\)FFrrz�boolrrc101s*�r��102���103�fddf������fddff}nZ�r:��104���105�fddf�	��106�	�107�fddff}n=t�fdd�t��108���109�fddf�	��110�	�111�fddf�	����	�fddf������fddff�D��}|D]}|r��jj|�df�qy�jj|��f�qydS)	Nrtrarrh�c3s �|]\}}�|r|VqdSr<r/)r^r_�partr�r/r0rl�s��112�zDImageDraw.rounded_rectangle.<locals>.draw_corners.<locals>.<genexpr>r)r?�	enumerater'r�rG)rz�partsr��r��drPZfull_xZfull_yr)r,rEZx0�x1Zy0�y1r/r0�draw_corners�s,������z1ImageDraw.rounded_rectangle.<locals>.draw_cornersTrvF)rzr�rr)r=rwr?rrrCr#�round�allrU�anyr�rFr@r'r�)r,rArXr+rMrEr�r.�rr��left�right�top�bottomr/r�r0�rounded_rectangle�s� $113"*(114115116117118119120121122123124125�zImageDraw.rounded_rectangler[r�F�tuple[float, float]�text�AnyStr | ImageText.Text�anchor�spacing�alignr>�	direction�features�list[str] | None�language�stroke_width�stroke_fill�embedded_colorr��argsr�kwargscst|tj�r	|�n&|dur�	���d��}t�||�	j|������r'���|r/��||�d�	fdd�}||�}|dur@dSd}�jrQ�j	durO|�j	�n|}��126127�|�D].\�128��dd����������	�129fdd�
}|dur�||�j�||kr�||�qX||�qXdS)z130Draw text.Nr7r+r:rrFcs*��|�\}}|dur|dusJ�|S|Sr<)r@)r+r)rP)r,r/r0�getink?s131zImageDraw.text.<locals>.getinkrr)r�rCrcs��	j}|dkr�rd}g}td�D]}|�t�132|��qt��133d�dt��134d�df}z+�jj�|g��R���|d�||d����\}}|d|d|d|dg}Wn1ty�z�jj	�|���|�|g��Rd|i���}Wnt135y��j�	��}YnwYnw|dkr�||�d�}}t�
d	|�d}	|�d|	�|\}136}�	jdurLj	j�||137||138|jd||jdf|�dSdS�	j�|||�dS)139Nrrr\rT)r�r�r�r�Z
stroke_filledr�r)rBrBrvr_)r*rx�appendrFrb�modfrZgetmask2�AttributeErrorZgetmask�	TypeErrorZgetband�struct�packZfillbandrZpaster�r'rL)r)r�rrer_rBr��offset�colorZ	ink_alpharprq�r�r�r�r�r��140image_textr�r�r{r,rAr/r0�	draw_textTsx$���141�$�142�	�143�
���144"��z!ImageDraw.text.<locals>.draw_text)r+r:rrF)r)r)rFr�rCrr)r=r�Textr9�getr�embed_color�stroker�r��_split)r,rAr�r+rr�r�r�r�r�r�r�r�r�r�r�r�r)Z145stroke_inkr�r/r�r0r�s<�146147��$8�148�zImageDraw.text�r7rcCs&|j|||||||||	|149|||
|d�S�Nr�)r�)r,rAr�r+rr�r�r�r�r�r�r�r�r�r7r/r/r0�multiline_text�s �zImageDraw.multiline_textc	Cs>|dur	|�|�}tj|||j|||d�}|r|��|��S)z@Get the length of a given string, in pixels with 1/64 precision.N)r�r�r�)r9rr�rr�Z150get_length)	r,r�rr�r�r�r�r7r�r/r/r0�151textlength�s152�zImageDraw.textlength�!tuple[float, float, float, float]c	CsR|dur	|�|�}t�|||j||||	�}
|r|
��|153r"|
�|154�|
�|||�S)z2Get the bounding box of a given string, in pixels.N)r9rr�rr�r�Zget_bbox)r,rAr�rr�r�r�r�r�r�r�r�r7r�r/r/r0�textbbox�s155�156zImageDraw.textbboxc
Cs"|j|||||||||	|157||d�Sr�)r�)
r,rAr�rr�r�r�r�r�r�r�r�r7r/r/r0�multiline_textbbox�s�zImageDraw.multiline_textbboxr<)rrrrrr)rr2)r7r8rr2)r)r:r+r:rr;)Nr)rArrBrCrDrCr+r:rErFrr)rArIrJrr+r:rr)NNr)rArrBrCrDrCr+r:rMr:rErFrr)158rArr+r:rMr:rErFrr)rArWrXrCr+r:rMr:rErFrr)NrN)159rArr+r:rErFrZrrr)NN)rr�r+r:rMr:rr)rArr+r:rr)rNNr)r�r�r�rFr�rCr+r:rMr:rErFrr)rArrXrCr+r:rMr:rErFr�r�rr)NNNr[r�NNNrNF) rAr�r�r�r+r:rrr�rr�rCr�r>r�rr�r�r�rr�rCr�r:r�r�r�rr�rrr)rAr�r�rr+r:rrr�rr�rCr�r>r�rr�r�r�rr�rCr�r:r�r�r7r8rr)NNNNF)r�rrrr�rr�r�r�rr�r�r7r8rrC)	NNr[r�NNNrF)rAr�r�rrrr�rr�rCr�r>r�rr�r�r�rr�rCr�r�r7r8rr�)�__name__�160__module__�__qualname__r�__annotations__r1r6r9r@rHrJrQrUrYr{rrzr|r�r�r�r�r�r�r�r�r�r/r/r/r0r=s�161162�163(164�������P��	��������0��(��*��rrrrrrcCs.zt|d�|�WStyt||�YSw)a�165    A simple 2D drawing interface for PIL images.166 167    :param im: The image to draw in.168    :param mode: Optional mode to use for color values.  For RGB169       images, this argument can be RGB or RGBA (to blend the170       drawing into the image).  For all other modes, this argument171       must be the same as the image mode.  If omitted, the mode172       defaults to the mode of the image.173    �getdraw)�getattrr�r)rrr/r/r0r�$s174�r��Image.Image | None�)tuple[ImageDraw2.Draw | None, ModuleType]cCs*ddlm}|dur|�|�nd}||fS)zm175    :param im: The image to draw in.176    :returns: A (drawing context, drawing resource factory) tuple.177    r)r
N)r4r
r�)rr
r'r/r/r0r�5sr��imagerA�tuple[int, int]�value�float | tuple[int, ...]�border�float | tuple[int, ...] | None�threshrCrcCsn|��}|dus178J�|\}}z|||f}t||�|krWdS||||f<Wnttfy2YdSw||fh}	t�}179|	r�t�}|	D]j\}}|d|f|d|f||df||dffD]O\}}
||
f|180vsn|dksn|
dkroq\z|||
f}Wnttfy�Yq\w|181�||
f�|dur�t||�|k}n|||fv}|r�||||
f<|�||
f�q\qB|	}182|}	|	s=dSdS)a�183    .. warning:: This method is experimental.184 185    Fills a bounded region with a given color.186 187    :param image: Target image.188    :param xy: Seed position (a 2-item coordinate tuple). See189        :ref:`coordinate-system`.190    :param value: Fill color.191    :param border: Optional border value.  If given, the region consists of192        pixels with a color different from the border color.  If not given,193        the region consists of pixels having the same color as the seed194        pixel.195    :param thresh: Optional threshold value which specifies a maximum196        tolerable difference of a pixel value from the 'background' in197        order for it to be replaced. Useful for filling regions of198        non-homogeneous, but similar, colors.199    Nrr)rK�_color_diffr#�200IndexError�set�add)r�rAr�r�r�Zpixelrprq�201background�edgeZ	full_edgeZnew_edge�s�trkr+r/r/r0�	floodfill@sH�2024����r�r�r�r�rFr��list[tuple[float, float]]cs�t|t�sd}t|��|dkrd}t|��t|ttf�s"d}t|��t|�dkrDtdd�|D��s7d}t|��ttt	t|���^��nPt|�dkr�t|d	ttf�r�td203d�|d	D��rgt|dtt	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�204    Generate a list of vertices for a 2D regular polygon.205 206    :param bounding_circle: The bounding circle is a sequence defined207        by a point and radius. The polygon is inscribed in this circle.208        (e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)209    :param n_sides: Number of sides210        (e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)211    :param rotation: Apply an arbitrary rotation to the polygon212        (e.g. ``rotation=90``, applies a 90 degree rotation)213    :return: List of regular polygon vertices214        (e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)215 216    How are the vertices computed?217    1. Compute the following variables218        - theta: Angle between the apothem & the nearest polygon vertex219        - side_length: Length of each polygon edge220        - centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)221        - polygon_radius: Polygon radius (last element of bounding_circle)222        - angles: Location of each polygon vertex in polar grid223            (e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])224 225    2. For each angle in angles, get the polygon vertex at that angle226        The vertex is computed using the equation below.227            X= xcos(φ) + ysin(φ)228            Y= −xsin(φ) + ycos(φ)229 230        Note:231            φ = angle in degrees232            x = 0233            y = polygon_radius234 235        The formula above assumes rotation around the origin.236        In our case, we are rotating around the centroid.237        To account for this, we use the formula below238            X = xcos(φ) + ysin(φ) + centroid_x239            Y = −xsin(φ) + ycos(φ) + centroid_y240    zn_sides should be an intrvzn_sides should be an int > 2z$bounding_circle should be a sequencecs��|]241}t|ttf�VqdSr<�r=rFrCr]r/r/r0rl�s�z4_compute_regular_polygon_vertices.<locals>.<genexpr>z0bounding_circle should only contain numeric datar\rcsr�r<r�r]r/r/r0rl�s�242�rzBbounding_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]rcrCrr�c243s�t|dt�t�d|��|dt�t�d|���dd�t|dt�t�d|��|dt�t�d|���dd�fS)Nrrarr\)r�rbrmrnro)r|rc)�centroidr/r0�_apply_rotation�s"�������z:_compute_regular_polygon_vertices.<locals>._apply_rotationrfcs�dg}�||�S�Nrr/)rfZstart_point)r��polygon_radiusr/r0�_compute_polygon_vertex�s244zB_compute_regular_polygon_vertices.<locals>._compute_polygon_vertexr�rFr�cSsPg}d|}dd||}t|�D]}|�|�||7}|dkr%|d8}q|S)Nrar�g�?)rxr�)r�r�r}rcZ
current_angle�_r/r/r0�_get_angles�s245�z6_compute_regular_polygon_vertices.<locals>._get_anglescsg|]}�|��qSr/r/)r^rf)r�r/r0r`sz5_compute_regular_polygon_vertices.<locals>.<listcomp>N)r|r�rcrCrr�)rfrCrr�)r�rFr�rCrr�)246r=rFr�r#rwr?ryr�rrC)r�r�r�r.r�r}r/)r�r�r�r�r0r��sR247+���248249
r��color1�color2csHt|t�r|n|f�t|t�r|n|f�t��fdd�tt���D��S)zJ250    Uses 1-norm distance to calculate difference between two values.251    c3s$�|]
}t�|�|�VqdSr<)�absr]��first�secondr/r0rls�"z_color_diff.<locals>.<genexpr>)r=r?�sumrxry)r�r�r/r�r0r�s r�r<)rrrrrr)rr�rr�r�)r�rrAr�r�r�r�r�r�rCrr)r�r�r�rFr�rCrr�)r�r�r�r�rrC)!�252__future__rrbr��collections.abcr�typingrr4rrr�
TYPE_CHECKINGr	�typesr253rrr
rZ_typingrrr&rMrr�rr�r�r�r�r�r/r/r/r0�<module>s:l�254A
Aluode/PerceptionLabPortable · CoolFace