Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么在使用tf.image.resize时图像会失真?_Python 3.x_Python Imaging Library_Tensorflow2.0_Image Resizing - Fatal编程技术网

Python 3.x 为什么在使用tf.image.resize时图像会失真?

Python 3.x 为什么在使用tf.image.resize时图像会失真?,python-3.x,python-imaging-library,tensorflow2.0,image-resizing,Python 3.x,Python Imaging Library,Tensorflow2.0,Image Resizing,我使用的是Tensorflow 2.4.0,代码如下: import tensorflow as tf import numpy as np import PIL image=PIL.Image.open('apple.jpeg') uiu=tf.image.resize(np.array(image), (224,224)) PIL.Image.fromarray(uiu.numpy(),"RGB").save("apple1.jpeg")

我使用的是Tensorflow 2.4.0,代码如下:

import tensorflow as tf
import numpy as np

import PIL
image=PIL.Image.open('apple.jpeg')
uiu=tf.image.resize(np.array(image), (224,224))
    
PIL.Image.fromarray(uiu.numpy(),"RGB").save("apple1.jpeg")
据推测,
apple1.jpeg
应该与
apple.jpeg
相同,但它们的区别如下:

apple.jpeg

apple1.jpeg

使用
tf.image.resize
时,为什么图像会失真?

上的文档:返回值的类型为
float32
,除非
方法是
ResizeMethod.NEAREST_NEIGHBOR
,否则返回的数据类型是
图像的数据类型

因此,
uiu.numpy()
的结果是一些dtype
np.float32
的numpy数组,但仍然有
0范围内的值。。。255,该枕头无法正确保存为有意义的图像。因此,在保存图像时只需强制执行dtype
np.uint8
,即代替

PIL.Image.fromarray(uiu.numpy(),“RGB”).save(“apple1.jpeg”)
使用类似以下内容:

PIL.Image.fromarray(uiu.numpy().astype(np.uint8),“RGB”).save(“apple1.jpeg”)
一般来说:为什么不首先使用

----------------------------------------
系统信息
----------------------------------------
平台:Windows-10-10.0.16299-SP0
Python:3.8.5
NumPy:1.20.1
枕头:8.1.0
TensorFlow:2.4.1
----------------------------------------