)EjEUddlmZddlZddlZddlmZddlmZddlm Z m Z m Z m Z dZ e r"ddlmZdd lmZdd lmZmZdd lmZdd lmZmZe jjZd ed< GddZd/d0dZd/d1dZ d2d3d$Zd4d+Zd5d.Z dS)6) annotationsN)Sequence)cast)Image ImageColor ImageFont ImageTextF)Callable) ModuleType)AnyAnyStr ImageDraw2)Coords_Inkz!Callable[[], Image.core._Outline]OutlineceZdZUdZded<dWdXd ZdYd ZdZdZ dWd[dZ d\d]dZ dWd^d Z d_d`d"Z d_dad#Z d_dbd&Z dcddd(Z dedfd+Z d_d`d,ZdWdgd-Z d_dad.Z dhdid4Z d_dad5Z dhdd6djd9Z dkdldOZ dkddPdmdRZ dnddPdodSZ dpddPdqdUZ dpddPdqdVZdS)r ImageDrawNImageFont.BaseImageFont | Nonefontim Image.Imagemode str | NonereturnNonec|d}||j}||jkr%|dkr|jdkrd}nd}t||dkr |j|_nd|_||_|j|_t j|j||_||_|dvr |j d|_ n|j d |_ |d vrd |_ 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. rNRGBARGBrz mode mismatchP)IF)1r!r"r#r%LF) _ensure_mutabler ValueErrorpalette_imagerrcoredrawdraw_inkinkfontmodefill)selfrrblendmsgs ?C:\PYTHON\MyICR_Suite\python\Lib\site-packages\PIL/ImageDraw.py__init__zImageDraw.__init__@s   <7D 27??v~~"'U"2"2% oo% 3;;:DLLDL %JOODGU33  :  y))!,,DHHy))"--DH ' ' 'DMMDM ImageFont.BaseImageFontcN|jstj|_|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.)rr load_default)r1s r4getfontzImageDraw.getfonths&(y 1!.00DIyr6 font_size float | NonecV|tj|S|SN)r r9r:)r1r;s r4_getfontzImageDraw._getfonts'  ))44 4<<>> !r6r. _Ink | Noner0tuple[int | None, int | None]chd}d}|||jr |j}n|j}n|t|trt j||j}|jr5t|tr |j||j }|j |}|t|trt j||j}|jr5t|tr |j||j }|j |}||fSr>) r0r. isinstancestrrgetcolorrr)tupler*r,r-)r1r.r0 result_ink result_fills r4_getinkzImageDraw._getinks,  ;4<y &"h !X c3''>$-c49==C<BJsE$:$:B,//T[AAC!Y//44 dC((@%.tTY??D<DJtU$;$;D<00t{CCD"i0066 ;&&r6rxyrstartfloatendwidthintc||\}}|&|dkr"|j|||||dSdSdS)z Draw an arc.Nr)rIr,draw_arc)r1rJrKrMr0rNr.s r4arcz ImageDraw.arcsULL&& T ?uzz I  r5#sE : : : : : ?zzr6 Sequence[int]bitmapc|||\}}||}|#|j||j|dSdS)zDraw a bitmap.N)loadrIr, draw_bitmapr)r1rJrTr0r.s r4rTzImageDraw.bitmaps^  LL&& T ;C ? I ! !"fi 5 5 5 5 5 ?r6outlinec|||\}}||j||||d|-||kr)|dkr%|j||||d|dSdSdSdS)z Draw a chord.Nrr)rIr, draw_chord r1rJrKrMr0rXrNr.fill_inks r4chordzImageDraw.chords Wd33 X   I UC1 = = = ?sh5A:: I UCa ? ? ? ? ? ?::r6c|||\}}||j||d|+||kr'|dkr#|j||d|dSdSdSdS)zDraw an ellipse.Nrr)rIr, draw_ellipser1rJr0rXrNr.r\s r4ellipsezImageDraw.ellipses Wd33 X   I " "2x 3 3 3 ?sh5A:: I " "2sAu 5 5 5 5 5 ?::r6Sequence[float]radiusc|d|z |d|z |d|z|d|zf}|||||dS)z4Draw a circle given center coordinates and a radius.rrN)ra)r1rJrcr0rXrN ellipse_xys r4circlezImageDraw.circlesPefnbefnbefnbefnU  Zw66666r6jointc(||d}|idkrd|j||dkrCdkr>tdtt fr,t ttt}n*fdtdtdD}tdt|dz D]}||}d||dz |f|||dzffD} | d| dkrBdfd } | d| dko| ddz | dkp&| d| dko| ddz| dk} |ddz z dz|ddz z dzf|ddz zdz |ddz zdz fg} | r| ddz| ddz}} n| ddz | ddz }} | | | dz |dz |dkrq| r,| || ddz|| || ddzg}n+| || ddz || || ddz g}| ||ddSdSdSdSdS)z6Draw a line, or a connected sequence of line segments.rNcurvec g|]:}tttt||dz;S))rrrLrF).0irJs r4 z"ImageDraw.line..sKXe_eBq1q5yM.B.BCCr6rlrc g|]M\}}tjtj|d|dz |d|dz dzNS)rrh)mathdegreesatan2)rmrKrMs r4roz"ImageDraw.line..sg'E3 TZAq0A58cRSfCT%U%UVVr6coordrbanglerLrtuple[float, ...]c |\}}|dz}dz dz }td||tjtj|zf||tjtj|zffDS)NZrlrc3~K|]8\}}||dkrtj|ntj|zV9dS)rN)rrfloorceil)rmpp_ds r4 z9ImageDraw.line..coord_at_angle.. s[%% &3C!GGC3P%%%%%%r6)rFrrcosradianssin)rurvxydistancerNs r4coord_at_anglez&ImageDraw.line..coord_at_angles %1 #(19q=$%%"#Htx U8K8K/L/L$L M!"Htx U8K8K/L/L$L M+%%%  r6ry)rN)rurbrvrLrrw) rIr, draw_linesrClistrFrrrLrangelenpiesliceline)r1rJr0rNrgr.pointsrnpointanglesrflippedcoordsrKrM gap_coordss ` ` r4rzImageDraw.linesCll4  # ?uzz I S% 0 0 0EAIIbedE]33!(8E?";R@@FF!&q#b''1!5!5Fq#f++/227=7=A"1IE$AE]E2"F1q5M2+FayF1I--       q F1I-M&)c/F1I2MQ )fQi/OF1IOfQi4OqEAI-158eai3G!3KLqEAI-158eai3G!3KLFF&,Qi"nfQi"ns&,Qi"nfQi"nsMM&%"*cBhEEEqyy"  .ufQi"n E E % .ufQi"n E E*JJ!/ufQi"n E E % .ufQi"n E E*J  *d! <<|z dz |zdzkr,j|zdz|z dz fdss|zg} dr| dxx|dzz cc<dr| dxx|dzzcc<j| d|z g} dr| dxx|dzz cc<dr| dxx|dzzcc<j| dkrdkr| dszdz g} dr| dxx|dzz cc<dr| dxx|dzzcc<j| dz dzg} dr| dxx|dzz cc<dr| dxx|dzzcc<j| dsĉzdz g} dr| dxx|dzz cc<dr| dxx|dzzcc<j| dz dzg} dr| dxx|dzz cc<dr| dxx|dzzcc<j| ddSdSdSdSdS)zDraw a rounded rectangle.rz&x1 must be greater than or equal to x0z&y1 must be greater than or equal to y0N)TTTTrl)FFrrboolrrc  r z zfddf z zfddff}nr z zfddf z zfddff}n`tfdt z zfddf z zfddf z z fddf z zfddffD}|D]-}|r jj|dfz jj| fz.dS)Nrrqrryc32K|]\}}| |VdSr>)rmrnpartrs r4rzDImageDraw.rounded_rectangle..draw_corners..sG  4qz       r6r)rF enumerater,rrQ)rpartsrrdr\full_xfull_yr.r1rNx0x1y0y1s r4 draw_cornersz1ImageDraw.rounded_rectangle..draw_cornerss "b1fb1f-sC8"q&"q&"-q#6 "b1fb1f-r371fb"b1f-sB7     #, "b1fb1f5sC@ 1fb"b1f5sC@ 1fb1fb"5q"= "q&"q&"5r3? $$      ? ??+DI+dh].BDDD&DI&e )<>>>  ? ?r6TrF)rrrr)rCrrFrrrLr(minroundallraanyrrOrIr,r)r1rJrcr0rXrNrr3rrleftrighttopbottomrr\rrr.rrrrs` `` @@@@@@@@@r4rounded_rectanglezImageDraw.rounded_rectangles bedE] + + 7!%hx&?!D!D HRhr22!(5/266NBB 77:CS// ! 77:CS// ! ?.G Rb&1* - - 2YY 2YY 2YY 2YY% w<< >"r'A+%F G"r'A+%F G >& >||Bgu=== 66W6>>"dGU;; ; QKK Wd33 X ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?D   L    X (("b1fqj"b1fqj)I8UVWWWWa!rAvz)) (("q&1*b"q&1*b)I8UVWWW =& =BQ+1:%GGGq1u$GGG1:%GGGq1u$GGG ((x;;;aR,1:&!HHHA%HHH1:&!HHHA%HHH ((!<<< ?sh5A:: L    92r2:>21:$FFFa!eOFFF1:$FFFa!eOFFF ((c1555b5j1nb"51:'1IIIQ&III1:'1IIIQ&III ((a888 8BU Q31:%GGGq1u$GGG1:%GGGq1u$GGG ((sA666eaR41:&!HHHA%HHH1:&!HHHA%HHH ((Q77777= ?::" 8 8r6rjrFtuple[float, float]textAnyStr | ImageText.Text[AnyStr]anchorspacingalignrD directionfeatureslist[str] | Nonelanguage stroke_width stroke_fillembedded_colorrargsr kwargsc ` t|tjr|nw|(d}tj||j|  r| r| | dfd }||}|dSd}jrj |j n|} |||D]Add  f d }|#||j||kr ||6||BdS)z Draw text.Nr;r0r@rrOcH|\}}||J|S|Sr>)rI)r0r.r\r1s r4getinkzImageDraw.text..getink2s3 LL..MC{+++Jr6rr.rrLrc  j}|dkr rd}tj}tj}t jjdt jjdf}t jtj rDjj j | |j ||g Rddi\}}||dz }||dz }nZ jj j | |j |g Rd|i}n/#t$r"j j }YnwxYw|dkr||d}}t!jd|d} |d| j>j|||||jdz||jdzf|dSdSj||f||dS) Nrr stroke_filledTrrKrrn)r/rOrrrrmodfrCrr FreeTypeFontgetmask2rrgetmask TypeErrorgetbandstructpackfillbandrpasterr,rW)r.rrrrrKroffsetcolor ink_alpharrrr image_textrrrr1s r4 draw_textz!ImageDraw.text..draw_textGs`}1$$$!DKKKK46**1-ty/@/@/CDjoy/EFF!B#;:?#; !  $  $ $ $ $'+ $! $ $LD&NANAAB6z6 I %$$( K "   #( %  %BBB)66tyAAB6>>#' Q4E & C 5 5a 8INN1i000w* !Aq!dil*:A ! >>*$$IcNNN #EB B r6r;rcH|||||||||| | | | | |SNr)r)r1rJrr0rrrrrrrrrrr;s r4multiline_textzImageDraw.multiline_textsI$yy                r6c|||}tj|||j|||}|r||S)z@Get the length of a given string, in pixels with 1/64 precision.N)rrr)r?r rrr get_length) r1rrrrrrr;rs r4 textlengthzImageDraw.textlengthsr <==++D^   I      %  " " $ $ $$$&&&r6!tuple[float, float, float, float]c  ||| }tj|||j|||| } | r| | r| | | |||S)z2Get the bounding box of a given string, in pixels.)r?r rrrrget_bbox)r1rJrrrrrrrrrrr;rs r4textbboxzImageDraw.textbboxs" <==++D^ $ 7Ix    %  " " $ $ $  ,   l + + +""2vu555r6c D|||||||||| | | |  Sr)r) r1rJrrrrrrrrrrr;s r4multiline_textbboxzImageDraw.multiline_textbboxsC }}              r6r>)rrrrrr)rr7)r;r<rr7)r.r@r0r@rrA)Nr) rJrrKrLrMrLr0r@rNrOrr)rJrSrTrr0r@rr)NNr)rJrrKrLrMrLr0r@rXr@rNrOrr) rJrr0r@rXr@rNrOrr) rJrbrcrLr0r@rXr@rNrOrr)NrN) rJrr0r@rNrOrgrrr)NN)rrr0r@rXr@rr)rJrr0r@rr)rNNr)rrrrOrrLr0r@rXr@rNrOrr)rJrrcrLr0r@rXr@rNrOrrrr) NNNrjrNNNrNF) rJrrrr0r@rrrrrrLrrDrrrrrrrrLrr@rrrr rr rr)rJrrrr0r@rrrrrrLrrDrrrrrrrrLrr@rrr;r<rr)NNNNF)rrrrrrrrrrrrr;r<rrL) NNrjrNNNrF)rJrrrrrrrrrLrrDrrrrrrrrLrrr;r<rr)__name__ __module__ __qualname__r__annotations__r5r:r?rIrRrTr]rarfrrrrrrrrrrrrrrr6r4rr=s+/D////&&&&&P2""""59'''''<! ; ; ; ; ;KO 6 6 6 6 6 !#@@@@@&!# 6 6 6 6 6$!# 7 7 7 7 7! K=K=K=K=K=`!# 2 2 2 2 2&!#CCCCC +++++!# KKKKK8 # / / / / / !# 8 8 8 8 8" # J89=J8J8J8J8J8J8`!/3! $%)##'$uuuuuv!/3! $%)##'$! #'!! ! ! ! ! ! L04 $%)#$'#'''''''<04! $%)#$6#'666666@04! $%)#$ #'        r6rrrrrrcv t|d|S#t$rt||cYSwxYw)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)getattrAttributeErrorr)rrs r4rrsR#%wr9%%d+++ ###T"""""#s 88Image.Image | None)tuple[ImageDraw2.Draw | None, ModuleType]c>ddlm}||j|nd}||fS)zm :param im: The image to draw in. :returns: A (drawing context, drawing resource factory) tuple. rrN)rr)rrr,s r4r r s; "$.?:?2   dD  r6imagerJtuple[int, int]valuefloat | tuple[int, ...]borderfloat | tuple[int, ...] | NonethreshrLrcz|}|J|\}} |||f}t|||krdS||||f<n#ttf$rYdSwxYw||fh} t } | rt } | D]\}}|dz|f|dz |f||dzf||dz ffD]\} } | | f| vs | dks| dkr || | f}| | | f|t|||k}n|||fv}|r||| | f<| | | fx#ttf$rYwxYw| } | } | 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. Nrr)rV _color_diffr( IndexErrorsetadd)rrJrrrpixelrr backgroundedge full_edgenew_edgestr}r0s r4 floodfillr#s6 JJLLE     DAq1a4[ uj ) )V 3 3 Fad  # F8DI 55 - -DAqa%a!eQZ!QUaQZH - -1q6Y&&!a%%1q55 -ad AMM1a&)))~*1j99VC 7-&+ad  aV,,,#J/D  -" + s(AAAA< DD/.D/rrrrOrlist[tuple[float, float]]c t|tsd}t||dkrd}t|t|tt fsd}t|t |dkr\td|Dsd}t|tttt |^ nt |dkrt|dtt frtd |dDr"t|d ttfsd}t|t |ddkrd }t|tttt |dtt|d  nd }t| dkrd }t|t|ttfsd}t|dfd d fd dd}|||}fd|DS) 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 intrzn_sides should be an int > 2z$bounding_circle should be a sequencec3NK|] }t|ttfV!dSr>rCrOrLrmrns r4rz4_compute_regular_polygon_vertices..s0HH1:a#u..HHHHHHr6z0bounding_circle should only contain numeric datarlrc3NK|] }t|ttfV!dSr>r'r(s r4rz4_compute_regular_polygon_vertices..sA  ,-Jq3, ' '      r6rzBbounding_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]rsrLrrc t|dtjtjd|z z|dtjtjd|z zz dzdt|dtjtjd|z z|dtjtjd|z zzdzdfS)Nrrqrrl)rrrrrr)rrscentroids r4_apply_rotationz:_compute_regular_polygon_vertices.._apply_rotations a48DLw$?$?@@@(TXdl3=&A&ABBBC1+    a48DLw$?$?@@@(TXdl3=&A&ABBBC1+     r6rvc$dg}||SNrr)rv start_pointr-polygon_radiuss r4_compute_polygon_vertexzB_compute_regular_polygon_vertices.._compute_polygon_vertexs%q) {E222r6rrOrcg}d|z }dd|zz |z}t|D]'}||||z }|dkr|dz}(|S)Nrqrg?)rappend)rrrrs current_angle_s r4 _get_anglesz6_compute_regular_polygon_vertices.._get_anglesso-sW},8 w % %A MM- ( ( ( W $Ms""$  r6c&g|] }|Srr)rmrvr2s r4roz5_compute_regular_polygon_vertices..s% ? ? ?u # #E * * ? ? ?r6)rr*rsrLrr)rvrLrr)rrOrrLrr*) rCrOrr(rrFrrrrL) rrrr3r7rr-r2r,r1s @@@@r4rr_sV gs # #(nn{{,oo oe} 5 54nn ?q  HHHHHHH "DCS// !$(ed?6K6K$L$L!>> _   " "z/!2DtUm'T'T "  1@1C      "OA.e == "ECS// ! q! " "a ' 'VCS// !U T/!*<%=%=>>e_Q%788 < oo4oo he - -2oo      3333333    [( + +F @ ? ? ? ? ? ??r6color1color2ct|tr|n|ft|tr|n|ftfdtt DS)zJ Uses 1-norm distance to calculate difference between two values. c3TK|]"}t||z V#dSr>)abs)rmrnfirstseconds r4rz_color_diff..s8EEQs58fQi'((EEEEEEr6)rCrFsumrr)r9r:r>r?s @@r4rrsn !// >FFfYE!&%00 ?VVviF EEEEE%F 2D2DEEE E EEr6r>)rrrrrr)rr rr r/) rrrJrrrrrrrLrr)rrrrOrrLrr$)r9rr:rrrL)! __future__rrrrcollections.abcrtypingrrrrr r TYPE_CHECKINGr typesr r rr_typingrrr+rXrrrrr r#rrrr6r4rGs@#"""""" $$$$$$555555555555 &((((((      """"""""%%%%%%%%.3Z-?????B  B  B  B  B  B  B  B  J#####".2 >>>>>B@@@@D F F F F F Fr6