o
    #jG                  
   @   s,  d dl Z d dlmZmZ d dlZd dlmZmZm	Z	 d dl
Z
zejjejjejjejjejjejjdZW n   ejejejejejejdZY g Zd+ddZd,d	d
Zd-ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd.dd Z	 d/d"d#Z d.d$d%Z!d0d'd(Z"d1d)d*Z#dS )2    N)IterableSequence)ImageImageEnhanceImageOps)nearestbilinearZbicubicboxZlanczosZhammingCHWc                 C   s4  |dvrt d| | jdkrtt| tj}n:| jdkr+tt| tj}n*| jdkr;tt| tj}n| jdkrMdtt| tj }ntt| }| jdkr]d	}n| jdkred
}nt	| j}tj
j|j}|dkrt|tjd }|| jd
 | jd |g}|dkr|g d}|S )aE  Converts a ``PIL.Image`` to paddle.Tensor.

    See ``ToTensor`` for more details.

    Args:
        pic (PIL.Image): Image to be converted to tensor.
        data_format (str, optional): Data format of output tensor, should be 'HWC' or
            'CHW'. Default: 'CHW'.

    Returns:
        Tensor: Converted image.

    )r
   ZHWCz&data_format should be CHW or HWC. Got IzI;16F1   ZYCbCr      uint8g     o@r   r
   )   r   r   )
ValueErrormodepaddle	to_tensornpasarrayZint32Zfloat32r   lenbaseZdata_feederZconvert_dtypedtypecastZreshapesize	transpose)ZpicZdata_formatimgZnchannelr    r    h/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/transforms/functional_pil.pyr   /   s.   






r   r   c                 C   s   t |tst |trt|dkstd| t |tr_| j\}}||kr)||ks1||kr3||kr3| S ||k rK|}t|| | }| ||ft| S |}t|| | }| ||ft| S | |ddd t| S )aX  
    Resizes the image to given size

    Args:
        input (PIL.Image): Image to be resized.
        size (int|list|tuple): Target size of input data, with (height, width) shape.
        interpolation (int|str, optional): Interpolation method. when use pil backend,
            support method are as following:
            - "nearest": Image.NEAREST,
            - "bilinear": Image.BILINEAR,
            - "bicubic": Image.BICUBIC,
            - "box": Image.BOX,
            - "lanczos": Image.LANCZOS,
            - "hamming": Image.HAMMING

    Returns:
        PIL.Image: Resized image.

    r   zGot inappropriate size arg: N)
isinstanceintr   r   	TypeErrorr   resize_pil_interp_from_str)r   r   interpolationwhowZohr    r    r!   r&   a   s$   

 r&   constantc           
      C   s  t |tjttfstdt |tjtttfstdt |ts$tdt |tr:t|dvr:t	dt| d |dv sBJ dt |trKt|}t |t
rX| } } }}t |trot|d	kro|d
  }}|d  }}t |trt|dkr|d
 }|d }|d	 }|d }|dkr| jdkr|  }tj| ||d}	|	| |	S tj| ||dS | jdkr|  }t| } t| ||f||ff|} t| } | | | S t| } t| jdkrt| ||f||fdf|} t| jd	krt| ||f||ff|} t| S )a;  
    Pads the given PIL.Image on all sides with specified padding mode and fill value.

    Args:
        img (PIL.Image): Image to be padded.
        padding (int|list|tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If list/tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a list/tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill (float, optional): Pixel fill value for constant fill. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant. Default: 0.
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default: 'constant'.

            - constant: pads with a constant value, this value is specified with fill

            - edge: pads with the last value on the edge of the image

            - reflect: pads with reflection of image (without repeating the last value on the edge)

                       padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                       will result in [3, 2, 1, 2, 3, 4, 3, 2]

            - symmetric: pads with reflection of image (repeating the last value on the edge)

                         padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                         will result in [2, 1, 1, 2, 3, 4, 4, 3]

    Returns:
        PIL.Image: Padded image.

    zGot inappropriate padding argzGot inappropriate fill argz"Got inappropriate padding_mode arg)r      z9Padding must be an int or a 2, or 4 element tuple, not a z element tuple)r,   edgeZreflectZ	symmetriczBPadding mode should be either constant, edge, reflect or symmetricr   r   r   r-   r   r,   P)borderfill)r   r   )r#   numbersNumberlisttupler%   strr   r   r   r$   r   Z
getpaletter   expandZ
putpaletter   r   padr   	fromarrayshape)
r   paddingr1   Zpadding_modepad_left	pad_rightpad_topZ
pad_bottompaletteimager    r    r!   r8      sv   #











