我尝试将.png图像转换为float32,方法如下,结果得到的图像如图所示。如果我移除tf.image.convert_image_dtype
调用,一切正常。
image = tf.io.read_file(filename)image = tf.image.decode_png(image, channels=3)image = tf.image.convert_image_dtype(image, tf.float32)
我还尝试了不同格式的图像,如.bmp和.jpg,但结果相同。我用来可视化上述生成图像的代码如下:
a = a.numpy()a = Image.fromarray(a, 'RGB')
正如我所说,如果我只是移除tf.image.convert_image_dtype
调用,一切正常。
以下是两个图像的下载链接(由于我的声望低于10,我还不能上传照片)。
回答:
你可以这样将其转换回整数类型
import tensorflow as tfimport numpy as npfrom PIL import Imageimage = tf.io.read_file("C:\\<your file>.png")image = tf.image.decode_png(image, channels=3)image = tf.image.convert_image_dtype(image, tf.float32)a = image.numpy()a = (a * 255 / np.max(a)).astype('uint8')a = Image.fromarray(a, 'RGB')a.show()