B .^fF@sddlZddlZddlmZddZddZdd Zd/d d Zd0ddZej ddfddZ d1ddZ ej fddZ ej fddZd2ddZd3ddZej ddfddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd4d+d,Zd-d.ZdS)5N)ImagecCs^t|trBt|dkr(|\}}\}}qRt|dkrR|\}}}}n|}}}}||||fS)N) isinstancetuplelen)borderlefttoprightbottomr0/tmp/pip-install-mwhup7aw/Pillow/PIL/ImageOps.py_borders   rcCs&t|tr"ddlm}|||}|S)Nr) ImageColor)rstrrZgetcolor)colormoderrrr_color(s   rcCsV|jdkrtdn>|jdkrJ|jdkr@t|dkr@|||}||StddS)NPzmode P support coming soon)LRGBrz!not supported for this image mode)rNotImplementedErrorrZpointOSError)imagelutrrr_lut0s     rc Cs6|}g}xtdt|dD]}|||d}|dk rvy d||<Wn*tk rtx|D] }d||<q`WYnX|rdd}xtdD]}|||}qW||d} xPtdD]D} | || kr| || } d|| <n|| | 8<d} | dkrPqW||d} xZtdddD]J} | || kr>| || } d|| <n|| | 8<d} | dkrPqWxtdD]} || rnPqnWx"tdddD]} || rPqW| | kr|ttdq d| | } | | } xJtdD]>}t|| | }|dkr d}n|dkrd}||qWq Wt||S)a Maximize (normalize) image contrast. This function calculates a histogram of the input image, 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: How many percent to cut off from the histogram. :param ignore: The background pixel value (use None for no background). :return: An image. rrNdgo@) histogramranger TypeErrorextendlistintappendr)rcutoffignorer#rZlayerhixnZcutlohiscaleoffsetrrr autocontrast@sd                  r3r!cCs|jdkst|dkrOszequalize..rr"r!r) rr6r#r$rr&r' functoolsreduceoperatoraddr)r) rmaskr,rbZhistostepr.r7rrrequalize?s    rWc Csdt|\}}}}||jd|}||jd|}t|j||ft||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)rrArr=rrr@) rr fillr r r r r:r;rDrrrexpand^s rYgcCsxt|}d|dkr dks*nd|d<d|dkrBdksLnd|d<d|kr`dksfnd}||jd||jdf}|jd|dd|jd|ddf}|d|d}|d|d}||kr|d} |d} n4||kr||d} |d} n|d} |d|} |d|d| |d} |d|d| |d} | | | | | | f} |j||| dS)a Returns a sized 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 size and crop. :param size: The requested output size in pixels, given as a (width, height) tuple. :param method: What resampling method to use. Default is :py:attr:`PIL.Image.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. grg?g?rr)Zbox)r'rAr<)rrArBZbleedrCZ bleed_pixelsZ live_sizeZlive_size_ratioZ output_ratioZ crop_widthZ crop_heightZ crop_leftZcrop_toprHrrrfitos0"     rZcCs |tjS)zq Flip the image vertically (top to bottom). :param image: The image to flip. :return: An image. ) transposerFLIP_TOP_BOTTOM)rrrrflipsr]cCs |dS)zi Convert the image to grayscale. :param image: The image to convert. :return: An image. r)r6)rrrr grayscalesr^cCs.g}xtdD]}|d|qWt||S)zc Invert (negate) the image. :param image: The image to invert. :return: An image. rr!)r$r)r)rrr7rrrinvertsr_cCs |tjS)zq Flip image horizontally (left to right). :param image: The image to mirror. :return: An image. )r[rFLIP_LEFT_RIGHT)rrrrmirrorsracCs@g}dd|d}xtdD]}|||@q Wt||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. rrr)r$r)r)rbitsrrTr7rrr posterizes rdcCsBg}x2tdD]&}||kr&||q|d|qWt||S)z Invert all pixel values above a threshold. :param image: The image to solarize. :param threshold: All pixels above this greyscale level are inverted. :return: An image. rr!)r$r)r)r thresholdrr7rrrsolarizes  rgcCsl|}|d}tjtjtjtjtjtjtj d|}|dk rd| |}|d=| |j d<|S| S)z If an image has an EXIF Orientation tag, return a new image that is transposed accordingly. Otherwise, return a copy of the image. :param image: The image to transpose. :return: An image. i)rrrbNexif)Zgetexifgetrr`Z ROTATE_180r\Z TRANSPOSEZ ROTATE_270Z TRANSVERSEZ ROTATE_90r[tobytesinforI)rrlZ orientationrBZtransposed_imagerrrexif_transposes    rp)rN)Nrr!r4)r)N)rr)re)rPrRrrrrrr3r8ZBICUBICrGrHr1ZBILINEARrLrWrYrZr]r^r_rardrgrprrrrs*   L T-   U