r8   c                 C   s   |  |||| || fS )a  Crops the given PIL Image.

    Args:
        img (PIL.Image): Image to be cropped. (0,0) denotes the top left
            corner of the image.
        top (int): Vertical component of the top left corner of the crop box.
        left (int): Horizontal component of the top left corner of the crop box.
        height (int): Height of the crop box.
        width (int): Width of the crop box.

    Returns:
        PIL.Image: Cropped image.

    )crop)r   topleftheightwidthr    r    r!   rA      s   rA   c                 C   sf   t |tjrt|t|f}| j\}}|\}}tt|| d }tt|| d }t| ||||S )a  Crops the given PIL Image and resize it to desired size.

    Args:
        img (PIL.Image): Image to be cropped. (0,0) denotes the top left corner of the image.
        output_size (sequence or int): (height, width) of the crop box. If int,
            it is used for both directions
        backend (str, optional): The image process backend type. Options are `pil`, `cv2`. Default: 'pil'.

    Returns:
        PIL.Image: Cropped image.

    g       @)r#   r2   r3   r$   r   roundrA   )r   Zoutput_sizeZimage_widthZimage_heightZcrop_heightZ
crop_widthZcrop_topZ	crop_leftr    r    r!   center_crop	  s   
rG   c                 C      |  tjS )zHorizontally flips the given PIL Image.

    Args:
        img (PIL.Image): Image to be flipped.

    Returns:
        PIL.Image:  Horizontally flipped image.

    )r   r   ZFLIP_LEFT_RIGHTr   r    r    r!   hflip!     rJ   c                 C   rH   )zVertically flips the given PIL Image.

    Args:
        img (PIL.Image): Image to be flipped.

    Returns:
        PIL.Image:  Vertically flipped image.

    )r   r   ZFLIP_TOP_BOTTOMrI   r    r    r!   vflip/  rK   rL   c                 C      t | }||} | S )a  Adjusts brightness of an Image.

    Args:
        img (PIL.Image): PIL Image to be adjusted.
        brightness_factor (float):  How much to adjust the brightness. Can be
            any non negative number. 0 gives a black image, 1 gives the
            original image while 2 increases the brightness by a factor of 2.

    Returns:
        PIL.Image: Brightness adjusted image.

    )r   Z
Brightnessenhance)r   Zbrightness_factorenhancerr    r    r!   adjust_brightness=     

rP   c                 C   rM   )a}  Adjusts contrast of an Image.

    Args:
        img (PIL.Image): PIL Image to be adjusted.
        contrast_factor (float): How much to adjust the contrast. Can be any
            non negative number. 0 gives a solid gray image, 1 gives the
            original image while 2 increases the contrast by a factor of 2.

    Returns:
        PIL.Image: Contrast adjusted image.

    )r   ZContrastrN   )r   Zcontrast_factorrO   r    r    r!   adjust_contrastP  rQ   rR   c                 C   rM   )a~  Adjusts color saturation of an image.

    Args:
        img (PIL.Image): PIL Image to be adjusted.
        saturation_factor (float):  How much to adjust the saturation. 0 will
            give a black and white image, 1 will give the original image while
            2 will enhance the saturation by a factor of 2.

    Returns:
        PIL.Image: Saturation adjusted image.

    )r   ColorrN   )r   Zsaturation_factorrO   r    r    r!   adjust_saturationc  rQ   rT   c                 C   s   d|  kr
dksn t d| d| j}|dv r| S | d \}}}tj|tjd}|tj}|t	|d  d	 }|tj}t
|d
}t
d|||f|} | S )a.  Adjusts hue of an image.

    The image hue is adjusted by converting the image to HSV and
    cyclically shifting the intensities in the hue channel (H).
    The image is then converted back to original image mode.

    `hue_factor` is the amount of shift in H channel and must be in the
    interval `[-0.5, 0.5]`.

    Args:
        img (PIL.Image): PIL Image to be adjusted.
        hue_factor (float):  How much to shift the hue channel. Should be in
            [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
            HSV space in positive and negative direction respectively.
            0 means no shift. Therefore, both -0.5 and 0.5 will give an image
            with complementary colors while 0 gives the original image.

    Returns:
        PIL.Image: Hue adjusted image.

    g      g      ?zhue_factor:z is not in [-0.5, 0.5].>   r   Lr   r   ZHSVr   r      rU   )r   r   convertsplitr   arrayr   ZastypeZint16r$   r   r9   merge)r   Z
