When converting to base 64 TypeError: 'str' does not support the buffer interface

im = Image.open(filePath) # load image self.msg = str(bytearray(list(im.getdata()))) # convert image data to string encodedMsg = base64.b64encode(self.msg) 

when I tried to encode the data read from the image on base64, it returns an error:

 File "Steganography.py", line 42, in msgToXml encodedMsg = base64.b64encode(self.msg) File "/opt/python3/current/lib/python3.4/base64.py", line 62, in b64encode encoded = binascii.b2a_base64(s)[:-1] TypeError: 'str' does not support the buffer interface 

This works when I'm at home using Ubuntu (python 2.7). But it shows an error when I use the school machine (python3.4). How can i solve this?

+5
source share
2 answers
 encodedMsg = base64.b64encode(self.msg.encode('ascii')) 

reference: Base64 encoding in Python 3

+10
source

In short, this is due to the fact that Python3 underwent a major overhaul of the unicode / string / bytes system. You should read this https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit , and this is https: //docs.python. org / 3.3 / howto / unicode.html to understand what happened and how to deal with it.

To answer your specific problem - if you don't convert your bytearray to str, everything should work.

0
source

Source: https://habr.com/ru/post/1207768/


All Articles