The binary encoding of an image, like PNG, is NOT UTF-8. I am getting 
errors trying to encode a PNG and serialize it to protobuf. Please see a 
copy of my stack overflow question below.

How do I do this in Python if I can only use UTF-8 but images are not UTF-8?


0down votefavorite 
<https://stackoverflow.com/questions/48193722/python-protobuf-encode-png#>

I have a protobuf field of type bytes

I am trying to do:

b = open("apng.png", "rb").read()
msg2.imagebinary = b
msg2b = msg2.SerializeToString()

However I get the error:

ValueError: 
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xac\x00\x00\x00\xaa\x08\x06\x00\x00\x000h\xa4\xc5\x00\x00\x0c%iCCPICC
 
Profile\x00\x00H\x89\x95\x97\x07TSI\x17\xc7\xe7\x95$$$\xb4@(RBo"M\xba\xf4\x1a@@\xaa`#$\x81\x84\x12C
 
\xa8\xd8\xd1E\x05\xd7\x82\x88\x08VtU\xc4\xb6\x16@\x16\x15\xb1\x97E\xb0\xf7\x05\x11\x15e],\xd8P\xf9&\x05t\xdd\xaf\x9c\xef\x9e3\xef\xfd\xce}w\xee\xfc\xef\xbcys\xde\x00\xa0\x1a\xc3\x16\x89\xb2Q5\x00r\x84\xf9\xe2\xd8\xd0\x00\xe6\xc4\xe4\x14&\xe9\x11@\x00\n\xc8\xb0\xe9\xb39y"\xff\x98\x98H\x00m\xf8\xfew{w\x13FC\xbbf\'\xcd\xf5\xcf\xe7\xff\xd5\xd4\xb9\xbc<\x0e\x00H\x0c\xe44n\x1e\'\x07\xf2a\x00pW\x8eH\x9c\x0f\x00\xa1\x0f\xfaMg\xe4\x8b
 
\x13\xa1J\xa0)\x86\x02!\x9bI9C\xce\xeeRN\x93s\xa4,&>6\x10r*\x00JT6[\x9c\x01\x80\x8aT\x17\xb3\x80\x93\x01\xf3\xa8,\x87\xec
 
\xe4\n\x84\x90\x9b!\xfbp\xf8l.\xe4\xcf\x90G\xe7\xe4L\x87\xacj\x05\xd9*\xed\xbb<\x19\x7f\xcb\x996\x92\x93\xcd\xce\x18ay-2S\n\x12\xe4\x89\xb2\xd9\xb3\xfe\xcf\xe9\xf8\xdf\x96\x93-\x19\x1e\xc3\x146*_\x1c\x16+\xadY:oY\xd3#\xa4L\x85|^\x98\x16\x15\rY\x03\xf2u
 
has type bytes, but isn't valid UTF-8 encoding. Non-UTF-8 strings must be 
converted to unicode objects before being added.

EDIT: I have also tried:

from PIL import Image

i = Image.open(theimage)
b = i.tobytes()
msg2.imagebinary = b
msg2b = msg2.SerializeToString()


-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to