hue_factorZ
input_moder*   svZnp_hr    r    r!   
adjust_huev  s   r^   r   c                 C   2   t |trt|gd }| | jtj|t| |S )a  Affine the image by matrix.

    Args:
        img (PIL.Image): Image to be affined.
        matrix (float or int): Affine matrix.
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set to PIL.Image.NEAREST . when use pil backend,
            support method are as following:
            - "nearest": Image.NEAREST,
            - "bilinear": Image.BILINEAR,
            - "bicubic": Image.BICUBIC
        fill (3-tuple or int): RGB pixel fill value for area outside the affined image.
            If int, it is used for all channels respectively.

    Returns:
        PIL.Image: Affined image.

    r   )r#   r$   r5   	transformr   r   ZAFFINEr'   )r   matrixr(   r1   r    r    r!   affine  s   
rb   Fc                 C   s0   t |trt|gd }| j|t| |||dS )a  Rotates the image by angle.

    Args:
        img (PIL.Image): Image to be rotated.
        angle (float or int): In degrees degrees counter clockwise order.
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set to PIL.Image.NEAREST . when use pil backend,
            support method are as following:
            - "nearest": Image.NEAREST,
            - "bilinear": Image.BILINEAR,
            - "bicubic": Image.BICUBIC
        expand (bool, optional): Optional expansion flag.
            If true, expands the output image to make it large enough to hold the entire rotated image.
            If false or omitted, make the output image the same size as the input image.
            Note that the expand flag assumes rotation around the center and no translation.
        center (2-tuple, optional): Optional center of rotation.
            Origin is the upper left corner.
            Default is the center of the image.
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        PIL.Image: Rotated image.

    r   )Z	fillcolor)r#   r$   r5   rotater'   )r   Zangler(   r7   centerr1   r    r    r!   rc     s   
rc   c                 C   r_   )a  Perspective the image.

    Args:
        img (PIL.Image): Image to be perspectived.
        coeffs (list[float]): coefficients (a, b, c, d, e, f, g, h) of the perspective transforms.
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set to PIL.Image.NEAREST . when use pil backend,
            support method are as following:
            - "nearest": Image.NEAREST,
            - "bilinear": Image.BILINEAR,
            - "bicubic": Image.BICUBIC
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        PIL.Image: Perspectived image.

    r   )r#   r$   r5   r`   r   r   ZPERSPECTIVEr'   )r   Zcoeffsr(   r1   r    r    r!   perspective  s   
re   r   c                 C   s`   |dkr|  d} | S |dkr,|  d} tj| tjd}t|||g}t|d} | S td)a  Converts image to grayscale version of image.

    Args:
        img (PIL.Image): Image to be converted to grayscale.
        backend (str, optional): The image process backend type. Options are `pil`,
                    `cv2`. Default: 'pil'.

    Returns:
        PIL.Image: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel

            if num_output_channels = 3 : returned image is 3 channel with r = g = b

    r   rU   r   rV   RGBz+num_output_channels should be either 1 or 3)rX   r   rZ   r   Zdstackr   r9   r   )r   Znum_output_channelsnp_imgr    r    r!   to_grayscale  s   
	
rh   c                 C   s>   t j| t jd}||||| ||| df< t|d} | S )ax  Erase the pixels of selected area in input image with given value. PIL format is
     not support inplace.

    Args:
         img (PIL.Image): input image, which shape is (C, H, W).
         i (int): y coordinate of the top-left point of erased region.
         j (int): x coordinate of the top-left point of erased region.
         h (int): Height of the erased region.
         w (int): Width of the erased region.
         v (np.array): value used to replace the pixels in erased region.
         inplace (bool, optional): Whether this transform is inplace. Default: False.

     Returns:
         PIL.Image: Erased image.

    rV   .rf   )r   rZ   r   r   r9   )r   ijr*   r)   r]   Zinplacerg   r    r    r!   erase#  s   rk   )r
   )r   )r   r,   )r   r   )r   FNr   )r   )F)$r2   collections.abcr   r   numpyr   ZPILr   r   r   r   Z
ResamplingZNEARESTZBILINEARZBICUBICZBOXZLANCZOSZHAMMINGr'   __all__r   r&   r8   rA   rG   rJ   rL   rP   rR   rT   r^   rb   rc   re   rh   rk   r    r    r    r!   <module>   sN   



2
*l
( 

)
 