
    &Vf                        d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ  G d d	e	          Z ed
          	 d/d            Z G d de	          Z ed          	 	 	 	 	 	 	 d0d            Z G d de	          Z ed          	 	 	 	 d1d            Z G d de	          Z ed          	 	 	 	 d2d            Z	 	 	 	 d2d Z G d! d"e	          Z ed#          	 d3d$            Z G d% d&e	          Z ed'          	 	 	 	 	 	 d4d(            Zd) Z G d* d+e	          Z ed,          	 	 	 	 	 	 d4d-            Zd. ZdS )5    )backend)ops)keras_export)KerasTensor)any_symbolic_tensors)	Operation)compute_conv_output_shapec                   .     e Zd Z	 d fd	Zd Zd Z xZS )RGBToGrayscalechannels_lastc                 V    t                                                       || _        d S N)super__init__data_format)selfr   	__class__s     P/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/ops/image.pyr   zRGBToGrayscale.__init__   s)     	&    c                 N    t           j                            || j                  S )Nr   )r   imagergb_to_grayscaler   r   r   s     r   callzRGBToGrayscale.call   s*    }--( . 
 
 	
r   c                 T   t          |j                  dvrt          d|j                   t          |j                  dk    rW| j        dk    r&t	          |j        d d         dz   |j                  S t	          d|j        dd          z   |j                  S t          |j                  d	k    rs| j        dk    r5t	          |j        d
         f|j        dd         z   dz   |j                  S t	          |j        d
         df|j        dd          z   |j                  S d S )N      wInvalid image rank: expected rank 3 (single image) or rank 4 (batch of images). Received input with shape: image.shape=r   r      dtyper#   r   r      )lenshape
ValueErrorr   r   r%   r   s     r   compute_output_specz"RGBToGrayscale.compute_output_spec   sX   u{6))-${- -   u{q  ?22"5;ss#3d#:%+NNNN"4%+abb/#9MMMM""?22"[^%AbD(99D@+   
 #A k!""o	&
  +    #"r   r   __name__
