)EjNgddlmZddlZddlZddlZddlmZddlmZm Z m Z m Z ddl m Z mZmZdUd ZdVdZdWdZ dXdYdZ dZd[d+Zejjfd\d/Zejjfd\d0Zejjdd1fd]d4Zd^d_d5Zejjfd`d9ZGd:d;e Zejjfdad=Zdbdcd>Z ddded@Z ejjdAd1fdfdCZ!dgdDZ"dgdEZ#dgdFZ$dgdGZ%dhdIZ&didjdLZ'e dkdPZ(e ddQdldSZ(ddQdmdTZ(dS)n) annotationsN)Sequence)LiteralProtocolcastoverload)ExifTagsImage ImagePaletteborderint | tuple[int, ...]returntuple[int, int, int, int]ct|trJt|dkr |x\}}\}}n4t|dkr|\}}}}nd}t||x}x}x}}||||fS)Nz=border must be an integer, or a tuple of two or four elements) isinstancetuplelen ValueError)r lefttoprightbottommsgs >C:\PYTHON\MyICR_Suite\python\Lib\site-packages\PIL/ImageOps.py_borderr!s&%  - v;;!  (. .ID# vv [[A  '- $D#uffQCS// !&,,,s,UV eV ##colorstr | int | tuple[int, ...]modestrcht|trddlm}|||}|S)Nr ) ImageColor)rr#r%getcolor)r r"r%s r_colorr(/s@%1      ##E400 Lrimage Image.Imagelut list[int]c|jdkrd}t||jdvr;|jdkrt|dkr||z|z}||Sd|j}t |)NPzmode P support coming soon)LRGBr0znot supported for mode )r"NotImplementedErrorrpointOSError)r)r+rs r_lutr57s zS*!#&&& | # # :  3s88s??)c/C{{34 44cllrFcutofffloat | tuple[float, float]ignoreint | Sequence[int] | NonemaskImage.Image | None preserve_toneboolc|r)|d|}n||}g}tdt|dD]}|||dz}|%t |t rd||<n |D]} d|| <|rt |t s||f}d} tdD] } | || z} t | |dzdz} tdD]9} | || kr| || z } d|| <n|| xx| zcc<d} | dkrn:t | |dzdz} tdddD]9} | || kr| || z } d|| <n|| xx| zcc<d} | dkrn:tdD] } || rn tdddD] } || rn | | kr1|ttdd | | z z }| |z}tdD]=} t | |z|z} | dkrd} n| dkrd} | | > t||S) a Maximize (normalize) image contrast. This function calculates a histogram of the input image (or mask region), removes ``cutoff`` percent of the lightest and darkest pixels from the histogram, and remaps the image so that the darkest pixel becomes black (0), and the lightest becomes white (255). :param image: The image to process. :param cutoff: The percent to cut off from the histogram on the low and high ends. Either a tuple of (low, high), or a single number for both. :param ignore: The background pixel value (use None for no background). :param mask: Histogram used in contrast operation is computed using pixels within the mask. If no mask is given the entire image is used for histogram computation. :param preserve_tone: Preserve image tone in Photoshop-like style autocontrast. .. versionadded:: 8.2.0 :return: An image. r/rr1Ndr go@) convert histogramrangerrintrextendlistappendr5)r)r6r8r:r<rCr+layerhixncutlohiscaleoffsets r autocontrastrRIs8*MM#&&0066 OOD)) Cq#i..#..:: eeck) *  &#&& &  BAbEE  fe,, * &)ACjj  "Ia&)ms*++CCjj  2;;"+CAbEEbEEESLEEEC!88Ea&)ms*++CCR((  2;;"+CAbEEbEEESLEEEC!88E**  Bu  R$$  Bu   88 JJtE#JJ'' ( ( ( (R"W%ES5[FCjj  ef,--66BB#XXB 2   s  rr@blackstr | tuple[int, ...]whitemid"str | int | tuple[int, ...] | None blackpointrE whitepointmidpointc|jdksJ|d|cxkr |cxkrdksnJnd|cxkr|cxkr |cxkrdksnJtttt |d}tttt |d}|.tttt |dnd} g} g} g} t |D]S} | |d| |d| |dT| t ||z }|D]} | |d| |d|dz zt|zz| |d| |d|dz zt|zz| |d| |d|dz zt|zzÐnt ||z }t ||z }|D]} | |d| | d|dz zt|zz| |d| | d|dz zt|zz| |d| | d|dz zt|zz|D]} | | d| |d| dz zt|zz| | d| |d| dz zt|zz| | d| |d| dz zt|zzt d|z D]S} | |d| |d| |dT|d}t|| | z| zS) a Colorize grayscale image. This function calculates a color wedge which maps all black pixels in the source image to the first color and all white pixels to the second color. If ``mid`` is specified, it uses three-color mapping. The ``black`` and ``white`` arguments should be RGB tuples or color names; optionally you can use three-color mapping by also specifying ``mid``. Mapping positions for any of the colors can be specified (e.g. ``blackpoint``), where these parameters are the integer value corresponding to where the corresponding color should be mapped. These parameters must have logical order, such that ``blackpoint <= midpoint <= whitepoint`` (if ``mid`` is specified). :param image: The image to colorize. :param black: The color to use for black input pixels. :param white: The color to use for white input pixels. :param mid: The color to use for midtone input pixels. :param blackpoint: an int value [0, 255] for the black mapping. :param whitepoint: an int value [0, 255] for the white mapping. :param midpoint: an int value [0, 255] for the midtone mapping. :return: An image. r/Nrr@r0r rr1) r"rrrEr(rDrHrrBr5)r)rTrVrWrYrZr[ rgb_black rgb_whitergb_midredgreenbluei range_map range_map1 range_map2s rcolorizergsB :     {J3333*33333333333J????(????j????C??????Xc]F5%$8$899IXc]F5%$8$899I9<d8C=&e"4"4555dG C E D:  "" 9Q<    Yq\""" IaL!!!!*z122   A JJ! qIaL9Q<$?@C NNRR    LL! qIaL9Q<$?@C NNRR    KK! qIaL9Q<$?@C NNRR     8j011 :011   A JJ! qGAJ1$=>#j//QQ    LL! qGAJ1$=>#j//QQ    KK! qGAJ1$=>#j//QQ     Y YA JJwqzA1 )B$Cs:$VV W W W LL Q)A,";<JOO    KK Q)A,*C%DJ%WW X X X X3# $ $"" 9Q<    Yq\""" IaL!!!! MM% E sU{T) * **rsizetuple[int, int]methodcv|j|jz }|d|dz }||kr}||krzequalize..s3333333rr rAr@r) r"rBrCrDrrFrG functoolsreduceoperatoraddrHr5) r)r:rJr+bhistosteprLrcs requalizersU zS e$$ A C 1c!ffc " " % %33aAG n333 u::?? JJtE#JJ'' ( ( ( ($X\599E"IE#MD % 4c ++,,,,AIs%%AJJqDy)))Aa!eH AA% s  rfillcdt|\}}}}||jdz|z}||jdz|z}t||j} |jr|jj} t j| || } t| tr;t| dkst| dkr| | } nd} tj |j||f| } | r| | j| | |||f| S)z Add border to the image :param image: The image to expand. :param border: Border width, in pixels. :param fill: Pixel fill value (a color value). Default is 0 (black). :return: An image. rr rN)rrhr(r"rr rrrrr'r rrr) r)r rrrrrrorpr r"rrs rexpandrs" 'vD#uf 5:a= 5 (E 5:a= 6 )F 4 $ $E }}!+D%2B2B42H2HII eU # # ,UqCJJ!OO$$U++E )EJ 7 7C. w---IIedC[!!! Jrbleedc|\}}d|cxkrdksnd}d|cxkrdksnd}d|cxkrdksnd}||jdz||jdzf}|jd|ddzz |jd|ddzz f}|d|dz } |d|dz } | | kr|d} |d} n-| | kr| |dz} |d} n|d} |d| z } |d|d| z |zz} |d|d| z |zz}| || | z|| zf}||||S)a Returns a resized and cropped version of the image, cropped to the requested aspect ratio and size. This function was contributed by Kevin Cazabon. :param image: The image to resize and crop. :param size: The requested output size in pixels, given as a (width, height) tuple. :param method: Resampling method to use. Default is :py:attr:`~PIL.Image.Resampling.BICUBIC`. See :ref:`concept-filters`. :param bleed: Remove a border around the outside of the image from all four edges. The value is a decimal percentage (use 0.01 for one percent). The default value is 0 (no border). Cannot be greater than or equal to 0.5. :param centering: Control the cropping position. Use (0.5, 0.5) for center cropping (e.g. if cropping the width, take 50% off of the left side, and therefore 50% off the right side). (0.0, 0.0) will crop from the top left corner (i.e. if cropping the width, take all of the crop off of the right side, and if cropping the height, take all of it off the bottom). (1.0, 0.0) will crop from the bottom left corner, etc. (i.e. if cropping the width, take all of the crop off the left side, and if cropping the height take none from the top, and therefore all off the bottom). :return: An image. rg?r|rr r)box)rhrr)r)rhrjrr} centering_x centering_y bleed_pixels live_sizelive_size_ratio output_ratio crop_width crop_height crop_leftcrop_toprs rfitrsP )K + $ $ $ $ $ $ $ $ + $ $ $ $ $ $ $ $ %    #     EJqM)55:a=+@AL  1 Q!++ 1 Q!++I  lYq\1O7T!W$L,&&q\ l L ( (!IaL0 l q\ l\1 Q9Q<*#< "KKIA)A,"< !KKH xZ!7K9O PD <<f$< / //rcJ|tjjS)zq Flip the image vertically (top to bottom). :param image: The image to flip. :return: An image. ) transposer TransposeFLIP_TOP_BOTTOMr)s rflipra ??5?: ; ;;rc,|dS)zi Convert the image to grayscale. :param image: The image to convert. :return: An image. r/)rBrs r grayscalerks ==  rcttddd}|jdkr||nt ||S)zc Invert (negate) the image. :param image: The image to invert. :return: An image. r@rA1)rGrDr"r3r5)r)r+s rinvertrusJ uS"b!! " "C$zS005;;s   d5#6F6FFrcJ|tjjS)zq Flip image horizontally (left to right). :param image: The image to mirror. :return: An image. )rr rFLIP_LEFT_RIGHTrs rmirrorrrrbitscrdd|z zdz fdtdD}t||S)z Reduce the number of bits for each color channel. :param image: The image to posterize. :param bits: The number of bits to keep for each channel (1-8). :return: An image. rr cg|]}|zSrr)rrcr:s rrzposterize..s ( ( (1t8 ( ( (rr1)rDr5)r)rr+r:s @r posterizersI1t8_q !D ( ( ( (U3ZZ ( ( (C s  r thresholdcg}tdD]6}||kr|||d|z 7t||S)z Invert all pixel values above a threshold. :param image: The image to solarize. :param threshold: All pixels above this grayscale level are inverted. :return: An image. r1r@)rDrHr5)r)rr+rcs rsolarizersa C 3ZZ   y== JJqMMMM JJsQw     s  rin_place Literal[True]NonecdSrrr)rs rexif_transposersLOCr)rLiteral[False]cdSrrrs rrrs #rc ||}|tjjd}t jjt jj t jj t jj t jj t jj t jjd|}||r1|j||_|jj|_n||}|r|n|}|}tjj|vr|tjj=d|jvr||jd<n7d|jvr.||jd<dD]}||jvrdD] |j|} t-| t.rt1j d| } nYt-| t4rt5 fd | D} n(t1j d | } | |j|<|s|Sn|s|SdS) a If an image has an EXIF Orientation tag, other than 1, transpose the image accordingly, and remove the orientation data. :param image: The image to transpose. :param in_place: Boolean. Keyword-only argument. If ``True``, the original image is modified in-place, and ``None`` is returned. If ``False`` (default), a new :py:class:`~PIL.Image.Image` object is returned with the transposition applied. If there is no transposition, a copy of the image will be returned. r )rrrrNexifzRaw profile type exif)zXML:com.adobe.xmpxmp)ztiff:Orientation="([0-9])"z,([0-9])r&c3hK|],}tjd|V-dS)rN)resubencode)rvpatterns r z!exif_transpose..sL**EFw~~'7'7a @ @******rr)loadgetexifgetr Base Orientationr rr ROTATE_180r TRANSPOSE ROTATE_270 TRANSVERSE ROTATE_90imrrh_sizeinfotobyteshexrr#rrrrr) r)r image_exif orientationrjtransposed_image exif_imagerkeyvaluers @rrrsl JJLLLJ..!:A>>K ? * ? % ? * ? $ ? % ? % ? $ c+   7x))&11EH(-EKK$v66 &>;M;M;O;O  783 5 5*/))$ 5 5!+ 4%eS11I$&F7B$>$>EE'u55I$)****JO***%%EE%'F7>>+;+;S%$H$HE/4 ,, $# # $ zz|| 4r)r rrr)r r!r"r#rr)r)r*r+r,rr*)rNNF) r)r*r6r7r8r9r:r;r<r=rr*)Nrr@rS)r)r*rTrUrVrUrWrXrYrErZrEr[rErr*)r)r*rhrirjrErr*) r)r*rhrirjrEr rXr}r~rr*)r)r)r*r rErr*)r)r*rrrmrErr*)r)r*rrrmrErr*r)r)r*r:r;rr*)rr)r)r*r rrr!rr*) r)r*rhrirjrErrr}r~rr*)r)r*rr*)r)r*rrErr*)r)r)r*rrErr*)r)r*rrrr)r)r*rrrr*)r)r*rr=rr;)) __future__rrrrcollections.abcrtypingrrrrr&r r r rr(r5rRrg ResamplingBICUBICrxr{rrrPrBILINEARrrrrrrrrrrrrrrr s&#""""" $$$$$$444444444444++++++++++ $ $ $ $    (+,)-# ]]]]]H/3d+d+d+d+d+P>C=M=U/////@>C=M=U/////D"*04%/ +++++\ R R R R R 8=7G7O,,,,,2      h    &$-(B%&()D"*%/ X0X0X0X0X0v<<<<GGGG<<<<    " OOO O 6;