a :jga @sHddlmZddlZddlZddlZddlmZddlmZm Z ddl m Z m Z m Z ddd d d Zd d ddddZddddddZd[dddddddddZd\dd d d!d"d"d"dd#d$d%Ze jjfdd&d"dd'd(d)Ze jjfdd&d"dd'd*d+Ze jjdd,fdd&d"d!d-dd.d/d0Zd]dd"dd1d2d3Ze jjfdd4d"dd5d6d7ZGd8d9d9eZe jjfdd9d"dd:d;d<Zd^dddd=d>d?Zd_ddd dd@dAdBZe jjdCd,fdd&d"d4d-ddDdEdFZdddGdHdIZ dddGdJdKZ!dddGdLdMZ"dddGdNdOZ#dd"ddPdQdRZ$d`dd"ddTdUdVZ%ddWddddXdYdZZ&dS)a) annotationsN)Sequence)Protocolcast)ExifTagsImage ImagePalettezint | tuple[int, ...]ztuple[int, int, int, int])borderreturncCs^t|trBt|dkr(|\}}\}}qRt|dkrR|\}}}}n|}}}}||||fS)N) isinstancetuplelen)r lefttoprightbottomr6/usr/local/lib/python3.9/site-packages/PIL/ImageOps.py_border!s   rzstr | int | tuple[int, ...]str)colormoder cCs&t|tr"ddlm}|||}|S)Nr) ImageColor)rrrgetcolor)rrrrrr_color,s   r Image.Imagez list[int])imagelutr cCsf|jdkrd}t|nJ|jdvrN|jdkrDt|dkrD|||}||Sd|j}t|dS)NPzmode P support coming soon)LRGBr$znot supported for mode )rNotImplementedErrorrpointOSError)r r!msgrrr_lut4s      r*Fzfloat | tuple[float, float]zint | Sequence[int] | NonezImage.Image | Nonebool)r cutoffignoremask preserve_toner cCsP|r|d|}n ||}g}tdt|dD]}|||d}|durxt|trfd||<n|D] } d|| <qj|rt|ts||f}d} tdD]} | || } qt| |dd} tdD]H} | || kr| || } d|| <n|| | 8<d} | dkrqqt| |dd} tdddD]N} | || kr\| || } d|| <n|| | 8<d} | dkr4qq4tdD]} || rqqtdddD]} || rqȐq| | kr|ttdq4d | | }| |}tdD]>} t| ||} | dkr(d} n| dkr6d} | | qq4t ||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#rr%Ndrgo@) convert histogramrangerrintrextendlistappendr*)r r,r-r.r/r4r!layerhZixncutlohiscaleoffsetrrr autocontrastFsj                         rBr1zstr | tuple[int, ...]z"str | int | tuple[int, ...] | Noner6)r blackwhitemid blackpoint whitepointmidpointr cCs*|jdksJ|durzequalize..rr2r1r ) rr3r4r5rr7r8 functoolsreduceoperatoraddr9r*) r r.r;r!bZhistostepr<rMrrrequalizes     r)r r fillr c Cst|\}}}}||jd|}||jd|}t||j} |jrtj|d} t| trt | dksxt | dkr| | } nd} t |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)r^r N)rrOrrr^r r_rrrrrr]r`rc) r r rrrrrrTrUrr^rdrrrexpands  "  r)r rOrPbleedr\r cCs^|\}}d|krdks"nd}d|kr6dks.r%)r5r*)r rr!rrr posterizesr)r thresholdr cCs>g}tdD]&}||kr$||q |d|q t||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. r%r1)r5r9r*)r rr!rMrrrsolarizes   r)in_place)r rr c CsX||}|tjjd}tjjtjj tjj tjj tjj tjj tjjd|}|durF||}|r~|j|_|j|_|r|n|}|}tjj|vr:|tjj=d|jvr||jd<nd|jvr||jd<dD]T}||jvrdD]@} |j|} t| trt| d| nt| d | |j|<qq|sT|Sn|sT|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)r rr rNexifzRaw profile type exif)zXML:com.adobe.xmpZxmp)ztiff:Orientation="([0-9])"z,([0-9])rrz)loadZgetexifgetrZBaseZ OrientationrrrZ ROTATE_180rZ TRANSPOSEZ ROTATE_270Z TRANSVERSEZ ROTATE_90rZim_sizeinfotobyteshexrrresubencoderk) r rZ image_exifZ orientationrPZtransposed_imageZ exif_imagerkeypatternvaluerrrexif_transposesN          r)rNNF)Nrr1rC)r)N)rr)r)' __future__rr{r}rcollections.abcrtypingrrrrrr rrr*rBrNZ ResamplingZBICUBICrYrZrgrhr@rmZBILINEARrvrrrrrrrrrrrrrrs`   dh ".!![