使用語句:
1 image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read()
讀取圖像時報錯如下:
1 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
原因:
0x92 即 10010010,UTF8 中編碼一個字符的第一個字節(start byte)只可能是 0xxxxxxx、110xxxxx、1110xxx、11110xxx……而后面的字節只可能是 10xxxxxx。也就是說 0x92 只能作為后面的字節,卻出現在了第一個字節的位置。
出現這種問題絕大部分情況是因為文件不是 UTF8 編碼的(例如,可能是 GBK 編碼的),而系統默認采用 UTF8 解碼。解決方法是改為對應的解碼方式。
解決方案:
將“r”改為“rb”的形式,如下:
1 image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read()