__module____qualname__r   r   r*   __classcell__r   s   @r   r   r   
   s`         $' ' ' ' ' '
 
 
      r   r   z keras.ops.image.rgb_to_grayscaler   c                     t          | f          r#t          |                              |           S t          j                            | |          S )aB  Convert RGB images to grayscale.

    This function converts RGB images to grayscale images. It supports both
    3D and 4D tensors, where the last dimension represents channels.

    Args:
        image: Input RGB image or batch of RGB images. Must be a 3D tensor
            with shape `(height, width, channels)` or a 4D tensor with shape
            `(batch, height, width, channels)`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            Defaults to `"channels_last"`.

    Returns:
        Grayscale image or batch of grayscale images.

    Examples:

    >>> import numpy as np
    >>> from keras.src import ops
    >>> x = np.random.random((2, 4, 4, 3))
    >>> y = ops.image.rgb_to_grayscale(x)
    >>> y.shape
    (2, 4, 4, 1)

    >>> x = np.random.random((4, 4, 3)) # Single RGB image
    >>> y = ops.image.rgb_to_grayscale(x)
    >>> y.shape
    (4, 4, 1)

    >>> x = np.random.random((2, 3, 4, 4))
    >>> y = ops.image.rgb_to_grayscale(x, data_format="channels_first")
    >>> y.shape
    (2, 1, 4, 4)
    r   )r   r   symbolic_callr   r   r   )r   r   s     r   r   r   6   se    V UH%% #
 
 

-

	 =)) *   r   c                   :     e Zd Z	 	 	 	 	 	 	 d	 fd	Zd Zd Z xZS )
ResizebilinearFconstant        r   c	                     t                                                       t          |          | _        || _        || _        || _        || _        || _        || _	        || _
        d S r   )r   r   tuplesizeinterpolation	antialiasr   crop_to_aspect_ratiopad_to_aspect_ratio	fill_mode
fill_value)
r   r;   r<   r=   r>   r?   r@   rA   r   r   s
            r   r   zResize.__init__l   sa     	$KK	*"&$8!#6 "$r   c                     t           j                            || j        | j        | j        | j        | j        | j        | j	        | j
        	  	        S )Nr<   r=   r   r>   r?   r@   rA   )r   r   resizer;   r<   r=   r   r>   r?   r@   rA   r   s     r   r   zResize.call   sP    }##I,n(!%!: $ 8n $ 

 

 
	
r   c                    t          |j                  dk    r*t          | j        |j        d         fz   |j                  S t          |j                  dk    rz| j        dk    r9t          |j        d         f| j        z   |j        d         fz   |j                  S t          |j        d         |j        d         f| j        z   |j                  S t          d|j                   )	Nr   r!   r$   r   r   r   r#   zwInvalid input rank: expected rank 3 (single image) or rank 4 (batch of images). Received input with shape: image.shape=)r'   r(   r   r;   r%   r   r)   r   s     r   r*   zResize.compute_output_spec   s   u{q  	U[_..ek    ""?22"[^%	1U[_4FF+   
 #[^U[^4ty@+    ) ;) )
 
 	
r   r6   FFFr7   r8   r   r,   r1   s   @r   r5   r5   k   sr         !"!#% % % % % %*
 
 

 
 
 
 
 
 
r   r5   zkeras.ops.image.resizer6   Fr7   r8   c	                    t          |          dk    rt          d|           t          | j                  dk     st          | j                  dk    rt          d| j         d          |r|rt          d          t          | f          r*t	          ||||||||                              |           S t          j                            | ||||||||		  	        S )
a.
  Resize images to size using the specified interpolation method.

    Args:
        image: Input image or batch of images. Must be 3D or 4D.
        size: Size of output image in `(height, width)` format.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            `"bilinear"`, and `"bicubic"`. Defaults to `"bilinear"`.
        antialias: Whether to use an antialiasing filter when downsampling an
            image. Defaults to `False`.
        crop_to_aspect_ratio: If `True`, resize the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            cropped so as to return the
            largest possible window in the image (of size `(height, width)`)
            that matches the target aspect ratio. By default
            (`crop_to_aspect_ratio=False`), aspect ratio may not be preserved.
        pad_to_aspect_ratio: If `True`, pad the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            evenly padded on the short side.
        fill_mode: When using `pad_to_aspect_ratio=True`, padded areas
            are filled according to the given mode. Only `"constant"` is
            supported at this time
            (fill with constant value, equal to `fill_value`).
        fill_value: Float. Padding value to use when `pad_to_aspect_ratio=True`.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, weight)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.

    Returns:
        Resized image or batch of images.

    Examples:

    >>> x = np.random.random((2, 4, 4, 3)) # batch of 2 RGB images
    >>> y = keras.ops.image.resize(x, (2, 2))
    >>> y.shape
    (2, 2, 2, 3)

    >>> x = np.random.random((4, 4, 3)) # single RGB image
    >>> y = keras.ops.image.resize(x, (2, 2))
    >>> y.shape
    (2, 2, 3)

    >>> x = np.random.random((2, 3, 4, 4)) # batch of 2 RGB images
    >>> y = keras.ops.image.resize(x, (2, 2),
    ...     data_format="channels_first")
    >>> y.shape
    (2, 3, 2, 2)
    r&   z<Expected `size` to be a tuple of 2 integers. Received: size=r   r   zExpected an image array with shape `(height, width, channels)`, or `(batch_size, height, width, channels)`, but got input with incorrect rank, of shape .zIOnly one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` can be `True`.rC   )r<   r=   r>   r   r?   r@   rA   )	r'   r)   r(   r   r5   r3   r   r   rD   )	r   r;   r<   r=   r>   r?   r@   rA   r   s	            r   rD   rD      sI   F 4yyA~~%"% %
 
 	
 5;!s5;//!33F7<{F F F
 
 	

  
3 

 
 	
 UH%% 
'#!5 3!	
 	
 	
 -

		 =#1/   
 
 
r   c                   4     e Zd Z	 	 	 	 d fd	Zd Zd Z xZS )	AffineTransformr6   r7   r   r   c                     t                                                       || _        || _        || _        || _        d S r   )r   r   r<   r@   rA   r   )r   r<   r@   rA   r   r   s        r   r   zAffineTransform.__init__  s?     	*"$&r   c                 t    t           j                            ||| j        | j        | j        | j                  S )Nr<   r@   rA   r   )r   r   affine_transformr<   r@   rA   r   r   r   	transforms      r   r   zAffineTransform.call  s=    }--,n( . 
 
 	
r   c                     t          |j                  dvrt          d|j                   t          |j                  dvrt          d|j                   t          |j        |j                  S )Nr   r    )r#   r&   zInvalid transform rank: expected rank 1 (single transform) or rank 2 (batch of transforms). Received input with shape: transform.shape=r$   )r'   r(   r)   r   r%   rO   s      r   r*   z#AffineTransform.compute_output_spec(  s    u{6))-${- -  
 yv--5#,?5 5  
 5;ek::::r   r6   r7   r   r   r,   r1   s   @r   rJ   rJ     si         !#' ' ' ' ' '
 
 
