我将图像从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 - 你应该(像这里的答案)直接存储二进制文件 –