1

I need some help, and am at loss as to where to look. Using python with matplotlib, i generate some plots with matplotlib.pyplot.savefig to an BytesIO() object, and later change it to tempfile object just to see if it works. Should i use savefig to save them to a png file anywhere, it works. Should i take the data from BytesIO or tempfile and save it to a png it works. But i want to put them in an HTML doc, like so

        print(plots)
    for i, f in enumerate(plots):
        f.seek(0)
        html += """<div style="text-align: center">
<img style="max-width:100%" src=\"data:image/png;base64,{}\">

""".format(base64.b64encode(f.read()))

it genereates the file just fine, and base64 is clearly filled with data, but all my browsers see the images as broken. Point is this used to work in python 2.7, and now it doesn't, with no visible errors.

Kara
  • 11
  • 1

1 Answers1

0

I appear to have solved the problem: It's because base64 image is a byte object

base64.b64encode(f.read()).decode("UTF-8") instead of base64.b64encode(f.read()) Appears to work correctly. As such, this answer may be less than ideal. Shoutout to a certain anon who pointed this out to me.

Kara
  • 11
  • 1