; ; ; ; ; ; ;r   rJ   z keras.ops.image.affine_transformc                     t          | |f          r't          ||||                              | |          S t          j                            | |||||          S )a  Applies the given transform(s) to the image(s).

    Args:
        image: Input image or batch of images. Must be 3D or 4D.
        transform: Projective transform matrix/matrices. A vector of length 8 or
            tensor of size N x 8. If one row of transform is
            `[a0, a1, a2, b0, b1, b2, c0, c1]`, then it maps the output point
            `(x, y)` to a transformed input point
            `(x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k)`,
            where `k = c0 x + c1 y + 1`. The transform is inverted compared to
            the transform mapping input points to output points. Note that
            gradients are not backpropagated into transformation parameters.
            Note that `c0` and `c1` are only effective when using TensorFlow
            backend and will be considered as `0` when using other backends.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            and `"bilinear"`. Defaults to `"bilinear"`.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"reflect"`. Defaults to `"constant"`.
            - `"reflect"`: `(d c b a | a b c d | d c b a)`
                The input is extended by reflecting about the edge of the last
                pixel.
            - `"constant"`: `(k k k k | a b c d | k k k k)`
                The input is extended by filling all values beyond
                the edge with the same constant value k specified by
                `fill_value`.
            - `"wrap"`: `(a b c d | a b c d | a b c d)`
                The input is extended by wrapping around to the opposite edge.
            - `"nearest"`: `(a a a a | a b c d | d d d d)`
                The input is extended by the nearest pixel.
        fill_value: Value used for points outside the boundaries of the input if
            `fill_mode="constant"`. Defaults to `0`.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, weight)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.

    Returns:
        Applied affine transform image or batch of images.

    Examples:

    >>> x = np.random.random((2, 64, 80, 3)) # batch of 2 RGB images
    >>> transform = np.array(
    ...     [
    ...         [1.5, 0, -20, 0, 1.5, -16, 0, 0],  # zoom
    ...         [1, 0, -20, 0, 1, -16, 0, 0],  # translation
    ...     ]
    ... )
    >>> y = keras.ops.image.affine_transform(x, transform)
    >>> y.shape
    (2, 64, 80, 3)

    >>> x = np.random.random((64, 80, 3)) # single RGB image
    >>> transform = np.array([1.0, 0.5, -20, 0.5, 1.0, -16, 0, 0])  # shear
    >>> y = keras.ops.image.affine_transform(x, transform)
    >>> y.shape
    (64, 80, 3)

    >>> x = np.random.random((2, 3, 64, 80)) # batch of 2 RGB images
    >>> transform = np.array(
    ...     [
    ...         [1.5, 0, -20, 0, 1.5, -16, 0, 0],  # zoom
    ...         [1, 0, -20, 0, 1, -16, 0, 0],  # translation
    ...     ]
    ... )
    >>> y = keras.ops.image.affine_transform(x, transform,
    ...     data_format="channels_first")
    >>> y.shape
    (2, 3, 64, 80)
    rM   )r   rJ   r3   r   r   rN   )r   rP   r<   r@   rA   r   s         r   rN   rN   8  s    h UI.// *'!#	
 
 

 -y
)
)	* =))# *   r   c                   4     e Zd Z	 	 	 	 d fd	Zd Zd Z xZS )	ExtractPatchesNr#   validr   c                     t                                                       t          |t                    r||f}|| _        || _        || _        || _        || _        d S r   )	r   r   
isinstanceintr;   stridesdilation_ratepaddingr   )r   r;   rZ   r[   r\   r   r   s         r   r   zExtractPatches.__init__  sa     	dC   	 $<D	*&r   c                 ^    t          || j        | j        | j        | j        | j                  S )Nr   r;   rZ   r[   r\   r   )_extract_patchesr;   rZ   r[   r\   r   r   s     r   r   zExtractPatches.call  s7    L,L(
 
 
 	
r   c           	         |j         }| j        s| j        d         | j        d         f}| j        dk    r|j         d         }n|j         d         }t	          |j                   dk    rd|z   }| j        d         | j        d         z  |z  }| j        d         | j        d         f}t          ||||| j        | j        | j                  }t	          |j                   dk    r
|dd          }t          ||j	        	          S )
Nr   r#   r   r!   r   r"   )rZ   r\   r   r[   r(   r%   )
r(   rZ   r;   r   r'   r	   r\   r[   r   r%   )r   r   image_shaperZ   channels_infilterskernel_size	out_shapes           r   r*   z"ExtractPatches.compute_output_spec  s	   k| 	3y|TYq\2G..+b/KK+b/Ku{q  ,K)A,1-;y|TYq\2-L(,
 
 
	 u{q  !!""I%+>>>>r   Nr#   rV   r   r,   r1   s   @r   rU   rU     si         #' ' ' ' ' '"
 
 
? ? ? ? ? ? ?r   rU   zkeras.ops.image.extract_patchesNr#   rV   c                     t          | f          r't          |||||                              |           S t          | |||||          S )a  Extracts patches from the image(s).

    Args:
        image: Input image or batch of images. Must be 3D or 4D.
        size: Patch size int or tuple (patch_height, patch_widht)
        strides: strides along height and width. If not specified, or
            if `None`, it defaults to the same value as `size`.
        dilation_rate: This is the input stride, specifying how far two
            consecutive patch samples are in the input. For value other than 1,
            strides must be 1. NOTE: `strides > 1` is not supported in
            conjunction with `dilation_rate > 1`
        padding: The type of padding algorithm to use: `"same"` or `"valid"`.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, weight)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.

    Returns:
        Extracted patches 3D (if not batched) or 4D (if batched)

    Examples:

    >>> image = np.random.random(
    ...     (2, 20, 20, 3)
    ... ).astype("float32") # batch of 2 RGB images
    >>> patches = keras.ops.image.extract_patches(image, (5, 5))
    >>> patches.shape
    (2, 4, 4, 75)
    >>> image = np.random.random((20, 20, 3)).astype("float32") # 1 RGB image
    >>> patches = keras.ops.image.extract_patches(image, (3, 3), (1, 1))
    >>> patches.shape
    (18, 18, 27)
    )r;   rZ   r[   r\   r   r   )r   rU   r3   r_   r^   s         r   extract_patchesrj     sr    \ UH%% '#
 
 
 -

	 tWmW+   r   c                    t          |t                    r|x}}n6t          |          dk    r|d         |d         }}nt          d|           |dk    r| j        d         }n|dk    r| j        d         }|s|}||z  |z  }	t
          j                            |	| j        	          }
t
          j        	                    |
||||	f          }
d
}t          | j                  dk    r#d}t
          j        
                    | d          } t
          j                            | |
||||          }|r!t
          j                            |d          }|S )Nr&   r   r#   zPInvalid `size` argument. Expected an int or a tuple of length 2. Received: size=r   r!   channels_firstra   r$   Fr   Taxis)inputskernelrZ   r\   r   r[   )rX   rY   r'   	TypeErrorr(   r   numpyeyer%   reshapeexpand_dimsnnconvsqueeze)r   r;   rZ   r[   r\   r   patch_hpatch_wrd   out_dimrp   
_unbatchedpatchess                r   r_   r_     s    $ 
  ''	Ta7DGA:>A A
 
 	
 o%%k"o	(	(	(k"o +-G]wek::F]""';8 F J
5;1
))%a)88joo#   G  9-''a'88Nr   c                   ,     e Zd Zd fd	Zd Zd Z xZS )MapCoordinatesr#   r7   r   c                 r    t                                                       || _        || _        || _        d S r   )r   r   orderr@   rA   )r   r   r@   rA   r   s       r   r   zMapCoordinates.__init__=  s2    
"$r   c                 h    t           j                            ||| j        | j        | j                  S )N)r   r@   rA   )r   r   map_coordinatesr   r@   rA   r   r   coordinatess      r   r   zMapCoordinates.callC  s6    },,*n - 
 
 	
r   c                 6   |j         d         t          |j                   k    r%t          d|j          d|j         d                    t          |j                   dk     rt          d|j                    t          |j         dd          |j                  S )Nr   z_First dim of `coordinates` must be the same as the rank of `image`. Received image with shape: z and coordinate leading dim of r&   zOInvalid coordinates rank: expected at least rank 2. Received input with shape: r#   r$   )r(   r'   r)   r   r%   r   s      r   r*   z"MapCoordinates.compute_output_specL  s    Q3u{#3#3339.3k9 9 #."3A"69 9   { !!A%%C/:/@C C   ;,QRR0DDDDr   )r#   r7   r   r,   r1   s   @r   r   r   <  sb        % % % % % %
 
 
E E E E E E Er   r   zkeras.ops.image.map_coordinatesc                     t          | |f          r%t          |||                              | |          S t          j                            | ||||          S )a  Map the input array to new coordinates by interpolation..

    Note that interpolation near boundaries differs from the scipy function,
    because we fixed an outstanding bug
    [scipy/issues/2640](https://github.com/scipy/scipy/issues/2640).

    Args:
        input: The input array.
        coordinates: The coordinates at which input is evaluated.
        order: The order of the spline interpolation. The order must be `0` or
            `1`. `0` indicates the nearest neighbor and `1` indicates the linear
            interpolation.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"mirror"` and `"reflect"`. Defaults to
            `"constant"`.
            - `"constant"`: `(k k k k | a b c d | k k k k)`
                The input is extended by filling all values beyond
                the edge with the same constant value k specified by
                `fill_value`.
            - `"nearest"`: `(a a a a | a b c d | d d d d)`
                The input is extended by the nearest pixel.
            - `"wrap"`: `(a b c d | a b c d | a b c d)`
                The input is extended by wrapping around to the opposite edge.
            - `"mirror"`: `(c d c b | a b c d | c b a b)`
                The input is extended by mirroring about the edge.
            - `"reflect"`: `(d c b a | a b c d | d c b a)`
                The input is extended by reflecting about the edge of the last
                pixel.
        fill_value: Value used for points outside the boundaries of the input if
            `fill_mode="constant"`. Defaults to `0`.

    Returns:
        Output image or batch of images.

    )r   r   r3   r   r   r   )inputr   r   r@   rA   s        r   r   r   \  sq    P UK011 ,
 
 -{
+
+		,
 =((  r   c                   8     e Zd Z	 	 	 	 	 	 d fd	Zd Zd Z xZS )	PadImagesNc                     t                                                       || _        || _        || _        || _        || _        || _        d S r   )r   r   top_paddingbottom_paddingleft_paddingright_paddingtarget_heighttarget_width)r   r   r   r   r   r   r   r   s          r   r   zPadImages.__init__  sQ     	&,(**(r   c           	      h    t          || j        | j        | j        | j        | j        | j                  S r   )_pad_imagesr   r   r   r   r   r   r   imagess     r   r   zPadImages.call  s;    
 
 	
r   c                    t          j        |          }| j        4t          |          dk    rdnd}| j        ||         z   | j        z   | _        | j        4t          |          dk    rdnd}| j        ||         z   | j        z   | _        |d         | j        | j        |d         f}t          |          dk    r
|dd          }t          ||j
                  S )Nr   r   r#   r&   r!   rb   )r   r(   r   r'   r   r   r   r   r   r   r%   )r   r   images_shapeheight_axis
