Tensorflow的convert_image_dtype打破了图像



我以以下方式将。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,所以我还不能上传照片)。

original_imageobtained_image

你可以像这样把它转换回整数

import tensorflow as tf
import numpy as np
from PIL import Image
image = 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()

最新更新