tensorflow UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: in...

tensorflow讀取圖像出現錯誤:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start bytepython

#!/usr/bin/python # -*- coding: utf-8 -*-

import matplotlib.pyplot as plt import tensorflow as tf filename = "/home/zzz/1-Work/Documents/2-Codes/learning/tf/17flowers/jpg/0/image_0001.jpg" image_raw_data = tf.gfile.FastGFile(filename, "r").read() # 問題出在這裏,mode應該爲「rb」而不是「r」 with tf.Session() as sess: img_data = tf.image.decode_jpeg(image_raw_data) tmp = img_data.eval() print(tmp) plt.imshow(tmp) plt.show() img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8) encoded_img = tf.image.encode_jpeg(img_data) with tf.gfile.GFile("/home/zzz/tf_tmp", "wb") as f: f.write(encoded_img.eval())

緣由:在ui

image_raw_data = tf.gfile.FastGFile(filename, "r").read()

這一行,讀取的時候讀取方式應該是「rb」,在讀取模式只使用「r」的時候,python試圖將一個byte-array轉成utf-8字符串,這樣python就會遇到utf-8的非法字符: 0xff in position 0,遇到這種狀況時,能夠將讀取方式改成「rb」,這樣在讀取數據的時候,會將數據按照二進制讀取,就不會有上述的解碼問題。編碼

ps:引發這個問題的另外一個緣由多是所要讀取的數據是按照utf-16編碼的,在這種狀況下,能夠加關鍵字參數,encoding=「utf-16」解決。spa

 

ref: https://stackoverflow.com/questions/42339876/error-unicodedecodeerror-utf-8-codec-cant-decode-byte-0xff-in-position-0-incode

相關文章
相關標籤/搜索