2

I am trying to take the contents of a BytesIO or StringIO object and use base64.standard_b64encode() to encode it. I have tried both. This works fine in python 2.7, however in python 3.5 I get the following error.

TypeError: Can't convert 'bytes' object to str implicitly

This is the portion of code having the problem.

output = BytesIO()
img.save(output,  format="PNG")
output.seek(0)


data = "data:image/png;base64," + base64.standard_b64encode(output.read())

html = "<html><body><img src='DATA'></body></html>"

I have seen references to fixing this error for strings using b"sting" but I don't know how that would apply to reading from a file.

Thanks

Antikythera
  • 91
  • 1
  • 8

1 Answers1

4

Turns out I the problem was not with the base64 encoding, but rather the string I was trying to append it to. I had to do the following so that python did not see it as a byte encoding anymore.

base64.b64encode(output.read()).decode()
Antikythera
  • 91
  • 1
  • 8