width_axisrg   s         r   r*   zPadImages.compute_output_spec  s   y((%"<00A55!!1K {+,%& 
 $!,//144!J!z*+$%  O	
	 |!!!!""I,
 
 
 	
r   NNNNNNr,   r1   s   @r   r   r     so         ) ) ) ) ) )"	
 	
 	

 
 
 
 
 
 
r   r   zkeras.ops.image.pad_imagesc           	          t          | f          r't          ||||||                              |           S t          | ||||||          S )a  Pad `images` with zeros to the specified `height` and `width`.

    Args:
        images: 4D Tensor of shape `(batch, height, width, channels)` or 3D
            Tensor of shape `(height, width, channels)`.
        top_padding: Number of rows of zeros to add on top.
        bottom_padding: Number of rows of zeros to add at the bottom.
        left_padding: Number of columns of zeros to add on the left.
        right_padding: Number of columns of zeros to add on the right.
        target_height: Height of output images.
        target_width: Width of output images.

    Returns:
        If `images` were 4D, a 4D float Tensor of shape
            `(batch, target_height, target_width, channels)`
        If `images` were 3D, a 3D float Tensor of shape
            `(target_height, target_width, channels)`

    Example:

    >>> images = np.random.random((15, 25, 3))
    >>> padded_images = keras.ops.image.pad_images(
    ...     images, 2, 3, target_height=20, target_width=30
    ... )
    >>> padded_images.shape
    (20, 30, 3)

    >>> batch_images = np.random.random((2, 15, 25, 3))
    >>> padded_batch = keras.ops.image.pad_images(
    ...     batch_images, 2, 3, target_height=20, target_width=30
    ... )
    >>> padded_batch.shape
    (2, 20, 30, 3))r   r   r3   r   )r   r   r   r   r   r   r   s          r   
