Python Forum
Trying to get base64Encoded Matplotlib Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get base64Encoded Matplotlib Data
#1
Hello,

I have a matplotlib figure that I want to return as a base64Encoded variable so it can be stored as a blob object. This variable would be the return result of a REST service so saving it directly to disk does not fit the project requirements.

I've tried various methods, but I cannot quite get the base64Encoded approach to work.

After the matplotlib is done, the following does create a disk file correctly:

[b]plt.savefig[/b]('test_file_spectrogram.png')
Now, to return that same figure as a base64Encoded variable, I tried this:

my_stringIObytes = io.BytesIO()
plt.savefig(my_stringIObytes, format='png')
my_stringIObytes.seek(0)
my_base64_jpgData = base64.b64encode(my_stringIObytes.read())
But when I convert this back to base64.decode(), it does not display a file. In fact, the actual png above is about 100KB, but this version when converted back is only 4KB.

---

I also tried to just write the data from the above directly to disk without an intermediate program:

with open('base64_decode_test.png', 'wb') as fl:
    fl.write(my_stringIObytes.getbuffer())
    fl.close()
But that doesn't work correctly either.

Would appreciate some suggestions.

Thanks!

Oliver
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,945 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,220 Mar-11-2021, 10:52 AM
Last Post: buran
  Matplotlib Hovering Event displaying (x, y) data RawlinsCross 0 6,187 Oct-18-2019, 06:13 PM
Last Post: RawlinsCross
  Matplotlib contour no data coordinates b4rtt 1 2,640 Jun-18-2019, 09:36 AM
Last Post: b4rtt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020