I want to convert the byte object exported with fig.to_image()
, to numpy array so it can be used in Folium as image overlay. (I can not save the image to disk)
How can this be done?
Here is an example:
import plotly.graph_objects as go
# Create plot
fig = go.Figure(data =
go.Contour(
z=[[10, 10.625, 12.5, 15.625, 20],
[5.625, 6.25, 8.125, 11.25, 15.625],
[2.5, 3.125, 5., 8.125, 12.5],
[0.625, 1.25, 3.125, 6.25, 10.625],
[0, 0.625, 2.5, 5.625, 10]]
))
# Export byte object
img_bytes = fig.to_image(format="png",width=600, height=350)
I have tried using PIL:
from PIL import Image
img = Image.frombytes("RGB", (350,600), img_bytes)
Getting ValueError: not enough image data
.
I have never used byte object before making this process very confusing for me.
PS: Any other way to use plotly figure on folium map is also appreciated.