pad_imagesr     ss    X VI&&  
 
 -

	    r   c                    t          j        |           } d}t          j        |           }t	          |          dk    r#d}t           j                            | d          } n%t	          |          dk    rt          d|           t          j        |           \  }	}
}}|||g                    d           dk    rt          d| d	| d
|           |||g                    d           dk    rt          d| d| d|           |||z
  |
z
  }|||z
  |
z
  }|||z
  |z
  }|||z
  |z
  }|dk     rt          d|           |dk     rt          d|           |dk     rt          d|           |dk     rt          d|           t           j        	                    t           j        
                    dd||||ddg          ddg          }t           j                            | |          }|||
z   |z   }|||z   |z   }|	|||g}t           j        	                    ||          }|s"t           j                            |dg          }|S )NTr   Fr   r   VInvalid shape for argument `images`: it must have rank 3 or 4. Received: images.shape=r#   z^Must specify exactly two of top_padding, bottom_padding, target_height. Received: top_padding=z, bottom_padding=, target_height=z^Must specify exactly two of left_padding, right_padding, target_width. Received: left_padding=z, right_padding=, target_width=z0top_padding must be >= 0. Received: top_padding=z2left_padding must be >= 0. Received: left_padding=z4right_padding must be >= 0. Received: right_padding=z6bottom_padding must be >= 0. Received: bottom_padding=r&   rm   )r   convert_to_tensorr   r(   r'   rr   ru   r)   countrt   stackpadrx   )r   r   r   r   r   r   r   is_batchr   batchheightwidthdepthpaddingspaddedpadded_shapes                   r   r   r     sd    &v..FH9V$$L
<A**6155	\		a		5&25 5
 
 	
 #&)F"3"3E65%^]399$??1DD-%0- - -- - +	- -
 
 	
 	m\288>>!CC+&2+ + ++ + )	+ +
 
 	
 #n4v=&4v=#m3e;$|3e;QO+OO
 
 	
 a5&25 5
 
 	
 q7'47 7
 
 	
 9(69 9
 
 	

 }$$		
 	
 
