
    ёiG                     b   S SK r S SKJrJr  S SKrS SKJrJrJ	r	  S SK
r
 \R                  R                  \R                  R                  \R                  R                  \R                  R                  \R                  R                   \R                  R"                  S.r/ rSS jrSS jrSS jrS rS	 rS
 rS rS rS rS rS rSS jr SS jr SS jr!SS jr"SS jr#g!   \R                  \R                  \R                  \R                  \R                   \R"                  S.r N= f)    N)IterableSequence)ImageImageEnhanceImageOps)nearestbilinearbicubicboxlanczoshammingc                    US;  a  [        SU 35      eU R                  S:X  a;  [        R                  " [        R
                  " U [        R                  5      5      nGOU R                  S:X  a:  [        R                  " [        R
                  " U [        R                  5      5      nOU R                  S:X  a:  [        R                  " [        R
                  " U [        R                  5      5      nOwU R                  S:X  a=  S[        R                  " [        R
                  " U [        R                  5      5      -  nO*[        R                  " [        R
                  " U 5      5      nU R                  S:X  a  S	nO(U R                  S:X  a  S
nO[        U R                  5      n[        R                  R                  R                  UR                  5      nUS:X  a(  [        R                  " U[        R                  5      S-  nUR                  U R                   S
   U R                   S   U/5      nUS:X  a  UR#                  / SQ5      nU$ )a%  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.

)CHWHWCz&data_format should be CHW or HWC. Got IzI;16F1   YCbCr      uint8g     o@r   r   )   r   r   )
ValueErrormodepaddle	to_tensornpasarrayint32float32r   lenbasedata_feederconvert_dtypedtypecastreshapesize	transpose)picdata_formatimgnchannelr&   s        g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/vision/transforms/functional_pil.pyr   r   /   s    .(A+OPP xx3rzz#rxx89	V	rzz#rxx89	Srzz#rzz:;	SF$$RZZRXX%>??rzz#/
xx7	V	sxx=KK##11#))<Ekk#rzz*U2
++sxx{CHHQK:
;CemmI&J    c                    [        U[        5      (       d2  [        U[        5      (       a  [        U5      S:X  d  [	        SU 35      e[        U[        5      (       a  U R
                  u  p4X4::  a  X1:X  d
  XC::  a  XA:X  a  U $ X4:  a,  Un[        X-  U-  5      nU R                  XV4[        U   5      $ Un[        X-  U-  5      nU R                  XV4[        U   5      $ U R                  USSS2   [        U   5      $ )a  
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owohs          r/   r6   r6   a   s    , 	4*T8"<"<Ta6tf=>>$xxFqyafJ5BTX\"B::rh(<](KLLBTX\"B::rh(<](KLLzz$tt*&:=&IJJr0   c                    [        U[        R                  [        [        45      (       d  [        S5      e[        U[        R                  [        [        [        45      (       d  [        S5      e[        U[        5      (       d  [        S5      e[        U[        5      (       a)  [        U5      S;  a  [        S[        U5       S3-   5      eUS;   d   S5       e[        U[        5      (       a  [	        U5      n[        U[        5      (       a  U=n=n=pg[        U[        5      (       a  [        U5      S	:X  a  US
   =pEUS   =pg[        U[        5      (       a#  [        U5      S:X  a  US
   nUS   nUS	   nUS   nUS:X  a]  U R                  S:X  a8  U R                  5       n[        R                  " XUS9n	U	R                  U5        U	$ [        R                  " XUS9$ U R                  S:X  am  U R                  5       n[         R"                  " U 5      n [         R$                  " U WW4WW44U5      n [&        R(                  " U 5      n U R                  U5        U $ [         R"                  " U 5      n [        U R*                  5      S:X  a  [         R$                  " U WW4WW4S4U5      n [        U R*                  5      S	:X  a  [         R$                  " U WW4WW44U5      n [&        R(                  " U 5      $ )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)constantedgereflect	symmetriczBPadding mode should be either constant, edge, reflect or symmetricr   r   r   r>   r   r?   P)borderfill)r   r   )r3   numbersNumberlisttupler5   strr   r"   r   r4   r   
getpaletter   expand
putpaletter   r   padr   	fromarrayshape)
r-   paddingrE   padding_modepad_left	pad_rightpad_top
pad_bottompaletteimages
             r/   rN   rN      s   F ge<==788dW^^S$>??455lC((<=='8$$WV)CGWn-.
 	

    L
 LL  '4  .'36===9=w'8$$W):&qz)&qz)'8$$W):1:!*AJ	QZ
z!88s?nn&GOOCdCEW%Ls>>88s?nn&G**S/C&&:&9(=>C
 //#&CNN7#Jjjosyy>Q&&:&9(=vFC syy>Q&&:&9(=>C s##r0   c                 2    U R                  X!X$-   X-   45      $ )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widths        r/   rZ   rZ      s     88Tcl;<<r0   c                    [        U[        R                  5      (       a  [        U5      [        U5      4nU R                  u  p#Uu  pE[        [        X4-
  S-  5      5      n[        [        X%-
  S-  5      5      n[        XXtU5      $ )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       @)r3   rF   rG   r4   r)   roundrZ   )r-   output_sizeimage_widthimage_heightcrop_height
crop_widthcrop_top	crop_lefts           r/   center_croprh   	  sz     +w~~..;'[)9: #K)K5,4;<=HE;3s:;<IyzBBr0   c                 @    U R                  [        R                  5      $ )zHorizontally flips the given PIL Image.

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

Returns:
    PIL.Image:  Horizontally flipped image.

)r*   r   FLIP_LEFT_RIGHTr-   s    r/   hfliprl   !       ==..//r0   c                 @    U R                  [        R                  5      $ )zVertically flips the given PIL Image.

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

Returns:
    PIL.Image:  Vertically flipped image.

)r*   r   FLIP_TOP_BOTTOMrk   s    r/   vfliprp   /  rm   r0   c                 T    [         R                  " U 5      nUR                  U5      n U $ )ac  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   
Brightnessenhance)r-   brightness_factorenhancers      r/   adjust_brightnessrv   =  s*     &&s+H


,
-CJr0   c                 T    [         R                  " U 5      nUR                  U5      n U $ )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   Contrastrs   )r-   contrast_factorru   s      r/   adjust_contrastrz   P  s)     $$S)H


?
+CJr0   c                 T    [         R                  " U 5      nUR                  U5      n U $ )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   Colorrs   )r-   saturation_factorru   s      r/   adjust_saturationr~   c  s*     !!#&H


,
-CJr0   c                    SUs=::  a  S::  d  O  [        SU S35      eU R                  nUS;   a  U $ U R                  S5      R                  5       u  p4n[        R
                  " U[        R                  S9nUR                  [        R                  5      nU[        US-  5      -   S	-  nUR                  [        R                  5      n[        R                  " US
5      n[        R                  " SX4U45      R                  U5      n U $ )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   r   r   LHSVr&   r      r   )r   r   convertsplitr   arrayr   astypeint16r4   r   rO   merge)r-   
hue_factor
input_moder:   svnp_hs          r/   
adjust_huer   v  s    , J%#%;zl2IJKKJ))
kk% &&(GA!88ARXX&D;;rxx D3zC'((C/D;;rxx Dc"A
++eaAY
'
/
/

;CJr0   c                     [        U[        5      (       a  [        U/S-  5      nU R                  U R                  [
        R                  U[        U   U5      $ )an  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   )r3   r4   rI   	transformr)   r   AFFINEr7   )r-   matrixr8   rE   s       r/   affiner     sN    & $dVaZ ==]+ r0   c                 ~    [        U[        5      (       a  [        U/S-  5      nU R                  U[        U   UUUS9$ )ad  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   )	fillcolor)r3   r4   rI   rotater7   )r-   angler8   rL   centerrE   s         r/   r   r     sK    : $dVaZ ::]+   r0   c                     [        U[        5      (       a  [        U/S-  5      nU R                  U R                  [
        R                  U[        U   U5      $ )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   )r3   r4   rI   r   r)   r   PERSPECTIVEr7   )r-   coeffsr8   rE   s       r/   perspectiver     sP    ( $dVaZ ==]+ r0   c                     US:X  a  U R                  S5      n U $ US:X  ae  U R                  S5      n [        R                  " U [        R                  S9n[        R                  " X"U/5      n[
        R                  " US5      n U $ [        S5      e)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   r   r   r   RGBz+num_output_channels should be either 1 or 3)r   r   r   r   dstackr   rO   r   )r-   num_output_channelsnp_imgs      r/   to_grayscaler     s      akk# J 
	!kk##RXX.FF34oofe, J FGGr0   c                     [         R                  " U [         R                  S9nXWXU-   2X"U-   2S4'   [        R                  " US5      n U $ )aH  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.

r   .r   )r   r   r   r   rO   )r-   ijr:   r9   r   inplacer   s           r/   eraser   #  sH    " XXc*F()11u9aa%i$%
//&%
(CJr0   )r   )r	   )r   r?   )r   r   )r   FNr   )r   )F)$rF   collections.abcr   r   numpyr   PILr   r   r   r   
ResamplingNEARESTBILINEARBICUBICBOXLANCZOSHAMMINGr7   __all__r   r6   rN   rZ   rh   rl   rp   rv   rz   r~   r   r   r   r   r   r    r0   r/   <module>r      s$    .  - -  ##++$$--##++####++##++& /d'KTi$X=$C000&&&%P@ JK&R@:E==NN==yy====s   BC% %AD.