far*_*had 3 python bytecode plotly folium
该
plotly.io.to_image函数用于以字节对象 ( Doc ) 的形式返回图像。
我想将表示 PNG 图像的字节对象转换为 numpy 数组,以便它可以在 Folium 中用作图像叠加层。
这是一个例子:
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)
Run Code Online (Sandbox Code Playgroud)
我尝试过使用 PIL:
from PIL import Image
img = Image.frombytes("RGB", (350,600), img_bytes)
Run Code Online (Sandbox Code Playgroud)
得到ValueError: not enough image data。
在让这个过程让我感到非常困惑之前,我从未使用过字节对象。
PS:在 folium 地图上使用绘图图的任何其他方式也值得赞赏。
在Plotly 论坛上得到了有效的答案:
这是将 Plotly 无花果图像转换为数组的函数:
import io
from PIL import Image
def plotly_fig2array(fig):
#convert Plotly fig to an array
fig_bytes = fig.to_image(format="png")
buf = io.BytesIO(fig_bytes)
img = Image.open(buf)
return np.asarray(img)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4325 次 |
| 最近记录: |