A H ]vx00F#f,~=#e+m;=,>L]""6<88F 9&&vQC&88Mr   c                   *     e Zd Z fdZd Zd Z xZS )
CropImagesc                     t                                                       || _        || _        || _        || _        || _        || _        d S r   )r   r   top_croppingbottom_croppingleft_croppingright_croppingr   r   )r   r   r   r   r   r   r   r   s          r   r   zCropImages.__init__s  sQ     	(.*,*(r   c           	      h    t          || j        | j        | j        | j        | j        | j                  S r   )_crop_imagesr   r   r   r   r   r   r   s     r   r   zCropImages.call  s;     
 
 	
r   c                    t          j        |          }|d         | j        | j        |d         f}| j        4t	          |          dk    rdnd}| j        ||         z
  | j        z
  | _        | j        4t	          |          dk    rdnd}| j        ||         z
  | j        z
  | _        |d         | j        | j        |d         f}t	          |          dk    r
|dd          }t          ||j
                  S )Nr   r!   r   r#   r&   rb   )r   r(   r   r   r'   r   r   r   r   r   r%   )r   r   r   rg   r   r   s         r   r*   zCropImages.compute_output_spec  s0   y((O	
	 %"<00A55!!1K!{+,&' 
 $!,//144!J"z*+%&  O	
	 |!!!!""I,
 
 
 	
r   r,   r1   s   @r   r   r   r  sV        ) ) ) ) )"	
 	
 	
!
 !
 !
 !
 !
 !
 !
