Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

How to get image bytes?

How to get png image bytes directly from Figure. Without saving the image locally or on the website.

Hi there,

What you can do is take your fig which is essentially a dictionary of all your data, then…

  • execute image_hex = py.image.get(fig, format='png', height=fig['layout']['height'], width=fig['layout']['width'])
    which uses py.image.get() to return the string of the png

-next make sure you import sys and execute sys.getsizeof(image_hex) which should give you the byte count

I tested this with a small scatterplot image which gave me a byte count of 4023, whereas clicking get info on the downloaded image gave me 4150, but the difference looks statistically insignificant.

Thanks! this is what I needed. But why does it still call the API? I don’t store anything on Plotly’s cloud. Is there a way to do the same thing with plotly.offline?

I believe the short answer to that is that you are still making a GET request, which is one of the ways you can interact with the server. It still counts for something, as does a POST request, which is more the bread and butter type of request.

Yes you can use py.image.get() in offline mode. I just tried it and it works.

Sorry for the late reply as well. Hope all this helps.