
    6biE              	          S r SSKrSSKrSSKrSSKrSSKJs  Jr	  SSK
Jr  SSKJr   SSKJr   \R"                  r\bE  \R*                  \R,                  \R.                  \R0                  \R2                  \R4                  S.r\	R8                  R:                  r\R,                  \R<                  \R.                  \R>                  \R@                  \RB                  \RD                  \RF                  S.r$\" S/ S	9SS
 j5       r%S r&\" SS5      SS j5       r'\" SS5      SS j5       r(\" SS5      SS j5       r)\" SS5           SS j5       r*g! \ a    \r GN f = f! \ a    SrSr GN1f = f)z$Utilities related to image handling.    N)backend)keras_export)Image)nearestbilinearbicubichammingboxlanczos)r   r   r   arealanczos3lanczos5gaussianmitchellcubicz&keras.preprocessing.image.smart_resize)v1c                 X   [        U5      S:w  a  [        SU S35      e[        R                  " U 5      nUR                  R
                  bM  UR                  R
                  S:  d  UR                  R
                  S:  a  [        SUR                   S35      e[        R                  " U5      nUS   US	   peUu  pxUR                  R
                  b  UR                  S
   n	OSn	[        R                  " [        R                  " Xg-  S5      U-  S5      n
[        R                  " [        R                  " XX-  S5      U-  S5      n[        R                  " XZ5      n
[        R                  " Xk5      n[        R                  " [        R                  " XZ-
  S5      S-  S5      n[        R                  " [        R                  " Xk-
  S5      S-  S5      nUR                  R
                  S:X  a3  [        R                  " SXS/5      n[        R                  " S
XS
/5      nO0[        R                  " XS/5      n[        R                  " XS
/5      n[        R                  " X>U5      n[        R                  R                  X1US9nUR                  R
                  b]  UR                  R
                  S:X  a  UR                  SSSU	45        UR                  R
                  S:X  a  UR                  SSU	45        [        U [        R                  5      (       a  UR!                  5       $ U$ )a	  Resize images to a target size without aspect ratio distortion.

Warning: `tf.keras.preprocessing.image.smart_resize` is not recommended for
new code. Prefer `tf.keras.layers.Resizing`, which provides the same
functionality as a preprocessing layer and adds `tf.RaggedTensor` support.
See the [preprocessing layer guide](
https://www.tensorflow.org/guide/tf_keras/preprocessing_layers)
for an overview of preprocessing layers.

TensorFlow image datasets typically yield images that have each a different
size. However, these images need to be batched before they can be
processed by TF-Keras layers. To be batched, images need to share the same
height and width.

You could simply do:

```python
size = (200, 200)
ds = ds.map(lambda img: tf.image.resize(img, size))
```

However, if you do this, you distort the aspect ratio of your images, since
in general they do not all have the same aspect ratio as `size`. This is
fine in many cases, but not always (e.g. for GANs this can be a problem).

Note that passing the argument `preserve_aspect_ratio=True` to `resize`
will preserve the aspect ratio, but at the cost of no longer respecting the
provided target size. Because `tf.image.resize` doesn't crop images,
your output images will still have different sizes.

This calls for:

```python
size = (200, 200)
ds = ds.map(lambda img: smart_resize(img, size))
```

Your output images will actually be `(200, 200)`, and will not be distorted.
Instead, the parts of the image that do not fit within the target size
get cropped out.

The resizing process is:

1. Take the largest centered crop of the image that has the same aspect
ratio as the target size. For instance, if `size=(200, 200)` and the input
image has size `(340, 500)`, we take a crop of `(340, 340)` centered along
the width.
2. Resize the cropped image to the target size. In the example above,
we resize the `(340, 340)` crop to `(200, 200)`.

Args:
  x: Input image or batch of images (as a tensor or NumPy array). Must be in
    format `(height, width, channels)` or `(batch_size, height, width,
    channels)`.
  size: Tuple of `(height, width)` integer. Target size.
  interpolation: String, interpolation to use for resizing. Supports
    `bilinear`, `nearest`, `bicubic`, `area`, `lanczos3`, `lanczos5`,
    `gaussian`, `mitchellcubic`. Defaults to `'bilinear'`.

Returns:
  Array with shape `(size[0], size[1], channels)`. If the input image was a
  NumPy array, the output is a NumPy array, and if it was a TF tensor,
  the output is a TF tensor.
   z6Expected `size` to be a tuple of 2 integers, but got: .N      zExpected an image array with shape `(height, width, channels)`, or `(batch_size, height, width, channels)`, but got input with incorrect rank, of shape float32int32r   )imagessizemethod)len
ValueErrortfconvert_to_tensorshaperankcastminimumstacksliceimageresize	set_shape
isinstancenpndarraynumpy)xr   interpolationimgr#   heightwidthtarget_heighttarget_widthstatic_num_channelscrop_height
crop_widthcrop_box_hstartcrop_box_wstartcrop_box_startcrop_box_sizes                   X/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/utils/image_utils.pysmart_resizer?   B   s   D 4yA~DTF!L
 	
 

q
!C
yy~~!99>>A!!3;;>99+QH 
 HHSME"IuRyE"&M
yy~~!!iim"''
%y1L@'K 
%y1MA7J **V1KE.Jgg
$i014gO gg
"I.2GO yy~~1o"JK"kr!BC?Q"GH+2!>?
((3
6C
((///
FC
yy~~!99>>QMM4t-@AB99>>QMM4':;<!RZZ  yy{J    c                     U R                  5       n U [        ;  a-  [        SR                  U [        R	                  5       5      5      e[        U    $ )NzFValue not recognized for `interpolation`: {}. Supported values are: {})lower_TF_INTERPOLATION_METHODSNotImplementedErrorformatkeys)r1   s    r>   get_interpolationrG      sN    !'')M55!f],E,J,J,LM
 	
 %]33r@   zkeras.utils.array_to_imgz&keras.preprocessing.image.array_to_imgc                    Uc  [         R                  " 5       nUc  [         R                  " 5       n[        c  [	        S5      e[
        R                  " XS9n U R                  S:w  a  [        SU R                   35      eUS;  a  [        SU 35      eUS:X  a  U R                  S	S
S5      n U(       a>  U [
        R                  " U 5      -
  n [
        R                  " U 5      nUS:w  a  X-  n U S-  n U R                  S
   S:X  a&  [        R                  " U R                  S5      S5      $ U R                  S
   S:X  a&  [        R                  " U R                  S5      S5      $ U R                  S
   S	:X  az  [
        R                  " U 5      S:  a0  [        R                  " U SS2SS2S4   R                  S5      S5      $ [        R                  " U SS2SS2S4   R                  S5      S5      $ [        SU R                  S
    35      e)a  Converts a 3D Numpy array to a PIL Image instance.

Usage:

```python
from PIL import Image
img = np.random.random(size=(100, 100, 3))
pil_img = tf.keras.utils.array_to_img(img)
```


Args:
    x: Input data, in any form that can be converted to a Numpy array.
    data_format: Image data format, can be either `"channels_first"` or
      `"channels_last"`. None means the global
      setting `tf.keras.backend.image_data_format()` is used (unless you
      changed it, it uses `"channels_last"`). Defaults to `None`.
    scale: Whether to rescale the image such that minimum and maximum values
      are 0 and 255 respectively. Defaults to `True`.
    dtype: Dtype to use. None makes the global setting
      `tf.keras.backend.floatx()` to be used (unless you changed it, it
      uses `"float32"`). Defaults to `None`.

Returns:
    A PIL Image instance.

Raises:
    ImportError: if PIL is not available.
    ValueError: if invalid `x` or `data_format` is passed.
NzCCould not import PIL.Image. The use of `array_to_img` requires PIL.dtyper   zJExpected image array to have rank 3 (single image). Got array with shape:    channels_lastchannels_firstzInvalid data_format: rM      r   r      r   uint8RGBARGBr   ILzUnsupported channel number: )r   image_data_formatfloatx	pil_imageImportErrorr-   asarrayndimr    r#   	transposeminmax	fromarrayastype)r0   data_formatscalerJ   x_maxs        r>   array_to_imgrc      s   F //1} 6
 	
 	

1"Avv{%%&WWI/
 	

 ==0>??
 &&KK1a q	Mq	A:JA	SwwqzQ""188G#4f==	
q""188G#4e<<	
q66!9s?&&qAqz'8'8'A3GG""1Q1W:#4#4W#=sCC7
|DEEr@   zkeras.utils.img_to_arrayz&keras.preprocessing.image.img_to_arrayc                 J   Uc  [         R                  " 5       nUc  [         R                  " 5       nUS;  a  [        SU 35      e[        R
                  " XS9n[        UR                  5      S:X  a  US:X  a  UR                  SSS5      nU$ [        UR                  5      S:X  ag  US:X  a0  UR                  SUR                  S   UR                  S   45      nU$ UR                  UR                  S   UR                  S   S45      n U$ [        S	UR                   35      e)
a'  Converts a PIL Image instance to a Numpy array.

Usage:

```python
from PIL import Image
img_data = np.random.random(size=(100, 100, 3))
img = tf.keras.utils.array_to_img(img_data)
array = tf.keras.utils.image.img_to_array(img)
```


Args:
    img: Input PIL Image instance.
    data_format: Image data format, can be either `"channels_first"` or
      `"channels_last"`. None means the global
      setting `tf.keras.backend.image_data_format()` is used (unless you
      changed it, it uses `"channels_last"`). Defaults to `None`.
    dtype: Dtype to use. None makes the global setting
      `tf.keras.backend.floatx()` to be used (unless you changed it, it
      uses `"float32"`). Defaults to `None`.

Returns:
    A 3D Numpy array.

Raises:
    ValueError: if invalid `img` or `data_format` is passed.
rK   zUnknown data_format: rI   r   rM   r   r   rN   zUnsupported image shape: )
r   rU   rV   r    r-   rY   r   r#   r[   reshape)r2   r`   rJ   r0   s       r>   img_to_arrayrf     s   B //1} ==0>?? 	

3$A
177|q**Aq!$A H 
QWW	**		1aggaj!''!*56A
 H 		1771:qwwqz156A H 4QWWI>??r@   zkeras.utils.save_imgz"keras.preprocessing.image.save_imgc                     Uc  [         R                  " 5       n[        XUS9nUR                  S:X  a3  US:X  d  US:X  a'  [        R
                  " S5        UR                  S5      nUR                  " U 4SU0UD6  g)	a>  Saves an image stored as a Numpy array to a path or file object.

Args:
    path: Path or file object.
    x: Numpy array.
    data_format: Image data format, either `"channels_first"` or
      `"channels_last"`.
    file_format: Optional file format override. If omitted, the format to
      use is determined from the filename extension. If a file object was
      used instead of a filename, this parameter should always be used.
    scale: Whether to rescale image values to be within `[0, 255]`.
    **kwargs: Additional keyword arguments passed to `PIL.Image.save()`.
N)r`   ra   rQ   jpgjpegz?The JPG format does not support RGBA images, converting to RGB.rR   rE   )r   rU   rc   modewarningswarnconvertsave)pathr0   r`   file_formatra   kwargsr2   s          r>   save_imgrr   R  ss     //1
q
?C
xx6{e3{f7LM	
 kk% HHT0+00r@   zkeras.utils.load_imgz"keras.preprocessing.image.load_imgc           	      `   U(       a  [         R                  " S5        Sn[        c  [        S5      e[	        U [
        R                  5      (       a  [        R                  " U 5      nO[	        U [        R                  [        [        45      (       a  [	        U [        R                  5      (       a  [        U R                  5       5      n [        U S5       n[        R                  " [
        R                  " UR                  5       5      5      nSSS5        O[        S[        U 5       35      eUS:X  a"  WR                   S;  a  UR#                  S5      nO[US	:X  a"  WR                   S
:w  a  UR#                  S
5      nO3US:X  a"  WR                   S:w  a  UR#                  S5      nO[%        S5      eUb  US   US   4nUR&                  U:w  a  U[(        ;  a<  [%        SR+                  USR-                  [(        R/                  5       5      5      5      e[(        U   n	U(       af  UR&                  u  pUu  pX-  U-  nX-  U-  n[1        X5      n[1        X5      nX-
  S-  nX-
  S-  nUU-   nUU-   nUUUU/nUR3                  XUS9nU$ UR3                  X5      nU$ ! , (       d  f       GN~= f)a:  Loads an image into PIL format.

Usage:

```python
image = tf.keras.utils.load_img(image_path)
input_arr = tf.keras.utils.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
predictions = model.predict(input_arr)
```

Args:
    path: Path to image file.
    grayscale: DEPRECATED use `color_mode="grayscale"`.
    color_mode: One of `"grayscale"`, `"rgb"`, `"rgba"`. Default: `"rgb"`.
      The desired image format.
    target_size: Either `None` (default to original size) or tuple of ints
      `(img_height, img_width)`.
    interpolation: Interpolation method used to resample the image if the
      target size is different from that of the loaded image. Supported
      methods are `"nearest"`, `"bilinear"`, and `"bicubic"`. If PIL version
      1.1.3 or newer is installed, `"lanczos"` is also supported. If PIL
      version 3.4.0 or newer is installed, `"box"` and `"hamming"` are also
      supported. By default, `"nearest"` is used.
    keep_aspect_ratio: Boolean, whether to resize images to a target
            size without aspect ratio distortion. The image is cropped in
            the center with target aspect ratio before resizing.

Returns:
    A PIL Image instance.

Raises:
    ImportError: if PIL is not available.
    ValueError: if interpolation method is not supported.
z<grayscale is deprecated. Please use color_mode = "grayscale"	grayscaleNz?Could not import PIL.Image. The use of `load_img` requires PIL.rbz,path should be path-like or io.BytesIO, not )rT   zI;16rS   rT   rgbarQ   rgbrR   z0color_mode must be "grayscale", "rgb", or "rgba"rN   r   zCInvalid interpolation method {} specified. Supported methods are {}z, r   )r
   )rk   rl   rW   rX   r,   ioBytesIOopenpathlibPathbytesstrresolveread	TypeErrortyperj   rm   r    r   _PIL_INTERPOLATION_METHODSrE   joinrF   r\   r*   )ro   rt   
color_modetarget_sizer1   keep_aspect_ratior2   fwidth_height_tupleresampler4   r3   r6   r5   r8   r9   r:   r;   crop_box_wendcrop_box_hendcrop_boxs                        r>   load_imgr   l  s   X J	
 !
M
 	
 $

##nnT"	D7<<4	5	5dGLL))t||~&D$..AFFH!56C  :4:,G
 	
 [  88--++c"C	v	88v++f%C	u	88u++e$CKLL)!nk!n=88))$>> %%+V%		"<"A"A"CD&  2-@H  #.@+$4E$3E
 "&6 3
#)#7A"=#(#5!"; /* < /+ =##!!	 jj!38jL J jj!3>Ju s   9J
J-)r   )NTN)NN)NNT)Frw   Nr   F)+__doc__rx   r{   rk   r/   r-   tensorflow.compat.v2compatv2r!   tf_keras.srcr    tensorflow.python.util.tf_exportr   PILr   rW   
Resamplingpil_image_resamplingAttributeErrorrX   NEARESTBILINEARBICUBICHAMMINGBOXLANCZOSr   r)   ResizeMethodNEAREST_NEIGHBORAREALANCZOS3LANCZOS5GAUSSIANMITCHELLCUBICrC   r?   rG   rc   rf   rr   r    r@   r>   <module>r      s   + 
    ! !   :	 &)(33 #'//(11'//'//#'''//" xx$$ %%,,##%%%%%%!//	  62>y ?yx4  HKFKF\  H22j $&JK1 L12 $&JK s LsU
  )()  I s.   E  E EE  EE   
E.-E.