K ic.ddlmZddlZddlZddlZddlmZddlmZm Z m Z m Z ddl m Z mZmZd$dZd%dZd&d Z d' d(d Z d) d*d Zej*j,f d+d Zej*j,f d+dZej*j,ddf d,dZd-d.dZej*j,f d/dZGdde Zej*j:f d0dZd1d2dZ d3 d4dZ ej*j,ddf d5dZ!d6dZ"d6dZ#d6dZ$d6dZ%d7dZ&d8d9dZ'e d:d Z(e d d! d;d"Z(d d!d} | j |d| j |d| j |d@| t ||z }|D]} | j |d| |d|dz zt|zz| j |d| |d|dz zt|zz| j |d| |d|dz zt|zznDt ||z }t ||z }|D]} | j |d| | d|dz zt|zz| j |d| | d|dz zt|zz| j |d| | d|dz zt|zz|D]} | j | d| |d| dz zt|zz| j | d| |d| dz zt|zz| j | d| |d| dz zt|zzt d|z D]>} | j |d| j |d| j |d@|jd}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%Nrr1r&r rr') r!rrr6r"r5r9rr3r.)r+blackwhitemid blackpoint whitepointmidpoint rgb_black rgb_whitergb_midredgreenbluei range_map range_map1 range_map2s rcolorizerYsB ::   {J3*333333J?(?j?C?????Xc]F5%$89IXc]F5%$89I9<d8C=&e"45dG C E D: " 9Q<  Yq\" IaL!" *z12  A JJ! qIaL9Q<$?@C NRR  LL! qIaL9Q<$?@C NRR  KK! qIaL9Q<$?@C NRR  8j01 :01  A JJ! qGAJ1$=>#j/QQ  LL! qGAJ1$=>#j/QQ  KK! qGAJ1$=>#j/QQ   YA JJwqzA1 )B$Cs:$VV W LL Q)A,";<JOO  KK Q)A,*C%DJ%WW X  Y3# $" 9Q<  Yq\" IaL!" MM% E sU{T) **rcb|j|jz }|d|dz }||k7rt||kDr8t|j|jz |dz}||dk7r?|d|f}n7t|j|jz |dz}||dk7r||df}|j||S)a Returns a resized version of the image, set to the maximum width and height within the requested size, while maintaining the original aspect ratio. :param image: The image to resize. :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`. :return: An image. rr resamplewidthheightroundresizer+sizemethodim_ratio dest_ratio new_height new_widths rcontainri  {{U\\)Ha47"J: j u||ekk9DGCDJT!W$Q,ekkELL847BCIDG#!47+ <<v< ..rcb|j|jz }|d|dz }||k7rt||kr8t|j|jz |dz}||dk7r?|d|f}n7t|j|jz |dz}||dk7r||df}|j||S)a Returns a resized version of the image, so that the requested size is covered, while maintaining the original aspect ratio. :param image: The image to resize. :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`. :return: An image. rr r[r]rbs rcoverrl,rjr)?rmc 2t|||}|j|k(r|}|Stj|j||}|j r#|j }||j||j|dk7rKt|d|jz tdt|ddz}|j||df|St|d|jz tdt|ddz} |j|d| f|S)at Returns a resized and padded version of the image, expanded to fill the requested aspect ratio and size. :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 color: The background color of the padded image. :param centering: Control the position of the original image within the padded version. (0.5, 0.5) will keep the image centered (0, 0) will keep the image aligned to the top left (1, 1) will keep the image aligned to the bottom right :return: An image. rr )rircr newr!palette getpalette putpaletter^r`maxminpaster_) r+rcrdr centeringresizedoutrpxys rpadr{Ks8eT6*G||t Jii D%0 ??((*G"w' ==DG #tAw.#aYq\19M2NNOA IIg1v & JtAw/3q#ilA:N3OOPA IIg1v & Jrct|\}}}}|j|||jd|z |jd|z fS)a/ Remove border from image. The same amount of pixels are removed from all four sides. This function works on all image modes. .. seealso:: :py:meth:`~PIL.Image.Image.crop` :param image: The image to crop. :param border: The number of pixels to remove. :return: An image. rr )rcroprc)r+rrrrrs rr}r}ysH 'vD#uf ::tS%**Q-%"7A9OP QQrc|dk(r|jS|dkr d}t|t||jzt||jzf}|j ||S)a Returns a rescaled image by a specific factor given in parameter. A factor greater than 1 expands the image, between 0 and 1 contracts the image. :param image: The image to rescale. :param factor: The expansion factor, as a float. :param resample: Resampling method to use. Default is :py:attr:`~PIL.Image.Resampling.BICUBIC`. See :ref:`concept-filters`. :returns: An :py:class:`~PIL.Image.Image` object. r rz!the factor must be greater than 0)copy ValueErrorr`r^r_ra)r+factorr\r-rcs rrErEsd{zz| 11ofu{{*+U6ELL3H-IJ||D(++rc eZdZdZ ddZy)SupportsGetMeshaE An object that supports the ``getmesh`` method, taking an image as an argument, and returning a list of tuples. Each tuple contains two tuples, the source box as a tuple of 4 integers, and a tuple of 8 integers for the final quadrilateral, in order of top left, bottom left, bottom right, top right. cyN)selfr+s rgetmeshzSupportsGetMesh.getmeshs rN)r+ Image.ImagereturnzUlist[tuple[tuple[int, int, int, int], tuple[int, int, int, int, int, int, int, int]]])__name__ __module__ __qualname____doc__rrrrrrs     rrc|j|jtjj|j ||S)aL Deform the image. :param image: The image to deform. :param deformer: A deformer object. Any object that implements a ``getmesh`` method can be used. :param resample: An optional resampling filter. Same values possible as in the PIL.Image.transform function. :return: An image. ) transformrcr TransformMESHr)r+deformerr\s rdeformrs7 ?? EOO(((*:*:5*A8 rcb|jdk(r|jd}|j|}g}tdt |dD]}|||dzDcgc]}|s| }}t |dkr$|j t tdMtjtj||dz dz}|s$|j t td|dz}tdD]!} |j||z||| |zz}#t||Scc}w) a Equalize the image histogram. This function applies a non-linear mapping to the input image, in order to create a uniform distribution of grayscale values in the output image. :param image: The image to equalize. :param mask: An optional mask. If given, only the pixels selected by the mask are included in the analysis. :return: An image. r$r&rr'r r2r1r) r!r3r4r5rr7r8 functoolsreduceoperatoraddr9r.) r+r<r?r,b_fhistosteprArUs requalizers zzS e$ A C 1c!fc " %AGn333 u:? JJtE#J' ($$X\\59E"IE#MD 4c +,AIs%AJJqDy)Aa!eH A% % s 4s D,D,c^t|\}}}}||jdz|z}||jdz|z}t||j} |jry|jj} t j | |j | } t| tr0t| dk(st| dk(r| j| } nd} tj|j||f| } | r| j| j | 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)rrcr"r!rpr rqrrrrr rorrru) r+rfillrrrrr^r_r r!rprxs rexpandrs 'vD#uf 5::a= 5 (E 5::a= 6 )F 4 $E }}}}!!++D%2B2B42HI eU #UqCJ!O$$U+E ))EJJ 7C w-IIedC[! Jrc|\}}d|cxkrdksnd}d|cxkrdksnd}d|cxkrdksnd}||jdz||jdzf}|jd|ddzz |jd|ddzz f}|d|dz } |d|dz } | | k(r |d} |d} n | | k\r| |dz} |d} n |d} |d| z } |d|d| z |zz} |d|d| z |zz}| || | z|| zf}|j|||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?rmrr r)box)rcra)r+rcrdbleedrv centering_x centering_y bleed_pixels live_sizelive_size_ratio output_ratio crop_width crop_height crop_leftcrop_topr}s rfitrsP )K + $ $ + $ $ % #  EJJqM)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$< //rcT|jtjjS)zq Flip the image vertically (top to bottom). :param image: The image to flip. :return: An image. ) transposer TransposeFLIP_TOP_BOTTOMr+s rflipr^ ??5??:: ;;rc$|jdS)zi Convert the image to grayscale. :param image: The image to convert. :return: An image. r%)r3rs r grayscalerhs == rcttddd}|jdk(r|j|St ||S)zc Invert (negate) the image. :param image: The image to invert. :return: An image. r1r21)r8r5r!r)r.)r+r,s rinvertrrs= uS"b! "C$zzS05;;s Fd5#6FFrcT|jtjjS)zq Flip image horizontally (left to right). :param image: The image to mirror. :return: An image. )rr rFLIP_LEFT_RIGHTrs rmirrorr}rrcrdd|z zdz }tdDcgc]}||z }}t||Scc}w)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 r')r5r.)r+bitsr<rUr,s r posterizersF1t8_q !D"3Z (1t8 (C ( s  )s 4cg}tdD]-}||kr|j||jd|z /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+ thresholdr,rUs rsolarizersM C 3Z  y= JJqM JJsQw   s rcyrrr+in_places rexif_transposersLOr)rcyrrrs rrrsrc |j|j}|jtjj d}t jjt jjt jjt jjt jjt jjt jjdj|}||r<|jj!||_|jj"|_n|j!|}|r|n}|j}tjj |vr-|tjj =d|j&vr|j)|j&d<n9d|j&vr+|j)j+|j&d<dD]}||j&vsdD] |j&|} t-| t.rt1j2 d| } nJt-| t4rt5 fd | D} n%t1j2 j7d | } | |j&|<|sSy|s|j9Sy) 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])rc3hK|])}tjjd|+yw)rN)resubencode).0vpatterns r z!exif_transpose..s**EFw~~'7a @*s/2r)loadgetexifgetr Base Orientationr rr ROTATE_180r TRANSPOSE ROTATE_270 TRANSVERSE ROTATE_90imrrc_sizeinfotobyteshexrrrrrrr) r+r image_exif orientationrdtransposed_image exif_imagerkeyvaluers @rrrs/ JJLJ..!:!:A>K ?? * * ?? % % ?? * * ?? $ $ ?? % % ?? % % ?? $ $ c+  xx))&1EH((--EK$v6 &U,< !!# == $ $ ,X]]../(*.,,. '(JOO;;?<<>;M;M;O  783 5*//)$ 5!+ 4%eS1$&FF7B$>E'u5$)*JO*%E%'FF7>>+;S%$HE/4 , 5 5 # # zz| r)rint | tuple[int, ...]rztuple[int, int, int, int])r str | int | tuple[int, ...]r!rrr)r+rr,z list[int]rr)rNNF) r+rr:zfloat | tuple[float, float]r;zint | Sequence[int] | Noner<Image.Image | Noner=boolrr)Nrr1)r+rrIstr | tuple[int, ...]rJrrK"str | int | tuple[int, ...] | NonerLr6rMr6rNr6rr)r+rrctuple[int, int]rdr6rr) r+rrcrrdr6r rrvtuple[float, float]rr)r)r+rrr6rr)r+rrfloatr\r6rr)r+rrrr\r6rrr)r+rr<rrr)rr)r+rrrrrrr) r+rrcrrdr6rrrvrrr)r+rrr)r+rrr6rr))r+rrr6rr)r+rrz Literal[True]rNone)r+rrzLiteral[False]rr)r+rrrrr)) __future__rrrrcollections.abcrtypingrrrrrr r r rr"r.rGrY ResamplingBICUBICrirlr{r}rErBILINEARrrrrrrrrrrrrrrrs1&# $44++ $ (+,)-# ] ] '] ']  ]  ]  ]H/3d+ d+ d+ !d+ , d+  d+  d+d+d+P>C=M=M=U=U/ /-/7://@>C=M=M=U=U/ /-/7://D""**04%/ + + + + . + # +  +\ R 8=7G7G7O7O, , %,14,,2  h  &$$--  (B%&()  ! & D""**%/ X0 X0 X0 X0  X0 # X0  X0v<G< " O O 6; %3