在学习TensorFlow图像处理函数时,调用 tf.gfile.FastGFile( )函数时,出现了如上的问题。
import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像的原始数据
file_dir = 'D:\\PycharmWorkspace\\test\\flower.jpeg'
image_raw_data = tf.gfile.FastGFile(file_dir, 'r').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
# 使用pyplot工具可视化得到的图像
plt.imshow(img_data.eval())
plt.show()
百度没有解决问题,看了stackoverflow解决了,stackoverflow的解决方案。
解决办法:改为 'rb'模式
image_raw_data = tf.gfile.FastGFile(file_dir, 'rb').read()
大神的解释:Python tries to convert a byte-array (a bytes
which it assumes to be a utf-8-encoded string) to a unicode string (str
). This process of course is a decoding according to utf-8 rules. When it tries this, it encounters a byte sequence which is not allowed in utf-8-encoded strings (namely this 0xff at position 0).