a :jg Iã@sÚddlmZddlZddlZddlmZddlmZddlm Z m Z m Z m Z e rhddl mZddlmZGd d „d ƒZGd d „d eƒZGd d„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd „d eƒZGd!d"„d"eƒZGd#d$„d$eƒZGd%d&„d&eƒZGd'd(„d(eƒZ Gd)d*„d*eƒZ!Gd+d,„d,eƒZ"Gd-d.„d.eƒZ#Gd/d0„d0eƒZ$Gd1d2„d2eƒZ%Gd3d4„d4eƒZ&Gd5d6„d6eƒZ'dS)7é)Ú annotationsN)ÚSequence)Ú ModuleType)Ú TYPE_CHECKINGÚAnyÚCallableÚcasté)Ú_imaging)Ú NumpyArrayc@s"eZdZejdddœdd„ƒZdS)ÚFilterú_imaging.ImagingCore©ÚimageÚreturncCsdS©N©©Úselfrrrú9/usr/local/lib/python3.9/site-packages/PIL/ImageFilter.pyÚfiltersz Filter.filterN)Ú__name__Ú __module__Ú __qualname__ÚabcÚabstractmethodrrrrrr sr c@s eZdZdS)ÚMultibandFilterN)rrrrrrrr$src@s&eZdZUded<dddœdd„ZdS)Ú BuiltinFilterztuple[Any, ...]Ú filterargsr rcCs"|jdkrd}t|ƒ‚|j|jŽS)NÚPúcannot filter palette images)ÚmodeÚ ValueErrorrr©rrÚmsgrrrr+s zBuiltinFilter.filterN)rrrÚ__annotations__rrrrrr(s rc@s,eZdZdZdZd dddddd œd d „ZdS) ÚKernelaº Create a convolution kernel. This only supports 3x3 and 5x5 integer and floating point kernels. Kernels can only be applied to "L" and "RGB" images. :param size: Kernel size, given as (width, height). This must be (3,3) or (5,5). :param kernel: A sequence containing kernel weights. The kernel will be flipped vertically before being applied to the image. :param scale: Scale factor. If given, the result for each pixel is divided by this value. The default is the sum of the kernel weights. :param offset: Offset. If given, this value is added to the result, after it has been divided by the scale factor. Nrztuple[int, int]zSequence[float]z float | NoneÚfloatÚNone)ÚsizeÚkernelÚscaleÚoffsetrcCsN|durt dd„|¡}|d|dt|ƒkrMóz!Kernel.__init__..rr z!not enough coefficients in kernel)Ú functoolsÚreduceÚlenr"r)rr)r*r+r,r$rrrÚ__init__Ds zKernel.__init__)Nr©rrrÚ__doc__Únamer4rrrrr&2s ûr&c@s6eZdZdZdZddddœdd„Zddd œd d „Zd S) Ú RankFilterao Create a rank filter. The rank filter sorts all pixels in a window of the given size, and returns the ``rank``'th value. :param size: The kernel size, in pixels. :param rank: What pixel value to pick. Use 0 for a min filter, ``size * size / 2`` for a median filter, ``size * size - 1`` for a max filter, etc. ZRankÚintr()r)ÚrankrcCs||_||_dSr©r)r:)rr)r:rrrr4aszRankFilter.__init__r rcCs>|jdkrd}t|ƒ‚| |jd|jd¡}| |j|j¡S)Nrr é)r!r"Úexpandr)Z rankfilterr:r#rrrres  zRankFilter.filterN©rrrr6r7r4rrrrrr8Ts r8c@s&eZdZdZdZd dddœdd„Zd S) Ú MedianFilterz‘ Create a median filter. Picks the median pixel value in a window with the given size. :param size: The kernel size, in pixels. ZMedianér9r(©r)rcCs||_||d|_dS)Nr<r;©rr)rrrr4wszMedianFilter.__init__N)r@r5rrrrr?msr?c@s&eZdZdZdZd dddœdd„Zd S) Ú MinFilterz Create a min filter. Picks the lowest pixel value in a window with the given size. :param size: The kernel size, in pixels. ZMinr@r9r(rAcCs||_d|_dS)Nrr;rBrrrr4†szMinFilter.__init__N)r@r5rrrrrC|srCc@s&eZdZdZdZd dddœdd„Zd S) Ú MaxFilterz Create a max filter. Picks the largest pixel value in a window with the given size. :param size: The kernel size, in pixels. ZMaxr@r9r(rAcCs||_||d|_dS)Nr r;rBrrrr4•szMaxFilter.__init__N)r@r5rrrrrD‹srDc@s6eZdZdZdZddddœdd„Zd d d œd d „Zd S)Ú ModeFiltera! Create a mode filter. Picks the most frequent pixel value in a box with the given size. Pixel values that occur only once or twice are ignored; if no pixel value occurs more than twice, the original pixel value is preserved. :param size: The kernel size, in pixels. ZModer@r9r(rAcCs ||_dSr)r)rBrrrr4¥szModeFilter.__init__r rcCs | |j¡Sr)Z modefilterr)rrrrr¨szModeFilter.filterN)r@r>rrrrrEšsrEc@s6eZdZdZdZd dddœdd„Zddd œd d „Zd S)Ú GaussianBlurafBlurs the image with a sequence of extended box filters, which approximates a Gaussian kernel. For details on accuracy see :param radius: Standard deviation of the Gaussian kernel. Either a sequence of two numbers for x and y, or a single number for both. r<úfloat | Sequence[float]r(©ÚradiusrcCs ||_dSr)rI)rrIrrrr4·szGaussianBlur.__init__r rcCs6|j}t|ttfƒr||f}|dkr,| ¡S| |¡S©N)rr)rIÚ isinstancer9r'ÚcopyZ gaussian_blur©rrÚxyrrrrºs zGaussianBlur.filterN)r<r>rrrrrF¬srFc@s4eZdZdZdZdddœdd„Zdddœd d „Zd S) ÚBoxBluracBlurs the image by setting each pixel to the average value of the pixels in a square box extending radius pixels in each direction. Supports float radius of arbitrary size. Uses an optimized implementation which runs in linear time relative to the size of the image for any radius value. :param radius: Size of the box in a direction. Either a sequence of two numbers for x and y, or a single number for both. Radius 0 does not blur, returns an identical image. Radius 1 takes 1 pixel in each direction, i.e. 9 pixels in total. rGr(rHcCsHt|ttfƒr|n||f}|ddks2|ddkr>d}t|ƒ‚||_dS)Nrr zradius must be >= 0)rKÚtupleÚlistr"rI)rrIrNr$rrrr4Ós zBoxBlur.__init__r rcCs6|j}t|ttfƒr||f}|dkr,| ¡S| |¡SrJ)rIrKr9r'rLZbox_blurrMrrrrÚs zBoxBlur.filterNr>rrrrrOÃs rOc@s:eZdZdZdZddddddœd d „Zd d d œd d„ZdS)Ú UnsharpMaskašUnsharp mask filter. See Wikipedia's entry on `digital unsharp masking`_ for an explanation of the parameters. :param radius: Blur Radius :param percent: Unsharp strength, in percent :param threshold: Threshold controls the minimum brightness change that will be sharpened .. _digital unsharp masking: https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking r<é–r@r'r9r()rIÚpercentÚ thresholdrcCs||_||_||_dSr)rIrTrU)rrIrTrUrrrr4ôszUnsharpMask.__init__r rcCs| |j|j|j¡Sr)Z unsharp_maskrIrTrUrrrrrûszUnsharpMask.filterN)r<rSr@r>rrrrrRãs ÿrRc@seZdZdZdZdS)ÚBLURZBlur)©érXér)r r r r r r rrrr r rrrr r rrrr r r r r r N©rrrr7rrrrrrVÿsrVc@seZdZdZdZdS)ÚCONTOURZContour)©r@r@r éÿ© éÿÿÿÿr_r_r_ér_r_r_r_NrZrrrrr[ sr[c@seZdZdZdZdS)ÚDETAILZDetail)r\ér) rr_rr_é r_rr_rNrZrrrrrasrac@seZdZdZdZdS)Ú EDGE_ENHANCEz Edge-enhance)r\r<r) r_r_r_r_rcr_r_r_r_NrZrrrrrd"srdc@seZdZdZdZdS)ÚEDGE_ENHANCE_MOREzEdge-enhance More)r\r r) r_r_r_r_é r_r_r_r_NrZrrrrre-srec@seZdZdZdZdS)ÚEMBOSSZEmboss)r\r é€) r_rrrr rrrrNrZrrrrrg8srgc@seZdZdZdZdS)Ú FIND_EDGESz Find Edges)r\r rr^NrZrrrrriCsric@seZdZdZdZdS)ÚSHARPENZSharpen)r\rYr) éþÿÿÿrkrkrké rkrkrkrkNrZrrrrrjNsrjc@seZdZdZdZdS)ÚSMOOTHZSmooth)r\é r) r r r r rXr r r r NrZrrrrrmYsrmc@seZdZdZdZdS)Ú SMOOTH_MOREz Smooth More)rWédr)r r r r r r rXrXrXr r rXé,rXr r rXrXrXr r r r r r NrZrrrrrodsroc@s”eZdZdZdZd%ddddd d d œd d „Zedddœdd„ƒZed&ddddddœdd„ƒZ d'dd ddddœdd„Z ddœdd „Z d!d!d"œd#d$„Z dS)(Ú Color3DLUTaŒThree-dimensional color lookup table. Transforms 3-channel pixels using the values of the channels as coordinates in the 3D lookup table and interpolating the nearest elements. This method allows you to apply almost any color transformation in constant time by using pre-calculated decimated tables. .. versionadded:: 5.2.0 :param size: Size of the table. One int or tuple of (int, int, int). Minimal size in any dimension is 2, maximum is 65. :param table: Flat lookup table. A list of ``channels * size**3`` float elements or a list of ``size**3`` channels-sized tuples with floats. Channels are changed first, then first dimension, then second, then third. Value 0.0 corresponds lowest value of output, 1.0 highest. :param channels: Number of channels in the table. Could be 3 or 4. Default is 3. :param target_mode: A mode for the result image. Should have not less than ``channels`` channels. Default is ``None``, which means that mode wouldn't be changed. z Color 3D LUTr@Nzint | tuple[int, int, int]z6Sequence[float] | Sequence[Sequence[int]] | NumpyArrayr9z str | NoneÚboolr()r)ÚtableÚchannelsÚ target_modeÚkwargsrc Ks¾|dvrd}t|ƒ‚| |¡|_}||_||_| dd¡}|d|d|d}d} d} t|d ƒr†z ddl} Wnty„Yn0| rêt || j ƒrê|} |r¦|   ¡} | j ||f||f|d|d|d|ffvrä|   ||¡}nd} nx|röt|ƒ}|rbt |dttfƒrbtttt|ƒ} g} | D]2}t|ƒ|krPd |›d }t|ƒ‚|  |¡q*| }| szt|ƒ||kr´d |›d |d›d |d›d |d›dt|ƒ› }t|ƒ‚||_dS)N©r@éú)Only 3 or 4 output channels are supportedÚ _copy_tableTrr r<FÚshapez2The elements of the table should have a length of Ú.z„The table should have either channels * size**3 float items or size**3 items of channels-sized tuples with floats. Table should be: Úxz. Actual length: )r"Ú _check_sizer)rur!ÚgetÚhasattrÚnumpyÚ ImportErrorrKZndarrayrLr|ZreshaperQrPrrr9r3Úextendrt)rr)rtrurvrwr$Z copy_tableÚitemsZ wrong_sizer‚Z numpy_tableZ raw_tableZ flat_tableZpixelrrrr4Œsn    ýÿÿþþþþýÿzColor3DLUT.__init__rztuple[int, int, int]rAc Csžz|\}}}WnLty@}zd}t|ƒ|‚WYd}~n$d}~0tyZ|||f}Yn0tdd„|Dƒƒ}|D]&}d|krŠdksrnd}t|ƒ‚qr|S)Nz>Size should be either an integer or a tuple of three integers.css|]}t|ƒVqdSr)r9)Ú.0r~rrrÚ Ûr0z)Color3DLUT._check_size..r<éAz Size should be in [2, 65] range.)r"Ú TypeErrorrP)r)Ú_Úer$Úsize_1drrrrÒs  zColor3DLUT._check_sizez2Callable[[float, float, float], tuple[float, ...]])r)Úcallbackrurvrc Cs¸| |¡\}}}|dvr$d}t|ƒ‚dg||||} d} t|ƒD]X} t|ƒD]J} t|ƒD]<} || |d| |d| |dƒ| | | |…<| |7} q^qRqF||||f| ||ddS)aSGenerates new LUT using provided callback. :param size: Size of the table. Passed to the constructor. :param callback: Function with three parameters which correspond three color channels. Will be called ``size**3`` times with values from 0.0 to 1.0 and should return a tuple with ``channels`` elements. :param channels: The number of channels which should return callback. :param target_mode: Passed to the constructor of the resulting lookup table. rxrzrr F©rurvr{)rr"Úrange)Úclsr)rrurvrŒÚsize_2dÚsize_3dr$rtÚidx_outr.ÚgÚrrrrÚgenerateâs(   ÿûzColor3DLUT.generateFz Callable[..., tuple[float, ...]]z int | None)rÚ with_normalsrurvrc Cs|dvrd}t|ƒ‚|j}|p |}|j\}} } dg|| | |} d} d} t| ƒD]Œ}t| ƒD]~}t|ƒD]p}|j| | |…}|r´|||d|| d|| dg|¢RŽ}n||Ž}|| | | |…<| |7} | |7} qlq`qTt|ƒ|j| ||pø|jddS)a#Transforms the table values using provided callback and returns a new LUT with altered values. :param callback: A function which takes old lookup table values and returns a new set of values. The number of arguments which function should take is ``self.channels`` or ``3 + self.channels`` if ``with_normals`` flag is set. Should return a tuple of ``self.channels`` or ``channels`` elements if it is set. :param with_normals: If true, ``callback`` will be called with coordinates in the color cube as the first three arguments. Otherwise, ``callback`` will be called only with actual color values. :param channels: The number of channels in the resulting lookup table. :param target_mode: Passed to the constructor of the resulting lookup table. )Nr@ryrzrr FrŽ)r"rur)rrtÚtyper!)rrr—rurvr$Zch_inZch_outrŒr‘r’rtZidx_inr“r.r”r•ÚvaluesrrrÚ transform s@       ýü ûzColor3DLUT.transformÚstr)rcCsX|jj›d|jjj›dj|jŽd|jd›g}|jrH| d|j›¡d d |¡¡S)Nz from zsize={:d}x{:d}x{:d}z channels=Údz target_mode=z<{}>ú ) Ú __class__rrtÚformatr)rur!ÚappendÚjoin)rr•rrrÚ__repr__Hs  ýzColor3DLUT.__repr__r rc CsDddlm}| |jp|j|jj|j|jd|jd|jd|j¡S)Nr )ÚImagerr<) Úr£Z color_lut_3dr!Z ResamplingZBILINEARrur)rt)rrr£rrrrRs  ùzColor3DLUT.filter)r@N)r@N)FNN) rrrr6r7r4Ú staticmethodrÚ classmethodr–ršr¢rrrrrrrqs"ûFû,û< rr)(Ú __future__rrr1Úcollections.abcrÚtypesrÚtypingrrrrr¤r Z_typingr r rrr&r8r?rCrDrErFrOrRrVr[rardrergrirjrmrorrrrrrÚs>      "