Bug/Problem when ploting the result of `tf.image.resize`. Solution is given to the problem
@VachanVY  isn't that a 'problem' with pyplot?
From what I remember, if the image is given as `uint` then it expects range 0-255. If it is given as `float`, it expects 0-1.
So resizing the image might change the output format to `float` with range 0-255, thus requiring you to divide by 255 or casting back to int to observe the correct image. Hi @maksym33,
> So resizing the image might change the output format to `float` with range 0-255, thus requiring you to divide by 255 or casting back to int to observe the correct image.

when given unnormalized values 0-255 as input it should output `int` values between 0-255 not decimal values between 0-255, which should be corrected The default method of interpolation for `tf.image.resize` is `bilinear`, so it would make sense for it to return floats, otherwise you would lose even more precision when resizing. You could try `nearest` method if you want to cast to int without losing precision.
Oh yeah, found this little note in the docs for resize, seems I had good instincts:
![image](https://github.com/tensorflow/tensorflow/assets/39015826/3aa4a6d6-c1fc-4ec8-8059-21f632beefe7)
 > The default method of interpolation for `tf.image.resize` is `bilinear`, so it would make sense for it to return floats, otherwise you would lose even more precision when resizing. You could try `nearest` method if you want to cast to int without losing precision. Oh yeah, found this little note in the docs for resize, seems I had good instincts: ![image](https://user-images.githubusercontent.com/39015826/277671632-3aa4a6d6-c1fc-4ec8-8059-21f632beefe7.png)

Never argued about that, just saying returning float values between 0-255 is of no use (if it is between 0-255 it should be int), so modifying `tf.image.resize` to return normalized tensor (0-1.) will be of use because that's the norm

What's the norm?
Ans: To return int values between 0-255 ***OR*** float values between 0-1 
(you shouldn't be mixing both which is happening with `tf.image.resize` which should be fixed, just divide input by 255. when input's not normalized because returning floats between 0-255 is wrong!) I think unexpectedly performing a rescaling operation when only trying to resize an image is even more confusing - and that is assuming that people use it only for the above two specified formats, which is not always the case.
 In Tensorflow/Deep Learning, only the above-mentioned formats are used (as per my knowledge).
If not, specify the others. Anyway, mixing things from 2 formats is not justifiable. I think swapping between dtypes is much more less troublesome than swapping between scales - which seem more pyplot specific. There is also the case of using colours encoded as hsv or cieLAB. Mostly we only use the above 2 mentioned formats (0-255 ints or 0-1 floats) for input to computer vision pipelines.

> I think swapping between dtypes is much more less troublesome than swapping between scales - which seem more pyplot specific. There is also the case of using colours encoded as hsv or cieLAB.

Ok but you cannot mix 2 formats just because it's less troublesome or whatever...
I only have to say this much (otherwise I'll be repeating), @sushreebarsa Please respond or assign the issue to someone else Hi, @VachanVY!   Sorry for the late response!
This is a known issue which generally occurs in TensorFlow when plotting the result of tf.image.resize. As a workaround you can use the` tf.cast()` function. You can convert the image to a `uint8` array before plotting it.
Please let us know if it helps?
Thank you! @sushreebarsa  How about including that casting inside `tf.image.resize` depending on the `dtype` of input? It's not only helpful for plotting but also gives the correct format for input into deep learning models.

Current behaviour:
returns `floats` between `0-255` when given an image with `int` values `0-255`  which is wrong.
no problem when given floats between `0-1` as it returns `floats` between `0-1` which is a correct format unlike above.
 @VachanVY Yes, we can use casting inside `tf.image.resize `depending on the dtype of input. As an example please refer to the following;
```
import tensorflow as tf

def resize_with_casting(image, size, dtype=tf.float32):
  """Resizes an image to a given size, casting the input to the desired dtype.

  Args:
    image: A Tensor of shape [height, width, channels] representing the image to resize.
    size: A list of two ints representing the new height and width of the image.
    dtype: The desired dtype of the output image.

  Returns:
    A Tensor of shape [new_height, new_width, channels] representing the resized image.
  """


  # Determine the current dtype of the input image.
  current_dtype = image.dtype

  # If the current dtype is not the desired dtype, cast the image.
  if current_dtype != dtype:
    image = tf.cast(image, dtype)

  # Resize the image.
  resized_image = tf.image.resize(image, size)

  # Return the resized image.
  return resized_image

# Example usage:

# Resize an image to a size of 256x256, casting the input to tf.float32.
image = tf.random.normal([512, 512, 3], dtype=tf.uint8)
resized_image = resize_with_casting(image, [256, 256])

# Resize an image to a size of 128x128, casting the input to tf.uint8.
image = tf.random.uniform([256, 256, 3])
resized_image = resize_with_casting(image, [128, 128], dtype=tf.uint8)


```
Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. > @VachanVY Yes, we can use casting inside `tf.image.resize `depending on the dtype of input. As an example please refer to the following;
> 
> ```
> import tensorflow as tf
> 
> def resize_with_casting(image, size, dtype=tf.float32):
>   """Resizes an image to a given size, casting the input to the desired dtype.
> 
>   Args:
>     image: A Tensor of shape [height, width, channels] representing the image to resize.
>     size: A list of two ints representing the new height and width of the image.
>     dtype: The desired dtype of the output image.
> 
>   Returns:
>     A Tensor of shape [new_height, new_width, channels] representing the resized image.
>   """
> 
> 
>   # Determine the current dtype of the input image.
>   current_dtype = image.dtype
> 
>   # If the current dtype is not the desired dtype, cast the image.
>   if current_dtype != dtype:
>     image = tf.cast(image, dtype)
> 
>   # Resize the image.
>   resized_image = tf.image.resize(image, size)
> 
>   # Return the resized image.
>   return resized_image
> 
> # Example usage:
> 
> # Resize an image to a size of 256x256, casting the input to tf.float32.
> image = tf.random.normal([512, 512, 3], dtype=tf.uint8)
> resized_image = resize_with_casting(image, [256, 256])
> 
> # Resize an image to a size of 128x128, casting the input to tf.uint8.
> image = tf.random.uniform([256, 256, 3])
> resized_image = resize_with_casting(image, [128, 128], dtype=tf.uint8)
> ```
> 
> Thank you!

I know I can write my own function but can that be included inside `tf.image.resize` is my question @VachanVY As far as I know, it is not possible to include your own custom function inside tf.image.resize. As tf.image.resize is a built-in TensorFlow function that is implemented in C++ and cannot be modified.

Thank you! Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62205">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62205">No</a>
 @VachanVY Reopening this issue as it still persists. 
@sachinprasadhs Could you please have a look at this.
Thank you! Hi, 

Apologies for the delayed response, tf.image.resize does not do any other validation on the range of values to return the normalized values between 0-1, it mainly performs on the method specified as shown in the implementation below.

https://github.com/tensorflow/tensorflow/blob/4dacf3f368eb7965e9b5c3bbdd5193986081c3b2/tensorflow/python/ops/image_ops_impl.py#L1740-L1787

In oder to change the existing implementation as per your suggestion, it may break for many users. 
Will escalate the issue to the team and wait for their response. 
### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

tf 2.13.0

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

3.10.12

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

```python
import tensorflow as tf
import matplotlib.pyplot as plt

image_string = tf.io.read_file("/content/_1574258b-b76a-401c-8c1d-f9cf632d7631.jpeg")
tensor = tf.io.decode_jpeg(image_string, channels=3)
print(tensor.shape)
plt.imshow(tensor.numpy())
plt.show()
```
![download](https://github.com/tensorflow/tensorflow/assets/109357590/091608bc-25e9-4165-b911-2787366e5eb1)

```python
plt.imshow((tf.image.resize(tensor, [100, 100])).numpy())
plt.show() # NO IMAGE, THERE IS PROBLEM
print(tf.image.resize(tensor, [100, 100]))
```
> WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

![download](https://github.com/tensorflow/tensorflow/assets/109357590/0fcaa65a-88cd-46d7-ac9e-acd4ca0b9fde)
```python
<tf.Tensor: shape=(100, 100, 3), dtype=float32, numpy=
array([[[ 12.7644  ,  18.7644  ,  32.7644  ],
        [ 14.      ,  20.      ,  34.      ],
        [ 14.      ,  20.      ,  34.      ],
        ...,
        [ 16.619999,  25.619999,  40.62    ],
        [ 16.039814,  28.039814,  44.039814],
        [ 17.995617,  29.995617,  45.995617]],

       [[ 14.      ,  20.      ,  34.      ],
        [ 15.      ,  21.      ,  35.      ],
        [ 16.      ,  22.      ,  36.      ],
        ...,
        [ 18.899963,  27.899963,  42.899963],
        [ 15.279907,  27.279907,  43.279907],
        [ 16.14    ,  28.14    ,  44.14    ]],

       [[ 14.      ,  22.      ,  35.      ],
        [ 16.099998,  24.099998,  37.1     ],
        [ 16.      ,  22.      ,  36.      ],
        ...,
        [ 15.619899,  31.619898,  46.6199  ],
        [ 15.907904,  32.9079  ,  48.9079  ],
        [ 17.240013,  34.240013,  50.240013]],

       ...,

       [[ 26.533968,  24.533968,  35.533966],
        [ 25.967827,  23.967827,  34.967827],
        [ 33.67996 ,  30.67996 ,  39.67996 ],
        ...,
        [114.779915,  31.979984,  41.879948],
        [119.47677 ,  33.44877 ,  44.46277 ],
        [103.15075 ,  21.950676,  31.050713]],

       [[ 27.532555,  25.532555,  36.532555],
        [ 27.5823  ,  25.5823  ,  36.5823  ],
        [ 23.576063,  21.576063,  32.576065],
        ...,
        [104.3003  ,  30.880442,  40.020397],
        [100.94113 ,  24.181866,  32.0615  ],
        [112.63941 ,  38.57296 ,  45.57296 ]],

       [[ 34.041607,  32.04161 ,  43.041607],
        [ 20.49961 ,  18.49961 ,  29.49961 ],
        [ 24.038023,  22.038023,  33.03802 ],
        ...,
        [ 83.27584 ,  32.956017,  39.75404 ],
        [ 95.79817 ,  19.357985,  27.21803 ],
        [100.01961 ,  31.019604,  36.019604]]], dtype=float32)>
```
```python
# SOLUTION
plt.imshow(tf.cast(tf.image.resize(tensor, [100, 100]), tf.int8)) # output range [0, 255] which is a correct format
plt.show()
```
![download](https://github.com/tensorflow/tensorflow/assets/109357590/6f20296b-97dd-4fde-9f98-5d3494679517)
```python
# OR (normalizing gives a better image, the below image is better than the above)
plt.imshow((tf.image.resize(tensor, [100, 100])/255.).numpy())
plt.show() # BUT ON NORMALIZING WE GET THE PROPER IMAGE
# OR
plt.imshow((tf.image.resize(tf.cast(tensor, tf.float32)/255., [100, 100])).numpy())
plt.show()
# Both give the same image
```
![download](https://github.com/tensorflow/tensorflow/assets/109357590/94eb3abb-b7d9-4ddc-8c80-eeca0bcec3ba)

The code is self-explanatory. A problem exists in `tf.mage.resize` when the input image is not normalized. 
* Normalizing the input image or the output of `tf.mage.resize`
* converting the output (of format float32) to `int8` when the input is of `int8` will solve the problem.


### Standalone code to reproduce the issue


[Colab Link](https://colab.research.google.com/drive/1y41vpFs6Cd4NHmSY6IRmU53PXgl_SAFE#scrollTo=trFnmSDqtNxz&line=1&uniqifier=1)



### Relevant log output

_No response_