将JPG图像添加到folium弹出菜单

2024-09-27 00:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图添加一个图片到叶弹出,但失败了。我使用python2.7版本和folium0.50版本。在

事实上,我在其他的帖子中也提到过这个页面,但是它仍然不起作用

http://nbviewer.jupyter.org/gist/ocefpaf/0ec5c93138744e5072847822818b4362

import folium

import base64

m = folium.Map(location = [33, -97], zoom_start = 6, tiles = "Mapbox Bright")

encoded = base64.b64encode(open('IMG_1769.JPG', 'rb').read()).decode()

html = '<img src="data:image/jpeg;base64,{}">'.format

iframe = folium.IFrame(html(encoded), width=632+20, height=420+20)

popup = folium.Popup(iframe, max_width=2650)

marker = folium.Marker([30,-100], popup=popup).add_to(m)

m.add_child(marker)

m.save("test.html")

Tags: import版本addhtml图片页面widthmarker
1条回答
网友
1楼 · 发布于 2024-09-27 00:18:06

我跟踪了this example,它(几乎)对我有用。绘图未正确解码base64,因为encoded变量是字节数组而不是字符串,因此产生了一个b'iVBOR头,而不是iVBOR报头(PNG报头的base64版本)。在

html(encoded)替换为html(encoded.decode('UTF-8'))修复了该问题。在

这是输出。在

Folium Heatmap with Graph


这是代码片段。在

    fig, ax = plt.subplots(figsize=(width, height))
    ax = subdf.plot(x='date', y='temperature', ax=ax, legend=False)
    ax.set_ylabel('Temp (°C)')
    png = '/tmp/temperatures_{}.png'.format(counter)
    fig.savefig(png, dpi=resolution)

    encoded = base64.b64encode(open(png, 'rb').read())



    html = '<img src="data:image/png;base64,{}">'.format
    #print(20*'-',encoded.decode('UTF-8'))
    iframe = IFrame(html(encoded.decode('UTF-8')), width=(width*resolution)+20, height=(height*resolution)+20)
    popup = folium.Popup(iframe, max_width=2650)

    icon = folium.Icon(color="red", icon="ok")
    marker = folium.Marker([lat, lon], popup=popup, icon=icon)
    marker.add_to(marker_cluster)

相关问题 更多 >

    热门问题