r   r   zkeras.ops.image.crop_imagesc           	          t          | f          r't          ||||||                              |           S t          | ||||||          S )a  Crop `images` to a specified `height` and `width`.

    Args:
        images: 4-D batch of images of shape `(batch, height, width, channels)`
             or 3-D single image of shape `(height, width, channels)`.
        top_cropping: Number of columns to crop from the top.
        bottom_cropping: Number of columns to crop from the bottom.
        left_cropping: Number of columns to crop from the left.
        right_cropping: Number of columns to crop from the right.
        target_height: Height of the output images.
        target_width: Width of the output images.

    Returns:
        If `images` were 4D, a 4D float Tensor of shape
            `(batch, target_height, target_width, channels)`
        If `images` were 3D, a 3D float Tensor of shape
            `(target_height, target_width, channels)`

    Example:

    >>> images = np.reshape(np.arange(1, 28, dtype="float32"), [3, 3, 3])
    >>> images[:,:,0] # print the first channel of the images
    array([[ 1.,  4.,  7.],
           [10., 13., 16.],
           [19., 22., 25.]], dtype=float32)
    >>> cropped_images = keras.image.crop_images(images, 0, 0, 2, 2)
    >>> cropped_images[:,:,0] # print the first channel of the cropped images
    array([[ 1.,  4.],
           [10., 13.]], dtype=float32))r   r   r3   r   )r   r   r   r   r   r   r   s          r   crop_imagesr     ss    P VI&&  
 
 -

	    r   c           
      f   t          j        |           } d}t          j        |           }t	          |          dk    r#d}t           j                            | d          } n%t	          |          dk    rt          d|           t          j        |           \  }	}
}}|||g                    d           dk    rt          d| d	| d
|           |||g                    d           dk    rt          d| d| d|           ||
|z
  |z
  }||
|z
  |z
  }|||z
  |z
  }|||z
  |z
  }|dk     rt          d|           |dk     rt          d|           |dk     rt          d|           |dk     rt          d|           t          j	        | t           j        
                    d||dg          t           j        
                    |	|||g                    }|	|||g}t           j                            ||          }|s"t           j                            |dg          }|S )NTr   Fr   r   r   r#   zaMust specify exactly two of top_cropping, bottom_cropping, target_height. Received: top_cropping=z, bottom_cropping=r   zaMust specify exactly two of left_cropping, right_cropping, target_width. Received: left_cropping=z, right_cropping=r   z2top_cropping must be >= 0. Received: top_cropping=z4target_height must be >= 0. Received: target_height=z4left_cropping must be >= 0. Received: left_cropping=z2target_width must be >= 0. Received: target_width=rm   )r   r   r   r(   r'   rr   ru   r)   r   slicer   rt   rx   )r   r   r   r   r   r   r   r   r   r   r   r   r   croppedcropped_shapes                  r   r   r     s&    &v..FH9V$$L
<A**6155	\		a		5&25 5
 
 	
 #&)F"3"3E65%o}5;;DAAQFF-&2- -  /- - +	- -
 
 	
 	~|4::4@@AEE+'4+ + -+ + )	+ +
 
 	
 -?0<?,~=~-=a5&25 5
 
 	
 q7'47 7
 
 	
 q7'47 7
 
 	
 a5&25 5
 
 	

 iQmQ?@@UM<GHH G M<?Mm##G];;G ;-''qc'::Nr   r+   rF   rR   rh   )r7   r   r   )	keras.srcr   r   keras.src.api_exportr   keras.src.backendr   r   keras.src.ops.operationr   keras.src.ops.operation_utilsr	   r   r   r5   rD   rJ   rN   rU   rj   r_   r   r   r   r   r   r   r   r    r   r   <module>r      s               - - - - - - ) ) ) ) ) ) 2 2 2 2 2 2 - - - - - - C C C C C C) ) ) ) )Y ) ) )X 011  1 1 1 211h7
 7
 7
 7
 7
Y 7
 7
 7
t &'' g g g ('gT%; %; %; %; %;i %; %; %;P 011 a a a 21aH3? 3? 3? 3? 3?Y 3? 3? 3?l /00 8 8 8 108| * * * *ZE E E E EY E E E@ /00@A3 3 3 103l8
 8
 8
 8
 8
	 8
 8
 8
v *++ = = = ,+=@` ` `F>
 >
 >
 >
 >
 >
 >
 >
B +,, 9 9 9 -,9xR R R R Rr   