我將圖像從python後端傳輸到C++後端。我選擇了谷歌的protobuf,有以下簡單的結構:在Python中讀取jpeg文件,將其編碼爲Unicode並將其放入到protobuf中
message data {
optional string image = 1;
}
然後我用Python來讀取圖像,並把它變成像場:
data = server_pb2.data()
data.image = (open(image_fn).read())
但protobuf的以下消息抱怨:
Value Error: [hex data] has type str, but isn't in 7-bit ASCII encoding. Non-ASCII strings must be converted to unicode objects before being added.
我試過幾種方法來製作數據unicode但沒有成功。
也許有人遇到過這個問題嗎?還是有更好的方法來傳輸圖像數據?
謝謝!
您不會將二進制「編碼」爲Unicode - 即向後使用編碼器,結果未定義。她http://tiny.cc/io - 你應該(像這裏的答案)直接存儲二進制文件 –